|
1 | | -import { Cache } from './cache'; |
| 1 | +import { Cache, hashCacheKey } from './cache'; |
2 | 2 | import { clientCredentialsTokenCache } from './client-credentials-token-cache'; |
3 | 3 | import { destinationCache } from './destination'; |
4 | 4 | import type { AuthenticationType, Destination } from './destination'; |
@@ -152,6 +152,54 @@ describe('Cache', () => { |
152 | 152 | }); |
153 | 153 | }); |
154 | 154 |
|
| 155 | + describe('hashCacheKey', () => { |
| 156 | + it('produces the same hash for plain objects with different key insertion order', () => { |
| 157 | + expect(hashCacheKey({ a: 1, b: 2 })).toEqual( |
| 158 | + hashCacheKey({ b: 2, a: 1 }) |
| 159 | + ); |
| 160 | + }); |
| 161 | + |
| 162 | + it('produces different hashes for plain objects with different values', () => { |
| 163 | + expect(hashCacheKey({ a: 1 })).not.toEqual(hashCacheKey({ a: 2 })); |
| 164 | + }); |
| 165 | + |
| 166 | + it('produces the same hash for Maps with different insertion order', () => { |
| 167 | + const m1 = new Map([ |
| 168 | + ['x', 1], |
| 169 | + ['y', 2] |
| 170 | + ]); |
| 171 | + const m2 = new Map([ |
| 172 | + ['y', 2], |
| 173 | + ['x', 1] |
| 174 | + ]); |
| 175 | + expect(hashCacheKey({ m: m1 })).toEqual(hashCacheKey({ m: m2 })); |
| 176 | + }); |
| 177 | + |
| 178 | + it('produces the same hash for Sets with different insertion order', () => { |
| 179 | + const s1 = new Set([1, 2, 3]); |
| 180 | + const s2 = new Set([3, 1, 2]); |
| 181 | + expect(hashCacheKey({ s: s1 })).toEqual(hashCacheKey({ s: s2 })); |
| 182 | + }); |
| 183 | + |
| 184 | + it('produces the same hash for class instances regardless of property insertion order', () => { |
| 185 | + class Point { |
| 186 | + [key: string]: number; |
| 187 | + } |
| 188 | + const p1 = new Point(); |
| 189 | + p1.y = 2; |
| 190 | + p1.x = 1; |
| 191 | + const p2 = new Point(); |
| 192 | + p2.x = 1; |
| 193 | + p2.y = 2; |
| 194 | + expect(hashCacheKey({ p: p1 })).toEqual(hashCacheKey({ p: p2 })); |
| 195 | + }); |
| 196 | + it('preserves arrays as arrays (does not coerce to object)', () => { |
| 197 | + expect(hashCacheKey({ arr: [1, 2, 3] })).not.toEqual( |
| 198 | + hashCacheKey({ arr: { 0: 1, 1: 2, 2: 3 } }) |
| 199 | + ); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
155 | 203 | describe('getOrInsertComputed', () => { |
156 | 204 | it('calls computeFn on cache miss and stores result', () => { |
157 | 205 | const compute = jest.fn().mockReturnValue({ entry: destinationOne }); |
|
0 commit comments