Skip to content

Commit

Permalink
Add tests for modules/auth and modules/command-menu (#3548)
Browse files Browse the repository at this point in the history
Co-authored-by: gitstart-twenty <[email protected]>
Co-authored-by: v1b3m <[email protected]>
Co-authored-by: Matheus <[email protected]>
  • Loading branch information
4 people authored Jan 19, 2024
1 parent c868347 commit 1bb7ff3
Show file tree
Hide file tree
Showing 7 changed files with 663 additions and 0 deletions.
111 changes: 111 additions & 0 deletions packages/twenty-front/src/modules/auth/hooks/__mocks__/useAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {
ChallengeDocument,
SignUpDocument,
VerifyDocument,
} from '~/generated/graphql';

export const queries = {
challenge: ChallengeDocument,
verify: VerifyDocument,
signup: SignUpDocument,
};

export const email = '[email protected]';
export const password = 'testing';
export const token =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';

export const variables = {
challenge: {
email,
password,
},
verify: { loginToken: token },
signup: {},
};

export const results = {
challenge: {
loginToken: {
token,
expiresAt: '2022-01-01',
},
},
verify: {
user: {
id: 'id',
firstName: 'firstName',
lastName: 'lastName',
email: 'email',
canImpersonate: 'canImpersonate',
supportUserHash: 'supportUserHash',
workspaceMember: {
id: 'id',
name: {
firstName: 'firstName',
lastName: 'lastName',
},
colorScheme: 'colorScheme',
avatarUrl: 'avatarUrl',
locale: 'locale',
},
defaultWorkspace: {
id: 'id',
displayName: 'displayName',
logo: 'logo',
domainName: 'domainName',
inviteHash: 'inviteHash',
allowImpersonation: true,
subscriptionStatus: 'subscriptionStatus',
featureFlags: {
id: 'id',
key: 'key',
value: 'value',
workspaceId: 'workspaceId',
},
},
},
tokens: {
accessToken: { token, expiresAt: 'expiresAt' },
refreshToken: { token, expiresAt: 'expiresAt' },
},
signup: {},
},
signUp: { loginToken: { token, expiresAt: 'expiresAt' } },
};

export const mocks = [
{
request: {
query: queries.challenge,
variables: variables.challenge,
},
result: jest.fn(() => ({
data: {
challenge: results.challenge,
},
})),
},
{
request: {
query: queries.verify,
variables: variables.verify,
},
result: jest.fn(() => ({
data: {
verify: results.verify,
},
})),
},
{
request: {
query: queries.signup,
variables: variables.challenge,
},
result: jest.fn(() => ({
data: {
signUp: results.signUp,
},
})),
},
];
147 changes: 147 additions & 0 deletions packages/twenty-front/src/modules/auth/hooks/__test__/useAuth.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { ReactNode } from 'react';
import { useApolloClient } from '@apollo/client';
import { MockedProvider } from '@apollo/client/testing';
import { expect } from '@storybook/test';
import { act, renderHook } from '@testing-library/react';
import { RecoilRoot, useRecoilValue } from 'recoil';

import { useAuth } from '@/auth/hooks/useAuth';
import { authProvidersState } from '@/client-config/states/authProvidersState';
import { billingState } from '@/client-config/states/billingState';
import { isDebugModeState } from '@/client-config/states/isDebugModeState';
import { isSignInPrefilledState } from '@/client-config/states/isSignInPrefilledState';
import { supportChatState } from '@/client-config/states/supportChatState';
import { telemetryState } from '@/client-config/states/telemetryState';
import { iconsState } from '@/ui/display/icon/states/iconsState';

import { email, mocks, password, results, token } from '../__mocks__/useAuth';

const Wrapper = ({ children }: { children: ReactNode }) => (
<MockedProvider mocks={mocks} addTypename={false}>
<RecoilRoot>{children}</RecoilRoot>
</MockedProvider>
);

const renderHooks = () => {
const { result } = renderHook(
() => {
return useAuth();
},
{
wrapper: Wrapper,
},
);
return { result };
};

describe('useAuth', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should return challenge object', async () => {
const { result } = renderHooks();

await act(async () => {
expect(await result.current.challenge(email, password)).toStrictEqual(
results.challenge,
);
});

expect(mocks[0].result).toHaveBeenCalled();
});

it('should verify user', async () => {
const { result } = renderHooks();

await act(async () => {
await result.current.verify(token);
});

expect(mocks[1].result).toHaveBeenCalled();
});

it('should handle credential sign-in', async () => {
const { result } = renderHooks();

await act(async () => {
await result.current.signInWithCredentials(email, password);
});

expect(mocks[0].result).toHaveBeenCalled();
expect(mocks[1].result).toHaveBeenCalled();
});

it('should handle sign-out', async () => {
const { result } = renderHook(
() => {
const client = useApolloClient();
const icons = useRecoilValue(iconsState);
const authProviders = useRecoilValue(authProvidersState);
const billing = useRecoilValue(billingState);
const isSignInPrefilled = useRecoilValue(isSignInPrefilledState);
const supportChat = useRecoilValue(supportChatState);
const telemetry = useRecoilValue(telemetryState);
const isDebugMode = useRecoilValue(isDebugModeState);
return {
...useAuth(),
client,
state: {
icons,
authProviders,
billing,
isSignInPrefilled,
supportChat,
telemetry,
isDebugMode,
},
};
},
{
wrapper: Wrapper,
},
);

const { signOut, client } = result.current;

await act(async () => {
await signOut();
});

expect(sessionStorage.length).toBe(0);
expect(client.cache.extract()).toEqual({});

const { state } = result.current;

expect(state.icons).toEqual({});
expect(state.authProviders).toEqual({
google: false,
magicLink: false,
password: true,
});
expect(state.billing).toBeNull();
expect(state.isSignInPrefilled).toBe(false);
expect(state.supportChat).toEqual({
supportDriver: 'none',
supportFrontChatId: null,
});
expect(state.telemetry).toEqual({
enabled: true,
anonymizationEnabled: true,
});
expect(state.isDebugMode).toBe(false);
});

it('should handle credential sign-up', async () => {
const { result } = renderHooks();

await act(async () => {
const res = await result.current.signUpWithCredentials(email, password);
expect(res).toHaveProperty('user');
expect(res).toHaveProperty('workspaceMember');
expect(res).toHaveProperty('workspace');
});

expect(mocks[2].result).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { act } from 'react-dom/test-utils';
import { renderHook } from '@testing-library/react';
import { RecoilRoot, useSetRecoilState } from 'recoil';

import { useIsLogged } from '@/auth/hooks/useIsLogged';
import { tokenPairState } from '@/auth/states/tokenPairState';

const renderHooks = () => {
const { result } = renderHook(
() => {
const isLogged = useIsLogged();
const setTokenPair = useSetRecoilState(tokenPairState);

return {
isLogged,
setTokenPair,
};
},
{
wrapper: RecoilRoot,
},
);
return { result };
};

describe('useIsLogged', () => {
it('should return correct value', async () => {
const { result } = renderHooks();

expect(result.current.isLogged).toBe(false);

await act(async () => {
result.current.setTokenPair({
accessToken: {
expiresAt: '',
token: 'testToken',
},
refreshToken: {
expiresAt: '',
token: 'testToken',
},
});
});

expect(result.current.isLogged).toBe(true);
});
});
Loading

0 comments on commit 1bb7ff3

Please sign in to comment.