@rocketmakers/api-swr
Version:
Rocketmakers front-end library for parsing a generated Typescript API client into a set of configurable React hooks for fetching and mutating data.
68 lines (67 loc) • 3.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const react_1 = require("@testing-library/react");
const useCacheManager_1 = require("./useCacheManager");
// key getter
const keyGetter = jest.fn().mockReturnValue(['match-1', 'match-2', 'not-found']);
// mutate
const mutate = jest.fn();
// Mock SWR
jest.mock('swr', () => {
const actual = jest.requireActual('swr');
return Object.assign(Object.assign({}, actual), { useSWRConfig: jest.fn(() => ({
cache: {
keys: keyGetter,
get: jest.fn(),
set: jest.fn(),
delete: jest.fn(),
},
mutate,
})) });
});
describe('useCacheManager', () => {
it('mutate should call SWR mutate with args', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
mutate.mockClear();
const { result } = (0, react_1.renderHook)(() => (0, useCacheManager_1.useCacheManager)());
const arg1 = {};
const arg2 = true;
yield result.current.mutate('match-1', arg1, arg2);
expect(mutate).toHaveBeenCalledWith('match-1', arg1, arg2);
}));
it('mutateInfinite should call SWR mutate for each matching key', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
mutate.mockClear();
const { result } = (0, react_1.renderHook)(() => (0, useCacheManager_1.useCacheManager)());
const arg1 = {};
const arg2 = true;
yield result.current.mutateInfinite('match', arg1, arg2);
expect(mutate).toHaveBeenCalledTimes(2);
expect(mutate).toHaveBeenCalledWith('match-1', arg1, arg2);
expect(mutate).toHaveBeenCalledWith('match-2', arg1, arg2);
expect(mutate).not.toHaveBeenCalledWith('not-found', arg1, arg2);
}));
it('invalidate should call SWR mutate with key', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
mutate.mockClear();
const { result } = (0, react_1.renderHook)(() => (0, useCacheManager_1.useCacheManager)());
yield result.current.invalidate('match-1');
expect(mutate).toHaveBeenCalledWith('match-1');
}));
it('invalidateInfinite should call SWR mutate for each matching key', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
mutate.mockClear();
const { result } = (0, react_1.renderHook)(() => (0, useCacheManager_1.useCacheManager)());
yield result.current.invalidateInfinite('match');
expect(mutate).toHaveBeenCalledTimes(2);
expect(mutate).toHaveBeenCalledWith('match-1');
expect(mutate).toHaveBeenCalledWith('match-2');
expect(mutate).not.toHaveBeenCalledWith('not-found');
}));
it('should provide a function to clear the SWR cache when executed', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
mutate.mockClear();
const { result } = (0, react_1.renderHook)(() => (0, useCacheManager_1.useCacheManager)());
yield result.current.clearAll();
expect(mutate).toHaveBeenCalledTimes(3);
expect(mutate).toHaveBeenCalledWith('match-1', undefined, { revalidate: false });
expect(mutate).toHaveBeenCalledWith('match-2', undefined, { revalidate: false });
expect(mutate).toHaveBeenCalledWith('not-found', undefined, { revalidate: false });
}));
});