grafana/public/app/core/components/Signup/VerifyEmailPage.test.tsx
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

69 lines
2.5 KiB
TypeScript

import { fireEvent, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { render } from 'test/redux-rtl';
import { VerifyEmailPage } from './VerifyEmailPage';
const postMock = jest.fn();
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
getBackendSrv: () => ({
post: postMock,
}),
config: {
buildInfo: {
version: 'v1.0',
commit: '1',
env: 'production',
edition: 'Open Source',
},
licenseInfo: {
stateInfo: '',
licenseUrl: '',
},
verifyEmailEnabled: true,
appSubUrl: '',
},
}));
describe('VerifyEmail Page', () => {
it('renders correctly', () => {
render(<VerifyEmailPage />);
expect(screen.getByText('Verify Email')).toBeInTheDocument();
expect(screen.getByRole('textbox', { name: /Email/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Send verification email' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Back to login' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Back to login' })).toHaveAttribute('href', '/login');
});
it('should pass validation checks for email field', async () => {
render(<VerifyEmailPage />);
fireEvent.click(screen.getByRole('button', { name: 'Send verification email' }));
expect(await screen.findByText('Email is required')).toBeInTheDocument();
await userEvent.type(screen.getByRole('textbox', { name: /Email/i }), 'test');
await waitFor(() => expect(screen.queryByText('Email is invalid')).toBeInTheDocument());
await userEvent.type(screen.getByRole('textbox', { name: /Email/i }), 'test@gmail.com');
await waitFor(() => expect(screen.queryByText('Email is invalid')).not.toBeInTheDocument());
});
it('should show complete signup if email-verification is successful', async () => {
postMock.mockResolvedValueOnce({ message: 'SignUpCreated' });
render(<VerifyEmailPage />);
await userEvent.type(screen.getByRole('textbox', { name: /Email/i }), 'test@gmail.com');
fireEvent.click(screen.getByRole('button', { name: 'Send verification email' }));
await waitFor(() =>
expect(postMock).toHaveBeenCalledWith('/api/user/signup', {
email: 'test@gmail.com',
})
);
expect(screen.getByRole('link', { name: 'Complete Signup' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Complete Signup' })).toHaveAttribute('href', '/signup');
});
});