-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseFlagsmithProject.test.tsx
More file actions
58 lines (47 loc) · 2.22 KB
/
useFlagsmithProject.test.tsx
File metadata and controls
58 lines (47 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { ReactNode } from 'react';
import { renderHook, waitFor } from '@testing-library/react';
import { TestApiProvider } from '@backstage/test-utils';
import { discoveryApiRef, fetchApiRef } from '@backstage/core-plugin-api';
import { useFlagsmithProject } from './useFlagsmithProject';
describe('useFlagsmithProject', () => {
const mockFetch = jest.fn();
const mockDiscoveryApi = {
getBaseUrl: jest.fn().mockResolvedValue('http://localhost:7007/api/proxy'),
};
const wrapper = ({ children }: { children: ReactNode }) => (
<TestApiProvider
apis={[
[discoveryApiRef, mockDiscoveryApi],
[fetchApiRef, { fetch: mockFetch }],
]}
>
{children}
</TestApiProvider>
);
beforeEach(() => {
jest.clearAllMocks();
});
it('returns error when projectId is missing', async () => {
const { result } = renderHook(() => useFlagsmithProject(undefined), { wrapper });
await waitFor(() => expect(result.current.loading).toBe(false));
expect(result.current.error).toBe('No Flagsmith project ID found in entity annotations');
});
it('fetches project data successfully', async () => {
const mockProject = { id: 123, name: 'Test', organisation: 1 };
const mockEnvs = [{ id: 1, name: 'Dev', api_key: 'key', project: 123 }];
const mockFeatures = [{ id: 1, name: 'flag', created_date: '2024-01-01', project: 123, tags: [1] }];
const mockTags = [{ id: 1, label: 'ui', color: '#2196F3' }];
mockFetch
.mockResolvedValueOnce({ ok: true, json: async () => mockProject })
.mockResolvedValueOnce({ ok: true, json: async () => ({ results: mockEnvs }) })
.mockResolvedValueOnce({ ok: true, json: async () => ({ results: mockFeatures }) })
.mockResolvedValueOnce({ ok: true, json: async () => ({ results: mockTags }) });
const { result } = renderHook(() => useFlagsmithProject('123'), { wrapper });
await waitFor(() => expect(result.current.loading).toBe(false));
expect(result.current.error).toBeNull();
expect(result.current.project).toEqual(mockProject);
expect(result.current.environments).toEqual(mockEnvs);
expect(result.current.features).toEqual(mockFeatures);
expect(result.current.tagMap.get(1)).toEqual(mockTags[0]);
});
});