|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +describe('Settings', () => { |
| 12 | + beforeEach(() => { |
| 13 | + jest.resetModules(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('lazy Platform initialization', () => { |
| 17 | + it('does not access Platform when the module is first loaded', () => { |
| 18 | + let platformAccessCount = 0; |
| 19 | + jest.doMock('../../Utilities/Platform', () => ({ |
| 20 | + __esModule: true, |
| 21 | + get default() { |
| 22 | + platformAccessCount++; |
| 23 | + return {OS: 'android'}; |
| 24 | + }, |
| 25 | + })); |
| 26 | + |
| 27 | + require('../Settings.js'); |
| 28 | + |
| 29 | + expect(platformAccessCount).toBe(0); |
| 30 | + |
| 31 | + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 32 | + const Settings = require('../Settings.js').default; |
| 33 | + Settings.get('any'); |
| 34 | + expect(platformAccessCount).toBeGreaterThan(0); |
| 35 | + warnSpy.mockRestore(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('can be required when Platform is undefined during module load', () => { |
| 39 | + jest.doMock('../../Utilities/Platform', () => ({ |
| 40 | + __esModule: true, |
| 41 | + default: undefined, |
| 42 | + })); |
| 43 | + |
| 44 | + expect(() => { |
| 45 | + require('../Settings.js'); |
| 46 | + }).not.toThrow(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('platform-specific implementation', () => { |
| 51 | + it('uses fallback implementation on Android', () => { |
| 52 | + jest.doMock('../../Utilities/Platform', () => ({ |
| 53 | + __esModule: true, |
| 54 | + default: {OS: 'android'}, |
| 55 | + })); |
| 56 | + |
| 57 | + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 58 | + const Settings = require('../Settings.js').default; |
| 59 | + |
| 60 | + expect(Settings.get('foo')).toBeNull(); |
| 61 | + expect(Settings.watchKeys('foo', () => {})).toBe(-1); |
| 62 | + expect(warnSpy).toHaveBeenCalled(); |
| 63 | + warnSpy.mockRestore(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('delegates get and set to the iOS implementation', () => { |
| 67 | + const setValues = jest.fn(); |
| 68 | + jest.doMock('../../Utilities/Platform', () => ({ |
| 69 | + __esModule: true, |
| 70 | + default: {OS: 'ios'}, |
| 71 | + })); |
| 72 | + jest.doMock('../NativeSettingsManager', () => ({ |
| 73 | + __esModule: true, |
| 74 | + default: { |
| 75 | + getConstants: () => ({settings: {myKey: 'initial'}}), |
| 76 | + setValues, |
| 77 | + }, |
| 78 | + })); |
| 79 | + |
| 80 | + const Settings = require('../Settings.js').default; |
| 81 | + |
| 82 | + expect(Settings.get('myKey')).toBe('initial'); |
| 83 | + Settings.set({myKey: 'updated'}); |
| 84 | + expect(Settings.get('myKey')).toBe('updated'); |
| 85 | + expect(setValues).toHaveBeenCalledWith({myKey: 'updated'}); |
| 86 | + }); |
| 87 | + |
| 88 | + it('caches the platform implementation across calls', () => { |
| 89 | + let platformAccessCount = 0; |
| 90 | + jest.doMock('../../Utilities/Platform', () => ({ |
| 91 | + __esModule: true, |
| 92 | + get default() { |
| 93 | + platformAccessCount++; |
| 94 | + return {OS: 'android'}; |
| 95 | + }, |
| 96 | + })); |
| 97 | + |
| 98 | + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 99 | + const Settings = require('../Settings.js').default; |
| 100 | + |
| 101 | + Settings.get('a'); |
| 102 | + Settings.get('b'); |
| 103 | + expect(platformAccessCount).toBe(1); |
| 104 | + warnSpy.mockRestore(); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments