LibreChat/client/src/components/Auth/__tests__/Login.spec.tsx
Dan Orlando 3634d8691a
Feat/startup config api (#518)
* feat: add api for config

* feat: add data service to client

* feat: update client pages with values from config endpoint

* test: update tests

* Update configurations and documentation to remove VITE_SHOW_GOOGLE_LOGIN_OPTION and change VITE_APP_TITLE to APP_TITLE

* include APP_TITLE with startup config

* Add test for new route

* update backend-review pipeline

* comment out test until we can figure out testing routes in CI

* update: .env.example

---------

Co-authored-by: fuegovic <32828263+fuegovic@users.noreply.github.com>
2023-06-15 12:36:34 -04:00

108 lines
3.4 KiB
TypeScript

import { render, waitFor } from 'layout-test-utils';
import userEvent from '@testing-library/user-event';
import Login from '../Login';
import * as mockDataProvider from '~/data-provider';
jest.mock('~/data-provider');
const setup = ({
useGetUserQueryReturnValue = {
isLoading: false,
isError: false,
data: {}
},
useLoginUserReturnValue = {
isLoading: false,
isError: false,
mutate: jest.fn(),
data: {},
isSuccess: false
},
useGetStartupCongfigReturnValue = {
isLoading: false,
isError: false,
data: {
googleLoginEnabled: true,
registrationEnabled: true,
serverDomain: 'mock-server'
}
}
} = {}) => {
const mockUseLoginUser = jest
.spyOn(mockDataProvider, 'useLoginUserMutation')
//@ts-ignore - we don't need all parameters of the QueryObserverSuccessResult
.mockReturnValue(useLoginUserReturnValue);
const mockUseGetUserQuery = jest
.spyOn(mockDataProvider, 'useGetUserQuery')
//@ts-ignore - we don't need all parameters of the QueryObserverSuccessResult
.mockReturnValue(useGetUserQueryReturnValue);
const mockUseGetStartupConfig = jest
.spyOn(mockDataProvider, 'useGetStartupConfig')
//@ts-ignore - we don't need all parameters of the QueryObserverSuccessResult
.mockReturnValue(useGetStartupCongfigReturnValue);
const renderResult = render(<Login />);
return {
...renderResult,
mockUseLoginUser,
mockUseGetUserQuery,
mockUseGetStartupConfig
};
};
test('renders login form', () => {
const { getByLabelText, getByRole } = setup();
expect(getByLabelText(/email/i)).toBeInTheDocument();
expect(getByLabelText(/password/i)).toBeInTheDocument();
expect(getByRole('button', { name: /Sign in/i })).toBeInTheDocument();
expect(getByRole('link', { name: /Sign up/i })).toBeInTheDocument();
expect(getByRole('link', { name: /Sign up/i })).toHaveAttribute('href', '/register');
expect(getByRole('link', { name: /Login with Google/i })).toBeInTheDocument();
expect(getByRole('link', { name: /Login with Google/i })).toHaveAttribute(
'href',
'mock-server/oauth/google'
);
});
test('calls loginUser.mutate on login', async () => {
const mutate = jest.fn();
const { getByLabelText, getByRole } = setup({
// @ts-ignore - we don't need all parameters of the QueryObserverResult
useLoginUserReturnValue: {
isLoading: false,
mutate: mutate,
isError: false
}
});
const emailInput = getByLabelText(/email/i);
const passwordInput = getByLabelText(/password/i);
const submitButton = getByRole('button', { name: /Sign in/i });
await userEvent.type(emailInput, 'test@test.com');
await userEvent.type(passwordInput, 'password');
await userEvent.click(submitButton);
waitFor(() => expect(mutate).toHaveBeenCalled());
});
test('Navigates to / on successful login', async () => {
const { getByLabelText, getByRole, history } = setup({
// @ts-ignore - we don't need all parameters of the QueryObserverResult
useLoginUserReturnValue: {
isLoading: false,
mutate: jest.fn(),
isError: false,
isSuccess: true
}
});
const emailInput = getByLabelText(/email/i);
const passwordInput = getByLabelText(/password/i);
const submitButton = getByRole('button', { name: /Sign in/i });
await userEvent.type(emailInput, 'test@test.com');
await userEvent.type(passwordInput, 'password');
await userEvent.click(submitButton);
waitFor(() => expect(history.location.pathname).toBe('/'));
});