Skip to content

Commit

Permalink
refactor: cover last 3 branches with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphe committed Oct 24, 2022
1 parent b0558a5 commit ee13f13
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/assertCacheEntry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { CacheMetadata } from './common';

export function logKey(key?: string) {
return key ? `for ${key} ` : '';
}

export function assertCacheEntry(
entry: unknown,
key?: string,
Expand All @@ -9,9 +13,9 @@ export function assertCacheEntry(
} {
if (!isRecord(entry)) {
throw new Error(
`Cache entry ${
key ? `for ${key} ` : ''
}is not a cache entry object, it's a ${typeof entry}`,
`Cache entry ${logKey(
key,
)}is not a cache entry object, it's a ${typeof entry}`,
);
}
if (
Expand All @@ -21,17 +25,13 @@ export function assertCacheEntry(
(entry.metadata.swr != null && typeof entry.metadata.swr !== 'number')
) {
throw new Error(
`Cache entry ${
key ? `for ${key} ` : ''
}does not have valid metadata property`,
`Cache entry ${logKey(key)}does not have valid metadata property`,
);
}

if (!('value' in entry)) {
throw new Error(
`Cache entry for ${
key ? `for ${key} ` : ''
}does not have a value property`,
`Cache entry for ${logKey(key)}does not have a value property`,
);
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/cachified.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
totalTtl,
} from './index';
import { Deferred } from './createBatch';
import { logKey } from './assertCacheEntry';

jest.mock('./index', () => {
if (process.version.startsWith('v18')) {
Expand Down Expand Up @@ -1249,7 +1250,9 @@ describe('cachified', () => {
(cb as Function)(new Error('Nope2'), null);
return false;
});
expect(cache.delete('test-0')).rejects.toThrowErrorMatchingInlineSnapshot(`"Nope2"`);
expect(cache.delete('test-0')).rejects.toThrowErrorMatchingInlineSnapshot(
`"Nope2"`,
);

// handle corrupt cache
await new Promise((res) => redis.set('test-3', '{{{', res));
Expand Down Expand Up @@ -1589,3 +1592,9 @@ describe('totalTtl helper', () => {
expect(totalTtl({ createdTime: 0, swr: 5 })).toBe(5);
});
});

describe('internal logKey helper', () => {
it('falls back to empty string, when no key given', () => {
expect(logKey()).toBe('');
});
});

0 comments on commit ee13f13

Please sign in to comment.