@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.
54 lines (53 loc) • 2.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const caching_1 = require("./caching");
/**
* cacheKeyConcat
*/
describe('cacheKeyConcat', () => {
test('returns concatenated cache key string', () => {
const result = (0, caching_1.cacheKeyConcat)('user', '123', undefined, 'profile');
expect(result).toEqual('user.123.profile');
});
test('returns empty string for undefined arguments', () => {
const result = (0, caching_1.cacheKeyConcat)(undefined, 'posts', undefined);
expect(result).toEqual('posts');
});
test('returns empty string for no arguments', () => {
const result = (0, caching_1.cacheKeyConcat)();
expect(result).toEqual('');
});
});
/**
* readCacheKey
*/
describe('readCacheKey', () => {
it('should return endpointId when cacheKey is not specified', () => {
const result = (0, caching_1.readCacheKey)('endpoint');
expect(result).toEqual('endpoint');
});
it('should return cache key built when executed with a string parameter', () => {
const result = (0, caching_1.readCacheKey)('endpoint', 'id', { id: 123 });
expect(result).toEqual('endpoint.123');
});
it('should return cache key built when executed with an array of string parameters', () => {
const result = (0, caching_1.readCacheKey)('endpoint', ['id', 'name'], { id: 123, name: 'test' });
expect(result).toEqual('endpoint.123.test');
});
it('should return cache key built when executed with a function', () => {
const cacheKey = (params) => `${params === null || params === void 0 ? void 0 : params.id}-${params === null || params === void 0 ? void 0 : params.name}`;
const result = (0, caching_1.readCacheKey)('endpoint', cacheKey, { id: 123, name: 'test' });
expect(result).toEqual('endpoint.123-test');
});
it('should return undefined when cache key function returns falsy value', () => {
const cacheKey = () => '';
const result = (0, caching_1.readCacheKey)('endpoint', cacheKey, { id: 123, name: 'test' });
expect(result).toBeUndefined();
});
it('should strip out values when any of the cache key array parameters are falsy', () => {
const result1 = (0, caching_1.readCacheKey)('endpoint', ['id', 'name'], { id: 123, name: '' });
expect(result1).toBe('endpoint.123');
const result2 = (0, caching_1.readCacheKey)('endpoint', ['id', 'name'], { id: 123, name: undefined });
expect(result2).toBe('endpoint.123');
});
});