|
| 1 | + |
| 2 | +import { screen, waitFor, fireEvent } from '@testing-library/react'; |
| 3 | +import { FlagsmithOverviewCard } from '../index'; |
| 4 | +import { |
| 5 | + renderWithBackstage, |
| 6 | + mockProject, |
| 7 | + mockEnvironments, |
| 8 | + mockFeatures, |
| 9 | + mockEntityNoAnnotations, |
| 10 | +} from '../../../__tests__/fixtures'; |
| 11 | + |
| 12 | +describe('FlagsmithOverviewCard', () => { |
| 13 | + const defaultFetchResponses = { |
| 14 | + '/projects/123/': mockProject, |
| 15 | + '/projects/123/environments/': { results: mockEnvironments }, |
| 16 | + '/projects/123/features/': { results: mockFeatures }, |
| 17 | + }; |
| 18 | + |
| 19 | + it('shows loading state initially', () => { |
| 20 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 21 | + fetchResponses: defaultFetchResponses, |
| 22 | + }); |
| 23 | + |
| 24 | + expect(screen.getByRole('status')).toBeInTheDocument(); |
| 25 | + expect(screen.getByText('Loading Flagsmith data...')).toBeInTheDocument(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('displays feature flag stats after loading', async () => { |
| 29 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 30 | + fetchResponses: defaultFetchResponses, |
| 31 | + }); |
| 32 | + |
| 33 | + await waitFor(() => { |
| 34 | + expect(screen.queryByText('Loading Flagsmith data...')).not.toBeInTheDocument(); |
| 35 | + }); |
| 36 | + |
| 37 | + // Should show total flags |
| 38 | + expect(screen.getByText('3')).toBeInTheDocument(); // Total flags |
| 39 | + expect(screen.getByText('Total Flags')).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('displays enabled/disabled counts', async () => { |
| 43 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 44 | + fetchResponses: defaultFetchResponses, |
| 45 | + }); |
| 46 | + |
| 47 | + await waitFor(() => { |
| 48 | + expect(screen.getByText('Total Flags')).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + // mockFeatures: 2 enabled (feature-one, feature-three), 1 disabled (feature-two) |
| 52 | + expect(screen.getByText('2')).toBeInTheDocument(); // Enabled count |
| 53 | + expect(screen.getByText('1')).toBeInTheDocument(); // Disabled count |
| 54 | + expect(screen.getByText('Enabled')).toBeInTheDocument(); |
| 55 | + expect(screen.getByText('Disabled')).toBeInTheDocument(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('displays error when project ID is missing', async () => { |
| 59 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 60 | + entity: mockEntityNoAnnotations, |
| 61 | + }); |
| 62 | + |
| 63 | + await waitFor(() => { |
| 64 | + expect(screen.queryByText('Loading Flagsmith data...')).not.toBeInTheDocument(); |
| 65 | + }); |
| 66 | + |
| 67 | + expect(screen.getByText(/No Flagsmith project ID found/)).toBeInTheDocument(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('displays error on API failure', async () => { |
| 71 | + const fetchApi = { |
| 72 | + fetch: jest.fn().mockResolvedValue({ |
| 73 | + ok: false, |
| 74 | + statusText: 'Forbidden', |
| 75 | + }), |
| 76 | + }; |
| 77 | + |
| 78 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 79 | + fetchApi, |
| 80 | + }); |
| 81 | + |
| 82 | + await waitFor(() => { |
| 83 | + expect(screen.queryByText('Loading Flagsmith data...')).not.toBeInTheDocument(); |
| 84 | + }); |
| 85 | + |
| 86 | + expect(screen.getByText(/Error:/)).toBeInTheDocument(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('displays feature list', async () => { |
| 90 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 91 | + fetchResponses: defaultFetchResponses, |
| 92 | + }); |
| 93 | + |
| 94 | + await waitFor(() => { |
| 95 | + expect(screen.getByText('feature-one')).toBeInTheDocument(); |
| 96 | + }); |
| 97 | + |
| 98 | + // At least some features should be visible |
| 99 | + expect(screen.getByText('feature-one')).toBeInTheDocument(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('renders card with title', async () => { |
| 103 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 104 | + fetchResponses: defaultFetchResponses, |
| 105 | + }); |
| 106 | + |
| 107 | + expect(screen.getByText('Flagsmith Feature Flags')).toBeInTheDocument(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('renders dashboard link', async () => { |
| 111 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 112 | + fetchResponses: defaultFetchResponses, |
| 113 | + }); |
| 114 | + |
| 115 | + await waitFor(() => { |
| 116 | + expect(screen.getByText('feature-one')).toBeInTheDocument(); |
| 117 | + }); |
| 118 | + |
| 119 | + const dashboardButton = screen.getByRole('button', { name: /open dashboard/i }); |
| 120 | + expect(dashboardButton).toBeInTheDocument(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('shows environments count', async () => { |
| 124 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 125 | + fetchResponses: defaultFetchResponses, |
| 126 | + }); |
| 127 | + |
| 128 | + await waitFor(() => { |
| 129 | + expect(screen.getByText('Total Flags')).toBeInTheDocument(); |
| 130 | + }); |
| 131 | + |
| 132 | + // Should show number of environments |
| 133 | + expect(screen.getByText('Environments')).toBeInTheDocument(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('handles many features with pagination', async () => { |
| 137 | + // Create more than 5 features to trigger pagination |
| 138 | + const manyFeatures = Array.from({ length: 10 }, (_, i) => ({ |
| 139 | + id: i + 1, |
| 140 | + name: `feature-${i + 1}`, |
| 141 | + description: `Feature ${i + 1}`, |
| 142 | + created_date: '2024-01-01T00:00:00Z', |
| 143 | + project: 123, |
| 144 | + default_enabled: i % 2 === 0, |
| 145 | + })); |
| 146 | + |
| 147 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 148 | + fetchResponses: { |
| 149 | + ...defaultFetchResponses, |
| 150 | + '/projects/123/features/': { results: manyFeatures }, |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + await waitFor(() => { |
| 155 | + expect(screen.getByText('feature-1')).toBeInTheDocument(); |
| 156 | + }); |
| 157 | + |
| 158 | + // Should show pagination |
| 159 | + expect(screen.getByRole('navigation', { name: /pagination/i })).toBeInTheDocument(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('navigates pages when pagination is clicked', async () => { |
| 163 | + const manyFeatures = Array.from({ length: 10 }, (_, i) => ({ |
| 164 | + id: i + 1, |
| 165 | + name: `feature-${i + 1}`, |
| 166 | + description: `Feature ${i + 1}`, |
| 167 | + created_date: '2024-01-01T00:00:00Z', |
| 168 | + project: 123, |
| 169 | + default_enabled: true, |
| 170 | + })); |
| 171 | + |
| 172 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 173 | + fetchResponses: { |
| 174 | + ...defaultFetchResponses, |
| 175 | + '/projects/123/features/': { results: manyFeatures }, |
| 176 | + }, |
| 177 | + }); |
| 178 | + |
| 179 | + await waitFor(() => { |
| 180 | + expect(screen.getByText('feature-1')).toBeInTheDocument(); |
| 181 | + }); |
| 182 | + |
| 183 | + // Click next page |
| 184 | + const nextButton = screen.getByRole('button', { name: /next page/i }); |
| 185 | + fireEvent.click(nextButton); |
| 186 | + |
| 187 | + // Should show next page features |
| 188 | + await waitFor(() => { |
| 189 | + expect(screen.getByText('feature-6')).toBeInTheDocument(); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + it('handles empty features list', async () => { |
| 194 | + renderWithBackstage(<FlagsmithOverviewCard />, { |
| 195 | + fetchResponses: { |
| 196 | + ...defaultFetchResponses, |
| 197 | + '/projects/123/features/': { results: [] }, |
| 198 | + }, |
| 199 | + }); |
| 200 | + |
| 201 | + await waitFor(() => { |
| 202 | + expect(screen.queryByText('Loading Flagsmith data...')).not.toBeInTheDocument(); |
| 203 | + }); |
| 204 | + |
| 205 | + // Should show 0 total flags |
| 206 | + expect(screen.getByText('0')).toBeInTheDocument(); |
| 207 | + }); |
| 208 | +}); |
0 commit comments