Enforce front project structure through ESLINT (#7863)

Fixes: https://github.com/twentyhq/twenty/issues/7329
This commit is contained in:
Charles Bochet
2024-10-20 20:20:19 +02:00
committed by GitHub
parent f801f3aa9f
commit eccf0bf8ba
260 changed files with 500 additions and 290 deletions

View File

@ -0,0 +1,141 @@
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 { RecoilRoot, useRecoilValue } from 'recoil';
import { iconsState } from 'twenty-ui';
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 { 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 isDebugMode = useRecoilValue(isDebugModeState);
return {
...useAuth(),
client,
state: {
icons,
authProviders,
billing,
isSignInPrefilled,
supportChat,
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,
microsoft: false,
magicLink: false,
password: false,
});
expect(state.billing).toBeNull();
expect(state.isSignInPrefilled).toBe(false);
expect(state.supportChat).toEqual({
supportDriver: 'none',
supportFrontChatId: null,
});
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();
});
});

View File

@ -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);
});
});