Fix bug and remove useless stuff (#3861)
This commit is contained in:
@ -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' },
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -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,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user