|
4 | 4 | getSearchWithEnforcedSegment, |
5 | 5 | isSearchEntryDefined, |
6 | 6 | maybeGetLatestReadableSearch, |
| 7 | + maybeReloadForApiVersion, |
7 | 8 | parseFilter, |
8 | 9 | parseLabelsEntry, |
9 | 10 | parseSearch, |
@@ -259,3 +260,64 @@ describe(`${getSearchWithEnforcedSegment.name}`, () => { |
259 | 260 | ).toEqual(expectedUpdatedSearch) |
260 | 261 | }) |
261 | 262 | }) |
| 263 | + |
| 264 | +describe(`${maybeReloadForApiVersion.name}`, () => { |
| 265 | + const dashboardPathname = '/example.com' |
| 266 | + |
| 267 | + type MockWindowLocation = Location & { replace: jest.Mock } |
| 268 | + |
| 269 | + function makeLocation(search: string): MockWindowLocation { |
| 270 | + return { |
| 271 | + pathname: dashboardPathname, |
| 272 | + search, |
| 273 | + hash: '', |
| 274 | + replace: jest.fn() |
| 275 | + } as unknown as MockWindowLocation |
| 276 | + } |
| 277 | + |
| 278 | + function makeHeaders(version: string | null): Headers { |
| 279 | + const headers = new Headers() |
| 280 | + if (version !== null) headers.set('x-api-version', version) |
| 281 | + return headers |
| 282 | + } |
| 283 | + |
| 284 | + it('reloads when effective API version is greater than expected', () => { |
| 285 | + const location = makeLocation('') |
| 286 | + maybeReloadForApiVersion(location, makeHeaders('1')) |
| 287 | + expect(location.replace).toHaveBeenCalledWith( |
| 288 | + `${dashboardPathname}?api_version_reloaded=1` |
| 289 | + ) |
| 290 | + }) |
| 291 | + |
| 292 | + it('does not reload when effective API version equals expected', () => { |
| 293 | + const location = makeLocation('') |
| 294 | + maybeReloadForApiVersion(location, makeHeaders('0')) |
| 295 | + expect(location.replace).not.toHaveBeenCalled() |
| 296 | + }) |
| 297 | + |
| 298 | + it('does not reload when effective API version is less than expected (FE loaded from newer node, cluster not fully updated)', () => { |
| 299 | + const location = makeLocation('') |
| 300 | + maybeReloadForApiVersion(location, makeHeaders('-1')) |
| 301 | + expect(location.replace).not.toHaveBeenCalled() |
| 302 | + }) |
| 303 | + |
| 304 | + it('does not reload when x-api-version header is absent', () => { |
| 305 | + const location = makeLocation('') |
| 306 | + maybeReloadForApiVersion(location, makeHeaders(null)) |
| 307 | + expect(location.replace).not.toHaveBeenCalled() |
| 308 | + }) |
| 309 | + |
| 310 | + it('does not reload when already reloaded for this version', () => { |
| 311 | + const location = makeLocation('?api_version_reloaded=1') |
| 312 | + maybeReloadForApiVersion(location, makeHeaders('1')) |
| 313 | + expect(location.replace).not.toHaveBeenCalled() |
| 314 | + }) |
| 315 | + |
| 316 | + it('reloads again if a newer version is detected after a previous reload', () => { |
| 317 | + const location = makeLocation('?api_version_reloaded=1') |
| 318 | + maybeReloadForApiVersion(location, makeHeaders('2')) |
| 319 | + expect(location.replace).toHaveBeenCalledWith( |
| 320 | + `${dashboardPathname}?api_version_reloaded=2` |
| 321 | + ) |
| 322 | + }) |
| 323 | +}) |
0 commit comments