|
1 | | -import { render, waitFor } from '@testing-library/react'; |
| 1 | +import { render, waitFor, screen } from '@testing-library/react'; |
2 | 2 | import { |
3 | 3 | FeatureFlagProvider, |
4 | 4 | useFeatureFlags, |
5 | 5 | } from '@providers/features-provider'; |
6 | 6 | import { Authenticator } from '@aws-amplify/ui-react'; |
| 7 | +import * as useAuthStatusHook from '@hooks/use-auth-status'; |
7 | 8 |
|
8 | | -const TestConsumer = () => { |
9 | | - const { featureFlags, loaded } = useFeatureFlags(); |
10 | | - return ( |
11 | | - <div> |
12 | | - <p data-testid='loaded'> {loaded ? 'true' : 'false'}</p> |
13 | | - <p data-testid='proofing'>{featureFlags.proofing ? 'true' : 'false'}</p> |
14 | | - <p data-testid='routing'>{featureFlags.routing ? 'true' : 'false'}</p> |
15 | | - </div> |
16 | | - ); |
17 | | -}; |
18 | | - |
19 | | -const renderProvider = () => { |
20 | | - return render( |
21 | | - <Authenticator.Provider> |
22 | | - <FeatureFlagProvider> |
23 | | - <TestConsumer /> |
24 | | - </FeatureFlagProvider> |
25 | | - </Authenticator.Provider> |
26 | | - ); |
27 | | -}; |
| 9 | +jest.mock('@hooks/use-auth-status'); |
| 10 | + |
| 11 | +global.fetch = jest.fn(); |
| 12 | + |
| 13 | +const mockUseAuthStatus = useAuthStatusHook.useAuthStatus as jest.Mock; |
28 | 14 |
|
29 | 15 | describe('FeatureFlagProvider', () => { |
| 16 | + const TestComponent = () => { |
| 17 | + const { featureFlags, loaded } = useFeatureFlags(); |
| 18 | + |
| 19 | + return ( |
| 20 | + <div> |
| 21 | + <p data-testid='loaded'> {loaded ? 'true' : 'false'}</p> |
| 22 | + <p data-testid='proofing'>{featureFlags.proofing ? 'true' : 'false'}</p> |
| 23 | + <p data-testid='routing'>{featureFlags.routing ? 'true' : 'false'}</p> |
| 24 | + </div> |
| 25 | + ); |
| 26 | + }; |
| 27 | + |
| 28 | + const renderWithProvider = () => |
| 29 | + render( |
| 30 | + <Authenticator.Provider> |
| 31 | + <FeatureFlagProvider> |
| 32 | + <TestComponent /> |
| 33 | + </FeatureFlagProvider> |
| 34 | + </Authenticator.Provider> |
| 35 | + ); |
| 36 | + |
30 | 37 | beforeEach(() => { |
31 | 38 | jest.resetAllMocks(); |
32 | 39 | }); |
33 | 40 |
|
34 | 41 | describe('when unauthenticated', () => { |
35 | 42 | beforeEach(() => { |
36 | | - jest.mock('@hooks/use-auth-status', () => ({ |
37 | | - useAuthStatus: () => 'unauthenticated', |
38 | | - })); |
| 43 | + mockUseAuthStatus.mockReturnValue('unauthenticated'); |
39 | 44 | }); |
40 | 45 |
|
41 | 46 | it('should provide all feature flags as false when unauthenticated', async () => { |
42 | | - const container = renderProvider(); |
| 47 | + renderWithProvider(); |
43 | 48 |
|
44 | 49 | await waitFor(() => { |
45 | | - expect(container.getByTestId('loaded')).toHaveTextContent('false'); |
46 | | - expect(container.getByTestId('proofing')).toHaveTextContent('false'); |
47 | | - expect(container.getByTestId('routing')).toHaveTextContent('false'); |
| 50 | + expect(screen.getByTestId('loaded')).toHaveTextContent('false'); // loading should remain false |
| 51 | + expect(screen.getByTestId('proofing')).toHaveTextContent('false'); |
| 52 | + expect(screen.getByTestId('routing')).toHaveTextContent('false'); |
48 | 53 | }); |
49 | 54 | }); |
50 | 55 | }); |
51 | 56 |
|
52 | 57 | describe('when authenticated', () => { |
53 | 58 | beforeEach(() => { |
54 | | - jest.mock('@hooks/use-auth-status', () => ({ |
55 | | - useAuthStatus: () => 'authenticated', |
56 | | - })); |
| 59 | + mockUseAuthStatus.mockReturnValue('authenticated'); |
57 | 60 | }); |
58 | 61 |
|
59 | 62 | it('should provide feature flags when authenticated', async () => { |
60 | | - global.fetch = jest.fn().mockResolvedValue({ |
| 63 | + (global.fetch as jest.Mock).mockResolvedValue({ |
61 | 64 | ok: true, |
62 | 65 | json: async () => ({ proofing: true, routing: true }), |
63 | 66 | }); |
64 | 67 |
|
65 | | - const container = renderProvider(); |
| 68 | + renderWithProvider(); |
66 | 69 |
|
67 | 70 | await waitFor(() => { |
68 | | - expect(container.getByTestId('loaded')).toHaveTextContent('true'); |
69 | | - expect(container.getByTestId('proofing')).toHaveTextContent('true'); |
70 | | - expect(container.getByTestId('routing')).toHaveTextContent('true'); |
| 71 | + expect(screen.getByTestId('loaded')).toHaveTextContent('true'); |
| 72 | + expect(screen.getByTestId('proofing')).toHaveTextContent('true'); |
| 73 | + expect(screen.getByTestId('routing')).toHaveTextContent('true'); |
71 | 74 | }); |
72 | 75 | }); |
73 | | - }); |
74 | 76 |
|
75 | | - it('should fall back to default flags on fetch error', async () => { |
76 | | - global.fetch = jest.fn().mockRejectedValue(new Error('Failed')); |
| 77 | + it('should fall back to default flags on fetch error', async () => { |
| 78 | + (global.fetch as jest.Mock).mockRejectedValue(new Error('Fetch failed')); |
77 | 79 |
|
78 | | - const container = renderProvider(); |
| 80 | + renderWithProvider(); |
79 | 81 |
|
80 | | - await waitFor(() => { |
81 | | - expect(container.getByTestId('loaded')).toHaveTextContent('true'); |
82 | | - expect(container.getByTestId('proofing')).toHaveTextContent('false'); |
83 | | - expect(container.getByTestId('routing')).toHaveTextContent('false'); |
| 82 | + await waitFor(() => { |
| 83 | + expect(screen.getByTestId('loaded')).toHaveTextContent('true'); |
| 84 | + expect(screen.getByTestId('proofing')).toHaveTextContent('false'); |
| 85 | + expect(screen.getByTestId('routing')).toHaveTextContent('false'); |
| 86 | + }); |
84 | 87 | }); |
85 | 88 | }); |
86 | 89 | }); |
0 commit comments