Skip to content

Commit

Permalink
fix(auth): Add getCurrentUser mock and update tests
Browse files Browse the repository at this point in the history
Added `getCurrentUser` mock to support testing and adjusted test cases accordingly. Cleanup and reorganization of mock data and improved structure for consistency. Minor import adjustments for better readability.
  • Loading branch information
AMoreaux committed Dec 24, 2024
1 parent 43320ff commit 7ae3f26
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
27 changes: 20 additions & 7 deletions packages/twenty-front/src/modules/auth/hooks/__mocks__/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ChallengeDocument,
GetCurrentUserDocument,
SignUpDocument,
VerifyDocument,
} from '~/generated/graphql';
Expand All @@ -8,6 +9,7 @@ export const queries = {
challenge: ChallengeDocument,
verify: VerifyDocument,
signup: SignUpDocument,
getCurrentUser: GetCurrentUserDocument,
};

export const email = '[email protected]';
Expand All @@ -22,6 +24,7 @@ export const variables = {
},
verify: { loginToken: token },
signup: {},
getCurrentUser: {},
};

export const results = {
Expand All @@ -32,7 +35,14 @@ export const results = {
},
},
verify: {
user: {
tokens: {
accessToken: { token, expiresAt: 'expiresAt' },
refreshToken: { token, expiresAt: 'expiresAt' },
},
},
signUp: { loginToken: { token, expiresAt: 'expiresAt' } },
getCurrentUser: {
currentUser: {
id: 'id',
firstName: 'firstName',
lastName: 'lastName',
Expand Down Expand Up @@ -65,13 +75,7 @@ export const results = {
},
},
},
tokens: {
accessToken: { token, expiresAt: 'expiresAt' },
refreshToken: { token, expiresAt: 'expiresAt' },
},
signup: {},
},
signUp: { loginToken: { token, expiresAt: 'expiresAt' } },
};

export const mocks = [
Expand Down Expand Up @@ -108,4 +112,13 @@ export const mocks = [
},
})),
},
{
request: {
query: queries.getCurrentUser,
variables: variables.getCurrentUser,
},
result: jest.fn(() => ({
data: results.getCurrentUser,
})),
},
];
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useApolloClient } from '@apollo/client';
import { MockedProvider } from '@apollo/client/testing';
import { expect } from '@storybook/test';
import { act, renderHook } from '@testing-library/react';
import { ReactNode } from 'react';
import { ReactNode, act } from 'react';
import { RecoilRoot, useRecoilValue } from 'recoil';
import { iconsState } from 'twenty-ui';

Expand All @@ -15,6 +14,7 @@ import { workspaceAuthProvidersState } from '@/workspace/states/workspaceAuthPro

import { isMultiWorkspaceEnabledState } from '@/client-config/states/isMultiWorkspaceEnabledState';
import { email, mocks, password, results, token } from '../__mocks__/useAuth';
import { renderHook } from '@testing-library/react';

const Wrapper = ({ children }: { children: ReactNode }) => (
<MockedProvider mocks={mocks} addTypename={false}>
Expand Down Expand Up @@ -59,6 +59,7 @@ describe('useAuth', () => {
});

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

it('should handle credential sign-in', async () => {
Expand Down

0 comments on commit 7ae3f26

Please sign in to comment.