|
| 1 | +import { AutoEnvAttributes, LDLogger } from '@launchdarkly/js-client-sdk-common'; |
| 2 | + |
| 3 | +import { makeClient } from '../src/BrowserClient'; |
| 4 | + |
| 5 | +const mockPlatformConstructor = jest.fn(); |
| 6 | + |
| 7 | +jest.mock('../src/platform/BrowserPlatform', () => ({ |
| 8 | + __esModule: true, |
| 9 | + default: jest.fn().mockImplementation((logger, options, storage) => { |
| 10 | + mockPlatformConstructor(logger, options, storage); |
| 11 | + // eslint-disable-next-line global-require |
| 12 | + const { makeBasicPlatform } = require('./BrowserClient.mocks'); |
| 13 | + const platform = makeBasicPlatform(options); |
| 14 | + // Surface the storage the client wired in so we can assert on it, while |
| 15 | + // keeping a working mock platform for the rest of construction. |
| 16 | + if (storage) { |
| 17 | + platform.storage = storage; |
| 18 | + } |
| 19 | + return platform; |
| 20 | + }), |
| 21 | +})); |
| 22 | + |
| 23 | +let logger: LDLogger; |
| 24 | + |
| 25 | +beforeEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + logger = { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() }; |
| 28 | +}); |
| 29 | + |
| 30 | +function getWiredStorage() { |
| 31 | + return mockPlatformConstructor.mock.calls[0][2]; |
| 32 | +} |
| 33 | + |
| 34 | +it('wraps and passes a custom storage through to the platform', async () => { |
| 35 | + const myStorage = { |
| 36 | + get: jest.fn(async () => 'cached'), |
| 37 | + set: jest.fn(async () => {}), |
| 38 | + clear: jest.fn(async () => {}), |
| 39 | + }; |
| 40 | + |
| 41 | + makeClient( |
| 42 | + 'client-side-id', |
| 43 | + { key: 'user-key', kind: 'user' }, |
| 44 | + AutoEnvAttributes.Disabled, |
| 45 | + { streaming: false, logger, diagnosticOptOut: true, storage: myStorage }, |
| 46 | + ); |
| 47 | + |
| 48 | + const wired = getWiredStorage(); |
| 49 | + expect(wired).toBeDefined(); |
| 50 | + |
| 51 | + // The wired storage delegates to the user's implementation. |
| 52 | + await expect(wired.get('k')).resolves.toEqual('cached'); |
| 53 | + await wired.set('k', 'v'); |
| 54 | + await wired.clear('k'); |
| 55 | + expect(myStorage.get).toHaveBeenCalledWith('k'); |
| 56 | + expect(myStorage.set).toHaveBeenCalledWith('k', 'v'); |
| 57 | + expect(myStorage.clear).toHaveBeenCalledWith('k'); |
| 58 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 59 | +}); |
| 60 | + |
| 61 | +it('does not pass a storage override when none is configured', () => { |
| 62 | + makeClient( |
| 63 | + 'client-side-id', |
| 64 | + { key: 'user-key', kind: 'user' }, |
| 65 | + AutoEnvAttributes.Disabled, |
| 66 | + { streaming: false, logger, diagnosticOptOut: true }, |
| 67 | + ); |
| 68 | + |
| 69 | + expect(getWiredStorage()).toBeUndefined(); |
| 70 | +}); |
| 71 | + |
| 72 | +it('does not let a throwing custom storage escape the wrapper', async () => { |
| 73 | + const throwingStorage = { |
| 74 | + get: jest.fn(async () => { |
| 75 | + throw new Error('boom'); |
| 76 | + }), |
| 77 | + set: jest.fn(async () => { |
| 78 | + throw new Error('boom'); |
| 79 | + }), |
| 80 | + clear: jest.fn(async () => { |
| 81 | + throw new Error('boom'); |
| 82 | + }), |
| 83 | + }; |
| 84 | + |
| 85 | + makeClient( |
| 86 | + 'client-side-id', |
| 87 | + { key: 'user-key', kind: 'user' }, |
| 88 | + AutoEnvAttributes.Disabled, |
| 89 | + { streaming: false, logger, diagnosticOptOut: true, storage: throwingStorage }, |
| 90 | + ); |
| 91 | + |
| 92 | + const wired = getWiredStorage(); |
| 93 | + await expect(wired.get('k')).resolves.toBeNull(); |
| 94 | + await expect(wired.set('k', 'v')).resolves.toBeUndefined(); |
| 95 | + await expect(wired.clear('k')).resolves.toBeUndefined(); |
| 96 | + expect(logger.error).toHaveBeenCalledTimes(3); |
| 97 | +}); |
0 commit comments