|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import fetchMock from 'fetch-mock'; |
| 20 | +import { screen, waitFor } from 'spec/helpers/testing-library'; |
| 21 | +import { |
| 22 | + API_ENDPOINTS, |
| 23 | + renderChartList, |
| 24 | + setupMocks, |
| 25 | +} from './ChartList.testHelpers'; |
| 26 | + |
| 27 | +jest.setTimeout(30000); |
| 28 | + |
| 29 | +const mockUser = { |
| 30 | + userId: 1, |
| 31 | + firstName: 'Test', |
| 32 | + lastName: 'User', |
| 33 | + roles: { |
| 34 | + Admin: [ |
| 35 | + ['can_read', 'Chart'], |
| 36 | + ['can_write', 'Chart'], |
| 37 | + ], |
| 38 | + }, |
| 39 | +}; |
| 40 | + |
| 41 | +describe('ChartList - Favorite Column Visibility', () => { |
| 42 | + beforeEach(() => { |
| 43 | + setupMocks(); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + fetchMock.resetHistory(); |
| 48 | + fetchMock.restore(); |
| 49 | + }); |
| 50 | + |
| 51 | + test('hides favorite column when no charts are favorited', async () => { |
| 52 | + // Mock favorite status API to return all false |
| 53 | + fetchMock.get( |
| 54 | + API_ENDPOINTS.CHART_FAVORITE_STATUS, |
| 55 | + { |
| 56 | + result: [ |
| 57 | + { id: 0, value: false }, |
| 58 | + { id: 1, value: false }, |
| 59 | + { id: 2, value: false }, |
| 60 | + { id: 3, value: false }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + { overwriteRoutes: true }, |
| 64 | + ); |
| 65 | + |
| 66 | + renderChartList(mockUser); |
| 67 | + |
| 68 | + await waitFor(() => { |
| 69 | + expect(screen.getByTestId('chart-list-view')).toBeInTheDocument(); |
| 70 | + }); |
| 71 | + |
| 72 | + // Wait for data to load |
| 73 | + await waitFor(() => { |
| 74 | + expect(screen.getByText('Test Chart 0')).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + // Favorite column should be hidden - check that favorite stars are not present |
| 78 | + const favoriteStars = screen.queryAllByTestId('fave-unfave-icon'); |
| 79 | + expect(favoriteStars).toHaveLength(0); |
| 80 | + |
| 81 | + // Verify that other columns are still present (check table headers) |
| 82 | + expect( |
| 83 | + screen.getByRole('columnheader', { name: 'Name' }), |
| 84 | + ).toBeInTheDocument(); |
| 85 | + expect( |
| 86 | + screen.getByRole('columnheader', { name: 'Type' }), |
| 87 | + ).toBeInTheDocument(); |
| 88 | + }); |
| 89 | + |
| 90 | + test('shows favorite column when at least one chart is favorited', async () => { |
| 91 | + // Mock favorite status API to return mixed favorites |
| 92 | + fetchMock.get( |
| 93 | + API_ENDPOINTS.CHART_FAVORITE_STATUS, |
| 94 | + { |
| 95 | + result: [ |
| 96 | + { id: 0, value: true }, // This chart is favorited |
| 97 | + { id: 1, value: false }, |
| 98 | + { id: 2, value: false }, |
| 99 | + { id: 3, value: false }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + { overwriteRoutes: true }, |
| 103 | + ); |
| 104 | + |
| 105 | + renderChartList(mockUser); |
| 106 | + |
| 107 | + await waitFor(() => { |
| 108 | + expect(screen.getByTestId('chart-list-view')).toBeInTheDocument(); |
| 109 | + }); |
| 110 | + |
| 111 | + // Wait for data to load |
| 112 | + await waitFor(() => { |
| 113 | + expect(screen.getByText('Test Chart 0')).toBeInTheDocument(); |
| 114 | + }); |
| 115 | + |
| 116 | + // Favorite column should be visible - wait for stars to appear |
| 117 | + await waitFor( |
| 118 | + () => { |
| 119 | + const favoriteStars = screen.getAllByTestId('fave-unfave-icon'); |
| 120 | + expect(favoriteStars.length).toBeGreaterThan(0); |
| 121 | + }, |
| 122 | + { timeout: 10000 }, |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + test('shows favorite column when all charts are favorited', async () => { |
| 127 | + // Mock favorite status API to return all true |
| 128 | + fetchMock.get( |
| 129 | + API_ENDPOINTS.CHART_FAVORITE_STATUS, |
| 130 | + { |
| 131 | + result: [ |
| 132 | + { id: 0, value: true }, |
| 133 | + { id: 1, value: true }, |
| 134 | + { id: 2, value: true }, |
| 135 | + { id: 3, value: true }, |
| 136 | + ], |
| 137 | + }, |
| 138 | + { overwriteRoutes: true }, |
| 139 | + ); |
| 140 | + |
| 141 | + renderChartList(mockUser); |
| 142 | + |
| 143 | + await waitFor(() => { |
| 144 | + expect(screen.getByTestId('chart-list-view')).toBeInTheDocument(); |
| 145 | + }); |
| 146 | + |
| 147 | + // Wait for data to load |
| 148 | + await waitFor(() => { |
| 149 | + expect(screen.getByText('Test Chart 0')).toBeInTheDocument(); |
| 150 | + }); |
| 151 | + |
| 152 | + // Favorite column should be visible |
| 153 | + await waitFor( |
| 154 | + () => { |
| 155 | + const favoriteStars = screen.getAllByTestId('fave-unfave-icon'); |
| 156 | + expect(favoriteStars.length).toBeGreaterThan(0); |
| 157 | + }, |
| 158 | + { timeout: 10000 }, |
| 159 | + ); |
| 160 | + }); |
| 161 | + |
| 162 | + test('hides favorite column when user is not logged in', async () => { |
| 163 | + // Mock favorite status API |
| 164 | + fetchMock.get( |
| 165 | + API_ENDPOINTS.CHART_FAVORITE_STATUS, |
| 166 | + { |
| 167 | + result: [ |
| 168 | + { id: 0, value: true }, |
| 169 | + { id: 1, value: false }, |
| 170 | + ], |
| 171 | + }, |
| 172 | + { overwriteRoutes: true }, |
| 173 | + ); |
| 174 | + |
| 175 | + // Render without userId (user not logged in) |
| 176 | + const noUser = { |
| 177 | + userId: null, |
| 178 | + firstName: '', |
| 179 | + lastName: '', |
| 180 | + roles: {}, |
| 181 | + }; |
| 182 | + |
| 183 | + renderChartList(noUser); |
| 184 | + |
| 185 | + await waitFor(() => { |
| 186 | + expect(screen.getByTestId('chart-list-view')).toBeInTheDocument(); |
| 187 | + }); |
| 188 | + |
| 189 | + // Wait for data to load |
| 190 | + await waitFor(() => { |
| 191 | + expect(screen.getByText('Test Chart 0')).toBeInTheDocument(); |
| 192 | + }); |
| 193 | + |
| 194 | + // Favorite column should be hidden when user is not logged in |
| 195 | + const favoriteStars = screen.queryAllByTestId('fave-unfave-icon'); |
| 196 | + expect(favoriteStars).toHaveLength(0); |
| 197 | + }); |
| 198 | + |
| 199 | + test('hides favorite column when chart list is empty', async () => { |
| 200 | + // Mock empty charts response |
| 201 | + fetchMock.get( |
| 202 | + API_ENDPOINTS.CHARTS, |
| 203 | + { |
| 204 | + result: [], |
| 205 | + chart_count: 0, |
| 206 | + }, |
| 207 | + { overwriteRoutes: true }, |
| 208 | + ); |
| 209 | + |
| 210 | + // Mock empty favorite status |
| 211 | + fetchMock.get( |
| 212 | + API_ENDPOINTS.CHART_FAVORITE_STATUS, |
| 213 | + { |
| 214 | + result: [], |
| 215 | + }, |
| 216 | + { overwriteRoutes: true }, |
| 217 | + ); |
| 218 | + |
| 219 | + renderChartList(mockUser); |
| 220 | + |
| 221 | + await waitFor(() => { |
| 222 | + expect(screen.getByTestId('chart-list-view')).toBeInTheDocument(); |
| 223 | + }); |
| 224 | + |
| 225 | + // No favorite stars should be present when there are no charts |
| 226 | + const favoriteStars = screen.queryAllByTestId('fave-unfave-icon'); |
| 227 | + expect(favoriteStars).toHaveLength(0); |
| 228 | + }); |
| 229 | + |
| 230 | + test('handles partial favorite status loading gracefully', async () => { |
| 231 | + // Mock partial favorite status (fewer items than charts) |
| 232 | + fetchMock.get( |
| 233 | + API_ENDPOINTS.CHART_FAVORITE_STATUS, |
| 234 | + { |
| 235 | + result: [ |
| 236 | + { id: 0, value: false }, |
| 237 | + { id: 1, value: false }, |
| 238 | + // Missing status for charts 2 and 3 |
| 239 | + ], |
| 240 | + }, |
| 241 | + { overwriteRoutes: true }, |
| 242 | + ); |
| 243 | + |
| 244 | + renderChartList(mockUser); |
| 245 | + |
| 246 | + await waitFor(() => { |
| 247 | + expect(screen.getByTestId('chart-list-view')).toBeInTheDocument(); |
| 248 | + }); |
| 249 | + |
| 250 | + // Wait for data to load |
| 251 | + await waitFor(() => { |
| 252 | + expect(screen.getByText('Test Chart 0')).toBeInTheDocument(); |
| 253 | + }); |
| 254 | + |
| 255 | + // Should hide column when favorite status is incomplete |
| 256 | + const favoriteStars = screen.queryAllByTestId('fave-unfave-icon'); |
| 257 | + expect(favoriteStars).toHaveLength(0); |
| 258 | + }); |
| 259 | +}); |
0 commit comments