TWNTY-3381 - Add tests for modules/apollo (#3530)
Add tests for `modules/apollo` Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com>
This commit is contained in:
committed by
GitHub
parent
aa8d689e3e
commit
d8c9a94e14
@ -0,0 +1,100 @@
|
||||
import { useApolloClient } from '@apollo/client';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import { RecoilRoot, useRecoilValue } from 'recoil';
|
||||
|
||||
import { useOptimisticEffect } from '@/apollo/optimistic-effect/hooks/useOptimisticEffect';
|
||||
import { optimisticEffectState } from '@/apollo/optimistic-effect/states/optimisticEffectState';
|
||||
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
|
||||
|
||||
const Wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<MockedProvider addTypename={false}>
|
||||
<RecoilRoot>{children}</RecoilRoot>
|
||||
</MockedProvider>
|
||||
);
|
||||
|
||||
describe('useOptimisticEffect', () => {
|
||||
it('should work as expected', async () => {
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const optimisticEffect = useRecoilValue(optimisticEffectState);
|
||||
const client = useApolloClient();
|
||||
const { findManyRecordsQuery } = useObjectMetadataItem({
|
||||
objectNameSingular: 'person',
|
||||
});
|
||||
return {
|
||||
...useOptimisticEffect({ objectNameSingular: 'person' }),
|
||||
optimisticEffect,
|
||||
cache: client.cache,
|
||||
findManyRecordsQuery,
|
||||
};
|
||||
},
|
||||
{
|
||||
wrapper: Wrapper,
|
||||
},
|
||||
);
|
||||
|
||||
const {
|
||||
registerOptimisticEffect,
|
||||
unregisterOptimisticEffect,
|
||||
triggerOptimisticEffects,
|
||||
optimisticEffect,
|
||||
findManyRecordsQuery,
|
||||
} = result.current;
|
||||
|
||||
expect(registerOptimisticEffect).toBeDefined();
|
||||
expect(typeof registerOptimisticEffect).toBe('function');
|
||||
expect(optimisticEffect).toEqual({});
|
||||
|
||||
const optimisticEffectDefinition = {
|
||||
variables: {},
|
||||
definition: {
|
||||
typename: 'Person',
|
||||
resolver: () => ({
|
||||
people: [],
|
||||
pageInfo: {
|
||||
endCursor: '',
|
||||
hasNextPage: false,
|
||||
hasPreviousPage: false,
|
||||
startCursor: '',
|
||||
},
|
||||
edges: [],
|
||||
}),
|
||||
},
|
||||
};
|
||||
|
||||
act(() => {
|
||||
registerOptimisticEffect(optimisticEffectDefinition);
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.optimisticEffect).toHaveProperty('Person-{}');
|
||||
});
|
||||
|
||||
expect(
|
||||
result.current.cache.readQuery({ query: findManyRecordsQuery }),
|
||||
).toBeNull();
|
||||
|
||||
act(() => {
|
||||
triggerOptimisticEffects({
|
||||
typename: 'Person',
|
||||
createdRecords: [{ id: 'id-0' }],
|
||||
});
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(
|
||||
result.current.cache.readQuery({ query: findManyRecordsQuery }),
|
||||
).toHaveProperty('people');
|
||||
});
|
||||
|
||||
act(() => {
|
||||
unregisterOptimisticEffect(optimisticEffectDefinition);
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.optimisticEffect).not.toHaveProperty('Person-{}');
|
||||
expect(result.current.optimisticEffect).toEqual({});
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,50 @@
|
||||
import { useApolloClient } from '@apollo/client';
|
||||
import { MockedProvider } from '@apollo/client/testing';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
|
||||
import { useOptimisticEvict } from '@/apollo/optimistic-effect/hooks/useOptimisticEvict';
|
||||
|
||||
const Wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<MockedProvider addTypename={false}>
|
||||
<RecoilRoot>{children}</RecoilRoot>
|
||||
</MockedProvider>
|
||||
);
|
||||
|
||||
describe('useOptimisticEvict', () => {
|
||||
it('should perform cache eviction', async () => {
|
||||
const mockedData = {
|
||||
'someType:1': { __typename: 'someType', id: '1', fieldName: 'value1' },
|
||||
'someType:2': { __typename: 'someType', id: '2', fieldName: 'value2' },
|
||||
'otherType:1': { __typename: 'otherType', id: '1', fieldName: 'value3' },
|
||||
};
|
||||
|
||||
const { result } = renderHook(
|
||||
() => {
|
||||
const { cache } = useApolloClient();
|
||||
cache.restore(mockedData);
|
||||
|
||||
return {
|
||||
...useOptimisticEvict(),
|
||||
cache,
|
||||
};
|
||||
},
|
||||
{
|
||||
wrapper: Wrapper,
|
||||
},
|
||||
);
|
||||
|
||||
const { performOptimisticEvict, cache } = result.current;
|
||||
|
||||
act(() => {
|
||||
performOptimisticEvict('someType', 'fieldName', 'value1');
|
||||
});
|
||||
|
||||
const cacheSnapshot = cache.extract();
|
||||
|
||||
expect(cacheSnapshot).toEqual({
|
||||
'someType:2': { __typename: 'someType', id: '2', fieldName: 'value2' },
|
||||
'otherType:1': { __typename: 'otherType', id: '1', fieldName: 'value3' },
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user