|
| 1 | +import TransientContextProcessor from '../TransientContextProcessor'; |
| 2 | + |
| 3 | +describe('TransientContextProcessor', () => { |
| 4 | + let localStorage; |
| 5 | + let logger; |
| 6 | + let uv; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + localStorage = {}; |
| 10 | + logger = { |
| 11 | + warn: jest.fn(), |
| 12 | + }; |
| 13 | + uv = new TransientContextProcessor(localStorage, logger); |
| 14 | + }); |
| 15 | + |
| 16 | + it('rejects null user', async () => { |
| 17 | + await expect(uv.processContext(null)).rejects.toThrow(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('leaves user with string key unchanged', async () => { |
| 21 | + const u = { key: 'someone', name: 'me' }; |
| 22 | + expect(await uv.processContext(u)).toEqual(u); |
| 23 | + }); |
| 24 | + |
| 25 | + it('stringifies non-string key', async () => { |
| 26 | + const u0 = { key: 123, name: 'me' }; |
| 27 | + const u1 = { key: '123', name: 'me' }; |
| 28 | + expect(await uv.processContext(u0)).toEqual(u1); |
| 29 | + }); |
| 30 | + |
| 31 | + it('uses cached key for anonymous user', async () => { |
| 32 | + const cachedKey = 'thing'; |
| 33 | + let storageKey; |
| 34 | + localStorage.get = async key => { |
| 35 | + storageKey = key; |
| 36 | + return cachedKey; |
| 37 | + }; |
| 38 | + const u = { anonymous: true }; |
| 39 | + expect(await uv.processContext(u)).toEqual({ key: cachedKey, anonymous: true }); |
| 40 | + expect(storageKey).toEqual('ld:$anonUserId'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('generates and stores key for anonymous user', async () => { |
| 44 | + let storageKey; |
| 45 | + let storedValue; |
| 46 | + localStorage.get = async () => null; |
| 47 | + localStorage.set = async (key, value) => { |
| 48 | + storageKey = key; |
| 49 | + storedValue = value; |
| 50 | + }; |
| 51 | + const u0 = { anonymous: true }; |
| 52 | + const u1 = await uv.processContext(u0); |
| 53 | + expect(storedValue).toEqual(expect.anything()); |
| 54 | + expect(u1).toEqual({ key: storedValue, anonymous: true }); |
| 55 | + expect(storageKey).toEqual('ld:$anonUserId'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('generates and stores a key for each transient context in a multi-kind context', async () => { |
| 59 | + const context = { |
| 60 | + kind: 'multi', |
| 61 | + user: { transient: true }, |
| 62 | + org: { transient: true }, |
| 63 | + app: { key: 'app' }, |
| 64 | + }; |
| 65 | + |
| 66 | + const storage = {}; |
| 67 | + localStorage.get = async key => storage[key]; |
| 68 | + localStorage.set = async (key, value) => { |
| 69 | + storage[key] = value; |
| 70 | + }; |
| 71 | + |
| 72 | + const processed = await uv.processContext(context); |
| 73 | + expect(processed.user.key).toBeDefined(); |
| 74 | + expect(processed.user.key).not.toEqual(processed.org.key); |
| 75 | + expect(processed.org.key).toBeDefined(); |
| 76 | + expect(processed.app.key).toEqual('app'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('uses cached keys for context kinds that have already been generated', async () => { |
| 80 | + const context = { |
| 81 | + kind: 'multi', |
| 82 | + user: { transient: true }, |
| 83 | + org: { transient: true }, |
| 84 | + another: { transient: true }, |
| 85 | + app: { key: 'app' }, |
| 86 | + }; |
| 87 | + |
| 88 | + const storage = { |
| 89 | + 'ld:$contextKey:org': 'cachedOrgKey', |
| 90 | + 'ld:$anonUserId': 'cachedUserKey', |
| 91 | + }; |
| 92 | + localStorage.get = async key => storage[key]; |
| 93 | + localStorage.set = async (key, value) => { |
| 94 | + storage[key] = value; |
| 95 | + }; |
| 96 | + |
| 97 | + const processed = await uv.processContext(context); |
| 98 | + expect(processed.user.key).toEqual('cachedUserKey'); |
| 99 | + expect(processed.org.key).toEqual('cachedOrgKey'); |
| 100 | + expect(processed.another.key).toBeDefined(); |
| 101 | + expect(processed.app.key).toEqual('app'); |
| 102 | + }); |
| 103 | + |
| 104 | + it.each([{ anonymous: true }, { kind: 'user', transient: true }, { kind: 'multi', user: { transient: true } }])( |
| 105 | + 'uses the same key to store any user context (legacy, single, multi)', |
| 106 | + async context => { |
| 107 | + const storage = {}; |
| 108 | + localStorage.get = async key => expect(key).toEqual('ld:$anonUserId'); |
| 109 | + localStorage.set = async (key, value) => { |
| 110 | + storage[key] = value; |
| 111 | + }; |
| 112 | + await uv.processContext(context); |
| 113 | + } |
| 114 | + ); |
| 115 | +}); |
0 commit comments