Fix bug and remove useless stuff (#3861)

This commit is contained in:
martmull
2024-02-06 18:06:15 +01:00
committed by GitHub
parent 7b8fffc3b8
commit eb54401afe
5 changed files with 0 additions and 80 deletions

View File

@ -1,50 +0,0 @@
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' },
});
});
});

View File

@ -1,24 +0,0 @@
import { useApolloClient } from '@apollo/client';
export const useOptimisticEvict = () => {
const cache = useApolloClient().cache;
const performOptimisticEvict = (
typename: string,
fieldName: string,
fieldValue: string,
) => {
const serializedCache = cache.extract();
Object.values(serializedCache)
.filter((item) => item.__typename === typename)
.forEach((item) => {
if (item[fieldName] === fieldValue) {
cache.evict({ id: cache.identify(item) });
}
});
};
return {
performOptimisticEvict,
};
};