|
| 1 | +import { FlagsmithClient } from './FlagsmithClient'; |
| 2 | + |
| 3 | +describe('FlagsmithClient', () => { |
| 4 | + const baseUrl = 'http://localhost:7007/api/proxy/flagsmith'; |
| 5 | + let client: FlagsmithClient; |
| 6 | + let mockFetch: jest.Mock; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + mockFetch = jest.fn(); |
| 10 | + client = new FlagsmithClient( |
| 11 | + { getBaseUrl: jest.fn().mockResolvedValue('http://localhost:7007/api/proxy') }, |
| 12 | + { fetch: mockFetch }, |
| 13 | + ); |
| 14 | + }); |
| 15 | + |
| 16 | + const mockOk = (data: unknown) => ({ ok: true, json: async () => data }); |
| 17 | + const mockError = (status: string) => ({ ok: false, statusText: status }); |
| 18 | + |
| 19 | + describe('getOrganizations', () => { |
| 20 | + it('fetches organizations', async () => { |
| 21 | + const org = { id: 1, name: 'Test Org', created_date: '2024-01-01' }; |
| 22 | + mockFetch.mockResolvedValue(mockOk({ results: [org] })); |
| 23 | + |
| 24 | + const result = await client.getOrganizations(); |
| 25 | + |
| 26 | + expect(mockFetch).toHaveBeenCalledWith(`${baseUrl}/organisations/`); |
| 27 | + expect(result).toEqual([org]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('throws on error', async () => { |
| 31 | + mockFetch.mockResolvedValue(mockError('Unauthorized')); |
| 32 | + await expect(client.getOrganizations()).rejects.toThrow('Failed to fetch organizations: Unauthorized'); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('getProjectsInOrg', () => { |
| 37 | + it('fetches projects', async () => { |
| 38 | + const project = { id: 1, name: 'Project', organisation: 1, created_date: '2024-01-01' }; |
| 39 | + mockFetch.mockResolvedValue(mockOk({ results: [project] })); |
| 40 | + |
| 41 | + const result = await client.getProjectsInOrg(1); |
| 42 | + |
| 43 | + expect(mockFetch).toHaveBeenCalledWith(`${baseUrl}/organisations/1/projects/`); |
| 44 | + expect(result).toEqual([project]); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('getProject', () => { |
| 49 | + it('fetches single project', async () => { |
| 50 | + const project = { id: 123, name: 'Project', organisation: 1, created_date: '2024-01-01' }; |
| 51 | + mockFetch.mockResolvedValue(mockOk(project)); |
| 52 | + |
| 53 | + const result = await client.getProject(123); |
| 54 | + |
| 55 | + expect(mockFetch).toHaveBeenCalledWith(`${baseUrl}/projects/123/`); |
| 56 | + expect(result).toEqual(project); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('getProjectFeatures', () => { |
| 61 | + it('fetches features', async () => { |
| 62 | + const features = [{ id: 1, name: 'feature', created_date: '2024-01-01', project: 1 }]; |
| 63 | + mockFetch.mockResolvedValue(mockOk({ results: features })); |
| 64 | + |
| 65 | + const result = await client.getProjectFeatures('123'); |
| 66 | + |
| 67 | + expect(mockFetch).toHaveBeenCalledWith(`${baseUrl}/projects/123/features/`); |
| 68 | + expect(result).toEqual(features); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('getProjectEnvironments', () => { |
| 73 | + it('fetches environments', async () => { |
| 74 | + const envs = [{ id: 1, name: 'Dev', api_key: 'key', project: 123 }]; |
| 75 | + mockFetch.mockResolvedValue(mockOk({ results: envs })); |
| 76 | + |
| 77 | + const result = await client.getProjectEnvironments(123); |
| 78 | + |
| 79 | + expect(mockFetch).toHaveBeenCalledWith(`${baseUrl}/projects/123/environments/`); |
| 80 | + expect(result).toEqual(envs); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('getUsageData', () => { |
| 85 | + it('fetches usage data', async () => { |
| 86 | + const usage = [{ flags: 100, identities: 50, traits: 25, environment_document: 10, day: '2024-01-01', labels: {} }]; |
| 87 | + mockFetch.mockResolvedValue(mockOk(usage)); |
| 88 | + |
| 89 | + const result = await client.getUsageData(1); |
| 90 | + |
| 91 | + expect(mockFetch).toHaveBeenCalledWith(`${baseUrl}/organisations/1/usage-data/`); |
| 92 | + expect(result).toEqual(usage); |
| 93 | + }); |
| 94 | + |
| 95 | + it('includes project_id filter', async () => { |
| 96 | + mockFetch.mockResolvedValue(mockOk([])); |
| 97 | + await client.getUsageData(1, 123); |
| 98 | + expect(mockFetch).toHaveBeenCalledWith(`${baseUrl}/organisations/1/usage-data/?project_id=123`); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('getFeatureVersions', () => { |
| 103 | + it('fetches versions', async () => { |
| 104 | + const versions = [{ uuid: 'v1', is_live: true, published: true }]; |
| 105 | + mockFetch.mockResolvedValue(mockOk({ results: versions })); |
| 106 | + |
| 107 | + const result = await client.getFeatureVersions(1, 100); |
| 108 | + |
| 109 | + expect(mockFetch).toHaveBeenCalledWith(`${baseUrl}/environments/1/features/100/versions/`); |
| 110 | + expect(result).toEqual(versions); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('getFeatureStates', () => { |
| 115 | + it('fetches states', async () => { |
| 116 | + const states = [{ id: 1, enabled: true, feature_segment: null }]; |
| 117 | + mockFetch.mockResolvedValue(mockOk(states)); |
| 118 | + |
| 119 | + const result = await client.getFeatureStates(1, 100, 'uuid'); |
| 120 | + |
| 121 | + expect(mockFetch).toHaveBeenCalledWith(`${baseUrl}/environments/1/features/100/versions/uuid/featurestates/`); |
| 122 | + expect(result).toEqual(states); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('getFeatureDetails', () => { |
| 127 | + it('combines versions and states', async () => { |
| 128 | + const versions = [{ uuid: 'v1', is_live: true, published: true }]; |
| 129 | + const states = [ |
| 130 | + { id: 1, enabled: true, feature_segment: null }, |
| 131 | + { id: 2, enabled: true, feature_segment: { segment: 1, priority: 1 } }, |
| 132 | + ]; |
| 133 | + |
| 134 | + mockFetch |
| 135 | + .mockResolvedValueOnce(mockOk({ results: versions })) |
| 136 | + .mockResolvedValueOnce(mockOk(states)); |
| 137 | + |
| 138 | + const result = await client.getFeatureDetails(1, 100); |
| 139 | + |
| 140 | + expect(result.liveVersion).toEqual(versions[0]); |
| 141 | + expect(result.featureState).toEqual(states); |
| 142 | + expect(result.segmentOverrides).toBe(1); |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns nulls when no live version', async () => { |
| 146 | + mockFetch.mockResolvedValue(mockOk({ results: [] })); |
| 147 | + |
| 148 | + const result = await client.getFeatureDetails(1, 100); |
| 149 | + |
| 150 | + expect(result.liveVersion).toBeNull(); |
| 151 | + expect(result.featureState).toBeNull(); |
| 152 | + expect(result.segmentOverrides).toBe(0); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments