From 6f73bb6eca4dff6e76048176bd42aa034ae7f931 Mon Sep 17 00:00:00 2001 From: Shaheer Kochai Date: Thu, 8 Aug 2024 09:18:23 +0430 Subject: [PATCH] feat: login flow tests (#5540) --- .../container/Login/__tests__/Login.test.tsx | 112 ++++++++++++++++++ frontend/src/container/Login/index.tsx | 9 +- frontend/src/mocks-server/handlers.ts | 20 ++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 frontend/src/container/Login/__tests__/Login.test.tsx diff --git a/frontend/src/container/Login/__tests__/Login.test.tsx b/frontend/src/container/Login/__tests__/Login.test.tsx new file mode 100644 index 0000000000..ace087ae39 --- /dev/null +++ b/frontend/src/container/Login/__tests__/Login.test.tsx @@ -0,0 +1,112 @@ +import Login from 'container/Login'; +import { act, fireEvent, render, screen, waitFor } from 'tests/test-utils'; + +const errorNotification = jest.fn(); +jest.mock('hooks/useNotifications', () => ({ + __esModule: true, + useNotifications: jest.fn(() => ({ + notifications: { + error: errorNotification, + }, + })), +})); + +describe('Login Flow', () => { + test('Login form is rendered correctly', async () => { + render(); + + const headingElement = screen.getByRole('heading', { + name: 'login_page_title', + }); + expect(headingElement).toBeInTheDocument(); + + const textboxElement = screen.getByRole('textbox'); + expect(textboxElement).toBeInTheDocument(); + + const buttonElement = screen.getByRole('button', { + name: 'button_initiate_login', + }); + expect(buttonElement).toBeInTheDocument(); + + const noAccountPromptElement = screen.getByText('prompt_no_account'); + expect(noAccountPromptElement).toBeInTheDocument(); + }); + + test(`Display "invalid_email" if email is not provided`, async () => { + render(); + + const buttonElement = screen.getByText('button_initiate_login'); + fireEvent.click(buttonElement); + + await waitFor(() => + expect(errorNotification).toHaveBeenCalledWith({ + message: 'invalid_email', + }), + ); + }); + + test('Display invalid_config if invalid email is provided and next clicked', async () => { + render(); + + const textboxElement = screen.getByRole('textbox'); + fireEvent.change(textboxElement, { + target: { value: 'failEmail@signoz.io' }, + }); + + const buttonElement = screen.getByRole('button', { + name: 'button_initiate_login', + }); + fireEvent.click(buttonElement); + + await waitFor(() => + expect(errorNotification).toHaveBeenCalledWith({ + message: 'invalid_config', + }), + ); + }); + + test('providing shaheer@signoz.io as email and pressing next, should make the login_with_sso button visible', async () => { + render(); + act(() => { + fireEvent.change(screen.getByTestId('email'), { + target: { value: 'shaheer@signoz.io' }, + }); + + fireEvent.click(screen.getByTestId('initiate_login')); + }); + + await waitFor(() => { + expect(screen.getByText('login_with_sso')).toBeInTheDocument(); + }); + }); + + test('Display email, password, forgot password if password=Y', () => { + render(); + + const emailTextBox = screen.getByTestId('email'); + expect(emailTextBox).toBeInTheDocument(); + + const passwordTextBox = screen.getByTestId('password'); + expect(passwordTextBox).toBeInTheDocument(); + + const forgotPasswordLink = screen.getByText('forgot_password'); + expect(forgotPasswordLink).toBeInTheDocument(); + }); + + test('Display tooltip with "prompt_forgot_password" if forgot password is clicked while password=Y', async () => { + render(); + const forgotPasswordLink = screen.getByText('forgot_password'); + + act(() => { + fireEvent.mouseOver(forgotPasswordLink); + }); + + await waitFor(() => { + const forgotPasswordTooltip = screen.getByRole('tooltip', { + name: 'prompt_forgot_password', + }); + expect(forgotPasswordLink).toBeInTheDocument(); + expect(forgotPasswordTooltip).toBeInTheDocument(); + }); + }); +}); diff --git a/frontend/src/container/Login/index.tsx b/frontend/src/container/Login/index.tsx index 81a6c535a5..265169fcfd 100644 --- a/frontend/src/container/Login/index.tsx +++ b/frontend/src/container/Login/index.tsx @@ -220,6 +220,7 @@ function Login({ - + {t('forgot_password')} @@ -250,6 +256,7 @@ function Login({ loading={precheckInProcess} type="primary" onClick={onNextHandler} + data-testid="initiate_login" > {t('button_initiate_login')} diff --git a/frontend/src/mocks-server/handlers.ts b/frontend/src/mocks-server/handlers.ts index dae41a06b0..87287a0d1a 100644 --- a/frontend/src/mocks-server/handlers.ts +++ b/frontend/src/mocks-server/handlers.ts @@ -132,6 +132,26 @@ export const handlers = [ return res(ctx.status(500)); }, ), + rest.get('http://localhost/api/v1/loginPrecheck', (req, res, ctx) => { + const email = req.url.searchParams.get('email'); + if (email === 'failEmail@signoz.io') { + return res(ctx.status(500)); + } + + return res( + ctx.status(200), + ctx.json({ + status: 'success', + data: { + sso: true, + ssoUrl: '', + canSelfRegister: false, + isUser: true, + ssoError: '', + }, + }), + ); + }), rest.get('http://localhost/api/v2/licenses', (req, res, ctx) => res(ctx.status(200), ctx.json(licensesSuccessResponse)),