-
Notifications
You must be signed in to change notification settings - Fork 14
/
fallback.spec.ts
93 lines (79 loc) · 4 KB
/
fallback.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Context, Status } from '@tinkoff/request-core';
import { metaTypes } from '@tinkoff/request-cache-utils';
import fallback from './fallback';
const next = jest.fn();
const getCacheKey = jest.fn((req) => req.url);
const mockPersistentCache = {
get: jest.fn(),
put: jest.fn(),
};
jest.mock('./persistent-cache', () => () => mockPersistentCache);
describe('plugins/cache/fallback', () => {
beforeEach(() => {
next.mockClear();
getCacheKey.mockClear();
mockPersistentCache.put.mockClear();
mockPersistentCache.get.mockClear();
});
it('save to cache on complete', () => {
const request = { url: 'test[123]//pf' };
const response = { a: 1, b: 2 };
const plugin = fallback({ getCacheKey, shouldExecute: true });
const context = new Context({ request, response });
plugin.complete(context, next, null);
expect(getCacheKey).toHaveBeenLastCalledWith(request);
expect(mockPersistentCache.put).toHaveBeenLastCalledWith(
encodeURIComponent(request.url),
response,
expect.any(Function)
);
expect(mockPersistentCache.get).not.toHaveBeenCalled();
});
it('tries return from cache on error', () => {
const fromCache = { test: 'pfpf' };
const request = { url: 'test[123]//pf' };
const error = new Error('123');
const plugin = fallback({ getCacheKey, shouldExecute: true });
const context = new Context({ request, error });
const next = jest.fn();
mockPersistentCache.get.mockImplementation((_, cb) => cb(null, fromCache));
context.updateExternalMeta = jest.fn(context.updateExternalMeta.bind(context));
plugin.error(context, next, null);
expect(getCacheKey).toHaveBeenLastCalledWith(request);
expect(mockPersistentCache.put).not.toHaveBeenCalled();
expect(mockPersistentCache.get).toHaveBeenLastCalledWith(encodeURIComponent(request.url), expect.any(Function));
expect(context.updateExternalMeta).toHaveBeenLastCalledWith(metaTypes.CACHE, { fallbackCache: true });
expect(next).toHaveBeenLastCalledWith({ status: Status.COMPLETE, response: fromCache });
});
it('tries return from cache on error, but persist cache errors', () => {
const request = { url: 'test[123]//pf' };
const error = new Error('123');
const plugin = fallback({ getCacheKey, shouldExecute: true });
const context = new Context({ request, error });
const next = jest.fn();
mockPersistentCache.get.mockImplementation((_, cb) => cb(error));
context.updateExternalMeta = jest.fn(context.updateExternalMeta.bind(context));
plugin.error(context, next, null);
expect(getCacheKey).toHaveBeenLastCalledWith(request);
expect(mockPersistentCache.put).not.toHaveBeenCalled();
expect(mockPersistentCache.get).toHaveBeenLastCalledWith(encodeURIComponent(request.url), expect.any(Function));
expect(context.updateExternalMeta).not.toHaveBeenLastCalledWith(metaTypes.CACHE, { fromFallback: true });
expect(next).toHaveBeenLastCalledWith();
});
it('if shouldFallback returns false, do not use cache', () => {
const request = { url: 'test[123]//pf' };
const error = new Error('123');
const shouldFallback = jest.fn(() => false);
const plugin = fallback({ getCacheKey, shouldFallback, shouldExecute: true });
const context = new Context({ request, error });
const next = jest.fn();
context.updateExternalMeta = jest.fn(context.updateExternalMeta.bind(context));
plugin.error(context, next, null);
expect(shouldFallback).toHaveBeenCalledWith(context.getState());
expect(getCacheKey).not.toHaveBeenCalled();
expect(mockPersistentCache.put).not.toHaveBeenCalled();
expect(mockPersistentCache.get).not.toHaveBeenCalled();
expect(context.updateExternalMeta).not.toHaveBeenCalled();
expect(next).toHaveBeenLastCalledWith();
});
});