Skip to content

Commit

Permalink
Added tests for okta login base screen
Browse files Browse the repository at this point in the history
  • Loading branch information
surajeaton committed Jul 25, 2024
1 parent e2aaa51 commit 6bb8ae1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import { render, screen, fireEvent, RenderResult } from '@testing-library/react';
import '@testing-library/jest-dom';
import { OktaLoginScreenBase } from './OktaLoginScreenBase';
import { useOktaAuth } from '@okta/okta-react';
import { Box } from '@mui/material';
import { ErrorContextProvider } from '../../contexts/ErrorContext';
import { OktaLoginScreenProps } from './types';
import { errorContextProviderProps } from '../../contexts/ErrorContext/ErrorContextProvider.test';

jest.mock('@okta/okta-react', () => ({
useOktaAuth: jest.fn(),
}));

const mockSignInWithRedirect = jest.fn();
const mockOnContactSupport = jest.fn();

describe('OktaLoginScreenBase', () => {
beforeEach(() => {
(useOktaAuth as jest.Mock).mockReturnValue({
authState: { isAuthenticated: false },
oktaAuth: { signInWithRedirect: mockSignInWithRedirect },
});
});

const renderer = (props?: OktaLoginScreenProps): RenderResult =>
render(
<ErrorContextProvider {...errorContextProviderProps}>
<OktaLoginScreenBase {...props} />
</ErrorContextProvider>
);

it('renders correctly', () => {
renderer({ header: <Box data-testid="test-header">Test Header</Box> });
expect(screen.getByText('Test Header')).toBeInTheDocument();
});

it('calls signInWithRedirect on login button click', () => {
renderer({ loginButtonLabel: 'Login' });
const loginButton = screen.getByText('Login');
fireEvent.click(loginButton);
expect(mockSignInWithRedirect).toHaveBeenCalled();
});

it('calls onContactSupport on contact support link click', () => {
renderer({
showContactSupport: true,
contactSupportLabel: 'Contact Support',
onContactSupport: mockOnContactSupport,
});
const contactSupportLink = screen.getByText('Contact Support');
fireEvent.click(contactSupportLink);
expect(mockOnContactSupport).toHaveBeenCalled();
});
});
2 changes: 1 addition & 1 deletion login-workflow/src/screens/OktaLoginScreen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type OktaLoginScreenProps = WorkflowCardBaseProps & {
/**
* Options for configuring the Okta Auth SDK.
*/
oktaAuthOptions: OktaAuthOptions;
oktaAuthOptions?: OktaAuthOptions;

/**
* The label for the username field
Expand Down

0 comments on commit 6bb8ae1

Please sign in to comment.