Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Goldziher committed Jan 2, 2024
1 parent a581ea5 commit fc45c5c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
9 changes: 9 additions & 0 deletions frontend/src/components/sign-in/firebase-login.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,13 @@ describe('FirebaseLogin tests', () => {
getEnv().NEXT_PUBLIC_FRONTEND_HOST + Navigation.PrivacyPolicy,
);
});

it('should open the reset password modal when the reset password button is clicked', () => {
render(<FirebaseLogin setLoading={vi.fn()} isInitialized={true} />);
const resetPasswordButton: HTMLButtonElement = screen.getByTestId(
'reset-password-button',
);
fireEvent.click(resetPasswordButton);
expect(screen.getByTestId('password-reset-modal')).toBeInTheDocument();
});
});
4 changes: 2 additions & 2 deletions frontend/src/components/sign-in/firebase-login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export function FirebaseLogin({
<div className="flex justify-end">
<button
className="btn btn-xs btn-link"
data-testid="reset-password-button"
onClick={() => {
setResetPWModalOpen(true);
}}
Expand Down Expand Up @@ -180,12 +181,11 @@ export function FirebaseLogin({
</button>
))}
<div className="card-section-divider" />

<div
data-testid="tos-and-privacy-policy-container"
className="text-xs text-center"
>
<span>{`${t('userAgreementMessage')} `}</span>
<span>{t('userAgreementMessage')}</span>
<a
className="link link-primary"
href={host + Navigation.TOS}
Expand Down
109 changes: 109 additions & 0 deletions frontend/src/components/sign-in/password-reset-modal.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { fireEvent, render, screen, waitFor } from 'tests/test-utils';

import { PasswordResetModal } from '@/components/sign-in/password-reset-modal';

describe('PasswordResetModal tests', () => {
it('renders correctly', async () => {
render(
<PasswordResetModal
handleCloseModal={vi.fn()}
handleResetPassword={vi.fn()}
/>,
);

await waitFor(() => {
expect(
screen.getByTestId('password-reset-modal'),
).toBeInTheDocument();
});

expect(
screen.getByTestId('password-reset-input-container'),
).toBeInTheDocument();
expect(
screen.getByTestId('password-reset-input-label'),
).toBeInTheDocument();
expect(screen.getByTestId('password-reset-input')).toBeInTheDocument();
expect(
screen.getByTestId('password-reset-button-container'),
).toBeInTheDocument();
expect(
screen.getByTestId('password-reset-cancel-button'),
).toBeInTheDocument();
expect(
screen.getByTestId('password-reset-submit-button'),
).toBeInTheDocument();
});

it('calls handleResetPassword when submit button is clicked', async () => {
const handleResetPassword = vi.fn();
render(
<PasswordResetModal
handleCloseModal={vi.fn()}
handleResetPassword={handleResetPassword}
/>,
);

await waitFor(() => {
expect(
screen.getByTestId('password-reset-modal'),
).toBeInTheDocument();
});

const input = screen.getByTestId('password-reset-input');

fireEvent.change(input, { target: { value: '[email protected]' } });

const submitButton = screen.getByTestId('password-reset-submit-button');
expect(submitButton).toBeEnabled();

fireEvent.click(submitButton);

expect(handleResetPassword).toHaveBeenCalled();
});

it('calls handleCloseModal when cancel button is clicked', async () => {
const handleCloseModal = vi.fn();
render(
<PasswordResetModal
handleCloseModal={handleCloseModal}
handleResetPassword={vi.fn()}
/>,
);

await waitFor(() => {
expect(
screen.getByTestId('password-reset-modal'),
).toBeInTheDocument();
});

const cancelButton = screen.getByTestId('password-reset-cancel-button');

fireEvent.click(cancelButton);

expect(handleCloseModal).toHaveBeenCalled();
});

it('disables submit button when email is invalid', async () => {
const handleResetPassword = vi.fn();
render(
<PasswordResetModal
handleCloseModal={vi.fn()}
handleResetPassword={handleResetPassword}
/>,
);

await waitFor(() => {
expect(
screen.getByTestId('password-reset-modal'),
).toBeInTheDocument();
});

const input = screen.getByTestId('password-reset-input');

fireEvent.change(input, { target: { value: 'invalidemail' } });

const submitButton = screen.getByTestId('password-reset-submit-button');
expect(submitButton).toBeDisabled();
});
});

0 comments on commit fc45c5c

Please sign in to comment.