|
| 1 | +import type { EvaluationDetails, Hook, HookContext } from '@openfeature/web-sdk'; |
| 2 | +import { DebounceHook } from './debounce-hook'; |
| 3 | + |
| 4 | +describe('DebounceHook', () => { |
| 5 | + describe('caching', () => { |
| 6 | + afterAll(() => { |
| 7 | + jest.resetAllMocks(); |
| 8 | + }); |
| 9 | + |
| 10 | + const innerHook: Hook = { |
| 11 | + before: jest.fn(), |
| 12 | + after: jest.fn(), |
| 13 | + error: jest.fn(), |
| 14 | + finally: jest.fn(), |
| 15 | + }; |
| 16 | + |
| 17 | + const supplier = (flagKey: string) => flagKey; |
| 18 | + |
| 19 | + const hook = new DebounceHook<string>(innerHook, { |
| 20 | + beforeCacheKeySupplier: supplier, |
| 21 | + afterCacheKeySupplier: supplier, |
| 22 | + errorCacheKeySupplier: supplier, |
| 23 | + finallyCacheKeySupplier: supplier, |
| 24 | + ttlMs: 60_000, |
| 25 | + maxCacheItems: 100, |
| 26 | + }); |
| 27 | + |
| 28 | + const evaluationDetails: EvaluationDetails<string> = { |
| 29 | + value: 'testValue', |
| 30 | + } as EvaluationDetails<string>; |
| 31 | + const err: Error = new Error('fake error!'); |
| 32 | + const context = {}; |
| 33 | + const hints = {}; |
| 34 | + |
| 35 | + it.each([ |
| 36 | + { |
| 37 | + flagKey: 'flag1', |
| 38 | + calledTimesTotal: 1, |
| 39 | + }, |
| 40 | + { |
| 41 | + flagKey: 'flag2', |
| 42 | + calledTimesTotal: 2, |
| 43 | + }, |
| 44 | + { |
| 45 | + flagKey: 'flag1', |
| 46 | + calledTimesTotal: 2, // should not have been incremented, same cache key |
| 47 | + }, |
| 48 | + ])('should cache each stage based on supplier', ({ flagKey, calledTimesTotal }) => { |
| 49 | + hook.before({ flagKey, context } as HookContext, hints); |
| 50 | + hook.after({ flagKey, context } as HookContext, evaluationDetails, hints); |
| 51 | + hook.error({ flagKey, context } as HookContext, err, hints); |
| 52 | + hook.finally({ flagKey, context } as HookContext, evaluationDetails, hints); |
| 53 | + |
| 54 | + expect(innerHook.before).toHaveBeenNthCalledWith(calledTimesTotal, expect.objectContaining({ context }), hints); |
| 55 | + expect(innerHook.after).toHaveBeenNthCalledWith( |
| 56 | + calledTimesTotal, |
| 57 | + expect.objectContaining({ context }), |
| 58 | + evaluationDetails, |
| 59 | + hints, |
| 60 | + ); |
| 61 | + expect(innerHook.error).toHaveBeenNthCalledWith( |
| 62 | + calledTimesTotal, |
| 63 | + expect.objectContaining({ context }), |
| 64 | + err, |
| 65 | + hints, |
| 66 | + ); |
| 67 | + expect(innerHook.finally).toHaveBeenNthCalledWith( |
| 68 | + calledTimesTotal, |
| 69 | + expect.objectContaining({ context }), |
| 70 | + evaluationDetails, |
| 71 | + hints, |
| 72 | + ); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('options', () => { |
| 77 | + afterAll(() => { |
| 78 | + jest.resetAllMocks(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('maxCacheItems should limit size', () => { |
| 82 | + const innerHook: Hook = { |
| 83 | + before: jest.fn(), |
| 84 | + }; |
| 85 | + |
| 86 | + const hook = new DebounceHook<string>(innerHook, { |
| 87 | + beforeCacheKeySupplier: (flagKey: string) => flagKey, |
| 88 | + ttlMs: 60_000, |
| 89 | + maxCacheItems: 1, |
| 90 | + }); |
| 91 | + |
| 92 | + hook.before({ flagKey: 'flag1' } as HookContext, {}); |
| 93 | + hook.before({ flagKey: 'flag2' } as HookContext, {}); |
| 94 | + hook.before({ flagKey: 'flag1' } as HookContext, {}); |
| 95 | + |
| 96 | + // every invocation should have run since we have only maxCacheItems: 1 |
| 97 | + expect(innerHook.before).toHaveBeenCalledTimes(3); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should rerun inner hook stage only after ttl', async () => { |
| 101 | + const innerHook: Hook = { |
| 102 | + before: jest.fn(), |
| 103 | + }; |
| 104 | + |
| 105 | + const flagKey = 'some-flag'; |
| 106 | + |
| 107 | + const hook = new DebounceHook<string>(innerHook, { |
| 108 | + beforeCacheKeySupplier: (flagKey: string) => flagKey, |
| 109 | + ttlMs: 500, |
| 110 | + maxCacheItems: 1, |
| 111 | + }); |
| 112 | + |
| 113 | + hook.before({ flagKey } as HookContext, {}); |
| 114 | + hook.before({ flagKey } as HookContext, {}); |
| 115 | + hook.before({ flagKey } as HookContext, {}); |
| 116 | + |
| 117 | + await new Promise((r) => setTimeout(r, 1000)); |
| 118 | + |
| 119 | + hook.before({ flagKey } as HookContext, {}); |
| 120 | + |
| 121 | + // only the first and last should have invoked the inner hook |
| 122 | + expect(innerHook.before).toHaveBeenCalledTimes(2); |
| 123 | + }); |
| 124 | + |
| 125 | + it('noop if supplier not defined', () => { |
| 126 | + const innerHook: Hook = { |
| 127 | + before: jest.fn(), |
| 128 | + after: jest.fn(), |
| 129 | + error: jest.fn(), |
| 130 | + finally: jest.fn(), |
| 131 | + }; |
| 132 | + |
| 133 | + const flagKey = 'some-flag'; |
| 134 | + const context = {}; |
| 135 | + const hints = {}; |
| 136 | + |
| 137 | + // no suppliers defined, so we no-op (do no caching) |
| 138 | + const hook = new DebounceHook<string>(innerHook, { |
| 139 | + ttlMs: 60_000, |
| 140 | + maxCacheItems: 100, |
| 141 | + }); |
| 142 | + |
| 143 | + const evaluationDetails: EvaluationDetails<string> = { |
| 144 | + value: 'testValue', |
| 145 | + } as EvaluationDetails<string>; |
| 146 | + |
| 147 | + for (let i = 0; i < 3; i++) { |
| 148 | + hook.before({ flagKey, context } as HookContext, hints); |
| 149 | + hook.after({ flagKey, context } as HookContext, evaluationDetails, hints); |
| 150 | + hook.error({ flagKey, context } as HookContext, hints); |
| 151 | + hook.finally({ flagKey, context } as HookContext, evaluationDetails, hints); |
| 152 | + } |
| 153 | + |
| 154 | + // every invocation should have run since we have only maxCacheItems: 1 |
| 155 | + expect(innerHook.before).toHaveBeenCalledTimes(3); |
| 156 | + expect(innerHook.after).toHaveBeenCalledTimes(3); |
| 157 | + expect(innerHook.error).toHaveBeenCalledTimes(3); |
| 158 | + expect(innerHook.finally).toHaveBeenCalledTimes(3); |
| 159 | + }); |
| 160 | + |
| 161 | + it.each([ |
| 162 | + { |
| 163 | + cacheErrors: false, |
| 164 | + timesCalled: 2, // should be called each time since the hook always errors |
| 165 | + }, |
| 166 | + { |
| 167 | + cacheErrors: true, |
| 168 | + timesCalled: 1, // should be called once since we cached the error |
| 169 | + }, |
| 170 | + ])('should cache errors if cacheErrors set', ({ cacheErrors, timesCalled }) => { |
| 171 | + const innerErrorHook: Hook = { |
| 172 | + before: jest.fn(() => { |
| 173 | + // throw an error |
| 174 | + throw new Error('fake!'); |
| 175 | + }), |
| 176 | + }; |
| 177 | + |
| 178 | + const flagKey = 'some-flag'; |
| 179 | + const context = {}; |
| 180 | + |
| 181 | + // this hook caches error invocations |
| 182 | + const hook = new DebounceHook<string>(innerErrorHook, { |
| 183 | + beforeCacheKeySupplier: (flagKey: string) => flagKey, |
| 184 | + maxCacheItems: 100, |
| 185 | + ttlMs: 60_000, |
| 186 | + cacheErrors, |
| 187 | + }); |
| 188 | + |
| 189 | + expect(() => hook.before({ flagKey, context } as HookContext)).toThrow(); |
| 190 | + expect(() => hook.before({ flagKey, context } as HookContext)).toThrow(); |
| 191 | + |
| 192 | + expect(innerErrorHook.before).toHaveBeenCalledTimes(timesCalled); |
| 193 | + }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments