Skip to content

Commit c5a9827

Browse files
committed
CCM-11148 Improve test coverage
1 parent d3ab8f8 commit c5a9827

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

frontend/src/__tests__/app/internal/features/route.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getSessionServer } from '@utils/amplify-utils';
33
import { fetchClient } from '@utils/server-features';
44
import { FEATURES, initialFeatureFlags } from '@utils/features';
55
import { NextRequest, NextResponse } from 'next/server';
6-
import { ErrorCase } from 'nhs-notify-backend-client';
6+
import { ErrorCase, Result } from 'nhs-notify-backend-client';
77

88
jest.mock('@utils/amplify-utils');
99
jest.mock('@utils/server-features');
@@ -45,6 +45,7 @@ describe('features route', () => {
4545
});
4646

4747
it('returns initialFeatureFlags if no access token', async () => {
48+
mockGetSessionServer.mockReset();
4849
mockGetSessionServer.mockResolvedValueOnce({});
4950

5051
const req = createRequest();
@@ -81,4 +82,14 @@ describe('features route', () => {
8182

8283
expect(json).toEqual(allFeaturesEnabled);
8384
});
85+
86+
it('returns initialFeatureFlags if fetchClient returns undefined', async () => {
87+
mockFetchClient.mockResolvedValueOnce({} as Result<null>);
88+
89+
const req = createRequest();
90+
const res = await getFeatures(req);
91+
const json = await res.json();
92+
93+
expect(json).toEqual(initialFeatureFlags);
94+
});
8495
});

frontend/src/__tests__/components/providers/features-provider.test.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,34 @@ describe('FeatureFlagProvider', () => {
8585
expect(screen.getByTestId('routing')).toHaveTextContent('false');
8686
});
8787
});
88+
89+
it('should fall back to default flags when response.ok is false', async () => {
90+
(global.fetch as jest.Mock).mockResolvedValue({
91+
ok: false,
92+
json: async () => ({ proofing: true, routing: true }),
93+
});
94+
95+
renderWithProvider();
96+
97+
await waitFor(() => {
98+
expect(screen.getByTestId('loaded')).toHaveTextContent('true');
99+
expect(screen.getByTestId('proofing')).toHaveTextContent('false');
100+
expect(screen.getByTestId('routing')).toHaveTextContent('false');
101+
});
102+
});
103+
});
104+
105+
it('should not fetch feature flags when authStatus is neither authenticated or unauthenticated', async () => {
106+
mockUseAuthStatus.mockReturnValue('configuring');
107+
108+
renderWithProvider();
109+
110+
await waitFor(() => {
111+
expect(screen.getByTestId('loaded')).toHaveTextContent('false');
112+
expect(screen.getByTestId('proofing')).toHaveTextContent('false');
113+
expect(screen.getByTestId('routing')).toHaveTextContent('false');
114+
});
115+
116+
expect(global.fetch).not.toHaveBeenCalled();
88117
});
89118
});

frontend/src/components/providers/features-provider.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function FeatureFlagProvider({
4646

4747
if (!response.ok) {
4848
setFeatureFlags(initialFeatureFlags);
49+
return;
4950
}
5051

5152
const data: ClientFeatures = await response.json();

0 commit comments

Comments
 (0)