|
| 1 | +import type { FeatureFlags } from '../remote-feature-flag-controller-types'; |
| 2 | +import { |
| 3 | + ClientType, |
| 4 | + DistributionType, |
| 5 | + EnvironmentType, |
| 6 | +} from '../remote-feature-flag-controller-types'; |
| 7 | +import { ClientConfigApiService } from './client-config-api-service'; |
| 8 | + |
| 9 | +const BASE_URL = 'https://client-config.api.cx.metamask.io/v1'; |
| 10 | + |
| 11 | +describe('ClientConfigApiService', () => { |
| 12 | + let originalConsoleError: typeof console.error; |
| 13 | + let clientConfigApiService: ClientConfigApiService; |
| 14 | + let mockFetch: jest.Mock; |
| 15 | + |
| 16 | + const mockFeatureFlags: FeatureFlags = { |
| 17 | + feature1: false, |
| 18 | + feature2: { chrome: '<109' }, |
| 19 | + }; |
| 20 | + |
| 21 | + const networkError = new Error('Network error'); |
| 22 | + Object.assign(networkError, { |
| 23 | + response: { |
| 24 | + status: 503, |
| 25 | + statusText: 'Service Unavailable', |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + mockFetch = jest.fn(); |
| 31 | + clientConfigApiService = new ClientConfigApiService({ fetch: mockFetch }); |
| 32 | + }); |
| 33 | + |
| 34 | + beforeAll(() => { |
| 35 | + originalConsoleError = console.error; |
| 36 | + console.error = jest |
| 37 | + .spyOn(console, 'error') |
| 38 | + .mockImplementation() as unknown as typeof console.error; |
| 39 | + }); |
| 40 | + |
| 41 | + afterAll(() => { |
| 42 | + console.error = originalConsoleError; |
| 43 | + }); |
| 44 | + |
| 45 | + describe('fetchFlags', () => { |
| 46 | + it('should successfully fetch and return feature flags', async () => { |
| 47 | + mockFetch.mockResolvedValueOnce({ |
| 48 | + ok: true, |
| 49 | + status: 200, |
| 50 | + statusText: 'OK', |
| 51 | + json: async () => mockFeatureFlags, |
| 52 | + }); |
| 53 | + |
| 54 | + const result = await clientConfigApiService.fetchFlags( |
| 55 | + ClientType.Extension, |
| 56 | + DistributionType.Main, |
| 57 | + EnvironmentType.Production, |
| 58 | + ); |
| 59 | + |
| 60 | + expect(mockFetch).toHaveBeenCalledWith( |
| 61 | + `${BASE_URL}/flags?client=extension&distribution=main&environment=prod`, |
| 62 | + { cache: 'no-cache' }, |
| 63 | + ); |
| 64 | + |
| 65 | + expect(result).toStrictEqual({ |
| 66 | + error: false, |
| 67 | + message: 'Success', |
| 68 | + statusCode: '200', |
| 69 | + statusText: 'OK', |
| 70 | + cachedData: mockFeatureFlags, |
| 71 | + cacheTimestamp: expect.any(Number), |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should return cached data when API request fails and cached data is available', async () => { |
| 76 | + const cachedData = { feature3: true }; |
| 77 | + const cacheTimestamp = Date.now(); |
| 78 | + |
| 79 | + mockFetch.mockRejectedValueOnce(networkError); |
| 80 | + |
| 81 | + const result = await clientConfigApiService.fetchFlags( |
| 82 | + ClientType.Extension, |
| 83 | + DistributionType.Main, |
| 84 | + EnvironmentType.Production, |
| 85 | + cachedData, |
| 86 | + cacheTimestamp, |
| 87 | + ); |
| 88 | + |
| 89 | + expect(result).toStrictEqual({ |
| 90 | + error: true, |
| 91 | + message: 'Network error', |
| 92 | + statusCode: '503', |
| 93 | + statusText: 'Service Unavailable', |
| 94 | + cachedData, |
| 95 | + cacheTimestamp, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should return empty object when API request fails and cached data is not available', async () => { |
| 100 | + mockFetch.mockRejectedValueOnce(networkError); |
| 101 | + const result = await clientConfigApiService.fetchFlags( |
| 102 | + ClientType.Extension, |
| 103 | + DistributionType.Main, |
| 104 | + EnvironmentType.Production, |
| 105 | + ); |
| 106 | + const currentTime = Date.now(); |
| 107 | + expect(result).toStrictEqual({ |
| 108 | + error: true, |
| 109 | + message: 'Network error', |
| 110 | + statusCode: '503', |
| 111 | + statusText: 'Service Unavailable', |
| 112 | + cachedData: {}, |
| 113 | + cacheTimestamp: currentTime, |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should handle non-200 responses without cache data', async () => { |
| 118 | + mockFetch.mockResolvedValueOnce({ |
| 119 | + ok: false, |
| 120 | + status: 404, |
| 121 | + statusText: 'Not Found', |
| 122 | + }); |
| 123 | + |
| 124 | + const result = await clientConfigApiService.fetchFlags( |
| 125 | + ClientType.Extension, |
| 126 | + DistributionType.Main, |
| 127 | + EnvironmentType.Production, |
| 128 | + ); |
| 129 | + const currentTime = Date.now(); |
| 130 | + expect(result).toStrictEqual({ |
| 131 | + error: true, |
| 132 | + message: 'Failed to fetch flags', |
| 133 | + statusCode: '404', |
| 134 | + statusText: 'Not Found', |
| 135 | + cachedData: {}, |
| 136 | + cacheTimestamp: currentTime, |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should handle non-200 responses with cache data', async () => { |
| 141 | + const cachedData = { feature3: true }; |
| 142 | + const cacheTimestamp = Date.now(); |
| 143 | + mockFetch.mockResolvedValueOnce({ |
| 144 | + ok: false, |
| 145 | + status: 404, |
| 146 | + statusText: 'Not Found', |
| 147 | + }); |
| 148 | + |
| 149 | + const result = await clientConfigApiService.fetchFlags( |
| 150 | + ClientType.Extension, |
| 151 | + DistributionType.Main, |
| 152 | + EnvironmentType.Production, |
| 153 | + cachedData, |
| 154 | + cacheTimestamp, |
| 155 | + ); |
| 156 | + const currentTime = Date.now(); |
| 157 | + expect(result).toStrictEqual({ |
| 158 | + error: true, |
| 159 | + message: 'Failed to fetch flags', |
| 160 | + statusCode: '404', |
| 161 | + statusText: 'Not Found', |
| 162 | + cachedData, |
| 163 | + cacheTimestamp: currentTime, |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should handle invalid API responses', async () => { |
| 168 | + mockFetch.mockResolvedValueOnce({ |
| 169 | + ok: true, |
| 170 | + status: 200, |
| 171 | + statusText: 'OK', |
| 172 | + json: async () => null, // Invalid response |
| 173 | + }); |
| 174 | + |
| 175 | + const result = await clientConfigApiService.fetchFlags( |
| 176 | + ClientType.Extension, |
| 177 | + DistributionType.Main, |
| 178 | + EnvironmentType.Production, |
| 179 | + ); |
| 180 | + |
| 181 | + const currentTime = Date.now(); |
| 182 | + expect(result).toStrictEqual({ |
| 183 | + error: true, |
| 184 | + message: 'Invalid API response', |
| 185 | + statusCode: null, |
| 186 | + statusText: null, |
| 187 | + cachedData: {}, |
| 188 | + cacheTimestamp: currentTime, |
| 189 | + }); |
| 190 | + }); |
| 191 | + }); |
| 192 | +}); |
0 commit comments