|
| 1 | +// (C) Copyright 2015 Moodle Pty Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { CoreResultMemoiser } from '@classes/result-memoiser'; |
| 16 | + |
| 17 | +describe('CoreResultMemoiser', () => { |
| 18 | + |
| 19 | + it('should cache and retrieve results with set, get, and isValid', () => { |
| 20 | + const memoiser = new CoreResultMemoiser<string>(); |
| 21 | + |
| 22 | + // Initially, no result is cached. |
| 23 | + expect(memoiser.get()).toBeUndefined(); |
| 24 | + expect(memoiser.get('param1')).toBeUndefined(); |
| 25 | + expect(memoiser.isValid()).toBe(false); |
| 26 | + expect(memoiser.isValid('param1')).toBe(false); |
| 27 | + |
| 28 | + // Set a result with parameters. |
| 29 | + memoiser.set('result', 'param1', 'param2'); |
| 30 | + expect(memoiser.get('param1', 'param2')).toBe('result'); |
| 31 | + expect(memoiser.isValid('param1', 'param2')).toBe(true); |
| 32 | + |
| 33 | + // Wrong params return undefined and are invalid. |
| 34 | + expect(memoiser.get('param1', 'different')).toBeUndefined(); |
| 35 | + expect(memoiser.get('param1')).toBeUndefined(); |
| 36 | + expect(memoiser.get('param1', 'param2', 'extra')).toBeUndefined(); |
| 37 | + expect(memoiser.isValid('param1')).toBe(false); |
| 38 | + expect(memoiser.isValid('param1', 'param2', 'extra')).toBe(false); |
| 39 | + |
| 40 | + // Overwrite with new params. |
| 41 | + memoiser.set('second', 'param3'); |
| 42 | + expect(memoiser.get('param1', 'param2')).toBeUndefined(); |
| 43 | + expect(memoiser.get('param3')).toBe('second'); |
| 44 | + |
| 45 | + // Handle no parameters. |
| 46 | + memoiser.set('42'); |
| 47 | + expect(memoiser.get()).toBe('42'); |
| 48 | + |
| 49 | + // Handle falsy values. |
| 50 | + const memoiserFalsy = new CoreResultMemoiser<string | number | boolean | null>(); |
| 51 | + memoiserFalsy.set('', 'empty'); |
| 52 | + expect(memoiserFalsy.get('empty')).toBe(''); |
| 53 | + |
| 54 | + memoiserFalsy.set(0, 'zero'); |
| 55 | + expect(memoiserFalsy.get('zero')).toBe(0); |
| 56 | + |
| 57 | + memoiserFalsy.set(false, 'false'); |
| 58 | + expect(memoiserFalsy.get('false')).toBe(false); |
| 59 | + |
| 60 | + memoiserFalsy.set(null, 'null'); |
| 61 | + expect(memoiserFalsy.get('null')).toBe(null); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should validate params with primitive types', () => { |
| 65 | + const memoiser = new CoreResultMemoiser<string>(); |
| 66 | + memoiser.set('result', 'string', 123, true, null, undefined); |
| 67 | + |
| 68 | + expect(memoiser.isValid('string', 123, true, null, undefined)).toBe(true); |
| 69 | + expect(memoiser.isValid('different', 123, true, null, undefined)).toBe(false); |
| 70 | + expect(memoiser.isValid('string', 456, true, null, undefined)).toBe(false); |
| 71 | + expect(memoiser.isValid('string', 123, false, null, undefined)).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should validate params with objects and arrays using deep equality', () => { |
| 75 | + const memoiser = new CoreResultMemoiser<string>(); |
| 76 | + const obj = { a: 1, b: { c: 2 } }; |
| 77 | + memoiser.set('result', obj); |
| 78 | + |
| 79 | + // Object deep equality. |
| 80 | + expect(memoiser.isValid({ a: 1, b: { c: 2 } })).toBe(true); |
| 81 | + expect(memoiser.isValid({ a: 1, b: { c: 3 } })).toBe(false); |
| 82 | + expect(memoiser.isValid({ a: 1 })).toBe(false); |
| 83 | + |
| 84 | + // Array deep equality. |
| 85 | + memoiser.set('array-result', [1, 2, 3]); |
| 86 | + expect(memoiser.isValid([1, 2, 3])).toBe(true); |
| 87 | + expect(memoiser.isValid([1, 2])).toBe(false); |
| 88 | + expect(memoiser.isValid([1, 2, 4])).toBe(false); |
| 89 | + expect(memoiser.isValid([1, 2, 3, 4])).toBe(false); |
| 90 | + |
| 91 | + // Mixed primitives and objects. |
| 92 | + memoiser.set('mixed', 'string', 123, { key: 'value' }, true); |
| 93 | + expect(memoiser.isValid('string', 123, { key: 'value' }, true)).toBe(true); |
| 94 | + expect(memoiser.isValid('string', 123, { key: 'different' }, true)).toBe(false); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should clear cache with invalidate', () => { |
| 98 | + const memoiser = new CoreResultMemoiser<string>(); |
| 99 | + memoiser.set('result', 'param1', 'param2', 'param3'); |
| 100 | + |
| 101 | + expect(memoiser.get('param1', 'param2', 'param3')).toBe('result'); |
| 102 | + expect(memoiser.isValid('param1', 'param2', 'param3')).toBe(true); |
| 103 | + |
| 104 | + memoiser.invalidate(); |
| 105 | + |
| 106 | + expect(memoiser.get('param1', 'param2', 'param3')).toBeUndefined(); |
| 107 | + expect(memoiser.isValid('param1', 'param2', 'param3')).toBe(false); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should memoise function results', () => { |
| 111 | + const memoiser = new CoreResultMemoiser<string>(); |
| 112 | + const fn = jest.fn(() => 'result'); |
| 113 | + |
| 114 | + // First call executes function and caches. |
| 115 | + let result = memoiser.memoise(fn, 'param1'); |
| 116 | + expect(fn).toHaveBeenCalledTimes(1); |
| 117 | + expect(result).toBe('result'); |
| 118 | + expect(memoiser.get('param1')).toBe('result'); |
| 119 | + |
| 120 | + // Second call with same params uses cache. |
| 121 | + result = memoiser.memoise(fn, 'param1'); |
| 122 | + expect(fn).toHaveBeenCalledTimes(1); |
| 123 | + expect(result).toBe('result'); |
| 124 | + |
| 125 | + // Different params re-execute. |
| 126 | + memoiser.invalidate(); |
| 127 | + const fn2 = jest.fn((param: string) => `result-${param}`); |
| 128 | + memoiser.memoise(() => fn2('param1'), 'param1'); |
| 129 | + memoiser.memoise(() => fn2('param2'), 'param2'); |
| 130 | + expect(fn2).toHaveBeenCalledTimes(2); |
| 131 | + expect(memoiser.get('param1')).toBe(undefined); |
| 132 | + expect(memoiser.get('param2')).toBe('result-param2'); |
| 133 | + |
| 134 | + // Object parameters. |
| 135 | + const memoiserObj = new CoreResultMemoiser<string>(); |
| 136 | + const fnObj = jest.fn(() => 'result'); |
| 137 | + memoiserObj.memoise(fnObj, { a: 1 }); |
| 138 | + memoiserObj.memoise(fnObj, { a: 1 }); // Same object structure |
| 139 | + memoiserObj.memoise(fnObj, { a: 2 }); // Different object |
| 140 | + expect(fnObj).toHaveBeenCalledTimes(2); |
| 141 | + expect(memoiserObj.get({ a: 1 })).toBe(undefined); |
| 142 | + expect(memoiserObj.get({ a: 2 })).toBe('result'); |
| 143 | + |
| 144 | + // Complex return types |
| 145 | + const memoiserComplex = new CoreResultMemoiser<{ value: string; count: number }>(); |
| 146 | + const fnComplex = jest.fn((param: string) => ({ value: `result-${param}`, count: 42 })); |
| 147 | + const result1 = memoiserComplex.memoise(() => fnComplex('first'), 'param'); |
| 148 | + const result2 = memoiserComplex.memoise(() => fnComplex('second'), 'param'); |
| 149 | + expect(fnComplex).toHaveBeenCalledTimes(1); |
| 150 | + expect(result1).toEqual({ value: 'result-first', count: 42 }); |
| 151 | + expect(result2).toBe(result1); // Same reference |
| 152 | + }); |
| 153 | + |
| 154 | +}); |
0 commit comments