|
| 1 | +import { mockApiData } from './mock-api-data'; |
| 2 | +import cloudConfig, { CloudConfigData } from '../index'; |
| 3 | + |
| 4 | +let cloudConfigData: CloudConfigData[] = []; |
| 5 | + |
| 6 | +const decryptSecret = 'CLIENT_/lMAHJcive'; |
| 7 | + |
| 8 | +describe('fetchAllConfigs', () => { |
| 9 | + it('should fetch configs successfully', async () => { |
| 10 | + const mockResponse = { |
| 11 | + ok: true, |
| 12 | + json: jest.fn().mockResolvedValue(mockApiData), |
| 13 | + }; |
| 14 | + const mockFetch = jest.fn().mockResolvedValue(mockResponse); |
| 15 | + global.fetch = mockFetch; |
| 16 | + |
| 17 | + cloudConfigData = await cloudConfig.fetchAll({ |
| 18 | + decryptSecret, |
| 19 | + }); |
| 20 | + |
| 21 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 22 | + expect(mockFetch).toHaveBeenCalledWith( |
| 23 | + expect.any(String), |
| 24 | + expect.any(Object) |
| 25 | + ); |
| 26 | + expect(mockResponse.json).toHaveBeenCalledTimes(1); |
| 27 | + expect(cloudConfigData[2].featureKey).toEqual(mockApiData[2].featureKey); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should throw an error when response is not ok', async () => { |
| 31 | + const mockResponse = { ok: false, status: 404, statusText: 'Not Found' }; |
| 32 | + const mockFetch = jest.fn().mockResolvedValue(mockResponse); |
| 33 | + global.fetch = mockFetch; |
| 34 | + |
| 35 | + const result = await cloudConfig.fetchAll(); |
| 36 | + expect(result.length).toEqual(0); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should catch and log errors', async () => { |
| 40 | + const mockError = new Error('Test error'); |
| 41 | + const mockFetch = jest.fn().mockRejectedValue(mockError); |
| 42 | + global.fetch = mockFetch; |
| 43 | + |
| 44 | + const consoleSpy = jest.spyOn(console, 'log').mockImplementation(); |
| 45 | + |
| 46 | + const result = await cloudConfig.fetchAll(); |
| 47 | + |
| 48 | + expect(consoleSpy).toHaveBeenCalledTimes(1); |
| 49 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 50 | + '💔💔💔 fetchAllConfigs error:', |
| 51 | + mockError |
| 52 | + ); |
| 53 | + expect(result).toEqual([]); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe('getConfigWithDefaultValue', () => { |
| 58 | + it('should return the correct cloud config value', () => { |
| 59 | + const boolean_2 = cloudConfig.getWithDefault({ |
| 60 | + configs: cloudConfigData, |
| 61 | + featureKey: 'boolean_2', |
| 62 | + defaultValue: true, |
| 63 | + }); |
| 64 | + expect(boolean_2).toEqual(false); |
| 65 | + |
| 66 | + const key_not_exists = cloudConfig.getWithDefault({ |
| 67 | + configs: cloudConfigData, |
| 68 | + featureKey: 'key_not_exists', |
| 69 | + defaultValue: 'default_value_if_key_not_exists', |
| 70 | + }); |
| 71 | + expect(key_not_exists).toEqual('default_value_if_key_not_exists'); |
| 72 | + |
| 73 | + const encrypt_array_001 = cloudConfig.getWithDefault({ |
| 74 | + configs: cloudConfigData, |
| 75 | + projectName: 'project001', |
| 76 | + groupName: 'group001', |
| 77 | + featureKey: 'encrypt_key_001', |
| 78 | + defaultValue: ['encrypt_key_001_value', 'value2'], |
| 79 | + }); |
| 80 | + expect(encrypt_array_001).toEqual([ |
| 81 | + 'a1', |
| 82 | + 'a2', |
| 83 | + 's3', |
| 84 | + 's a7', |
| 85 | + 'hamilton east', |
| 86 | + ]); |
| 87 | + |
| 88 | + const encrypt_json_001 = cloudConfig.getWithDefault({ |
| 89 | + configs: cloudConfigData, |
| 90 | + featureKey: 'json_key_1', |
| 91 | + defaultValue: { message: 'json message' }, |
| 92 | + }); |
| 93 | + expect(encrypt_json_001.message).toEqual('I am JSON'); |
| 94 | + |
| 95 | + const not_enabled_key = cloudConfig.get({ |
| 96 | + configs: cloudConfigData, |
| 97 | + featureKey: 'float_key_1', |
| 98 | + }); |
| 99 | + expect(not_enabled_key).toEqual(null); |
| 100 | + }); |
| 101 | +}); |
0 commit comments