|
1 | 1 | import { ShellHooksProvider } from '@scality/module-federation'; |
2 | 2 | import { act, renderHook } from '@testing-library/react-hooks'; |
| 3 | +import { rest } from 'msw'; |
| 4 | +import { setupServer } from 'msw/node'; |
3 | 5 | import type { PropsWithChildren } from 'react'; |
4 | 6 | import { QueryClient } from 'react-query'; |
5 | 7 | import { QueryClientProvider } from '../../../../QueryClientProvider'; |
6 | 8 | import type { LocationTypeKey } from '../../../../types/config'; |
7 | 9 | import * as DSRProvider from '../../../DataServiceRoleProvider'; |
8 | | -import { mockShellAlerts, mockShellHooks, WrapperAsStorageManager } from '../../../utils/testUtil'; |
| 10 | +import { _ManagementContext } from '../../../ManagementProvider'; |
| 11 | +import { |
| 12 | + mockShellAlerts, |
| 13 | + mockShellHooks, |
| 14 | + TEST_API_BASE_URL, |
| 15 | + TEST_MANAGEMENT_CLIENT, |
| 16 | + WrapperAsStorageManager, |
| 17 | +} from '../../../utils/testUtil'; |
9 | 18 | import { MockedAccountsLocationsAdapter } from '../../adapters/accounts-locations/MockedAccountsLocationsAdapter'; |
10 | 19 | import { |
11 | 20 | ACCOUNT_OWN_METRICS, |
@@ -45,11 +54,23 @@ const queryClient = new QueryClient({ |
45 | 54 | }, |
46 | 55 | }, |
47 | 56 | }); |
| 57 | +const server = setupServer( |
| 58 | + rest.get(`${TEST_API_BASE_URL}/api/v1/instance/:instanceId/status`, (_req, res, ctx) => res(ctx.json({}))), |
| 59 | +); |
| 60 | + |
| 61 | +beforeAll(() => server.listen({ onUnhandledRequest: 'bypass' })); |
| 62 | +afterEach(() => server.resetHandlers()); |
| 63 | +afterAll(() => server.close()); |
| 64 | + |
48 | 65 | const Wrapper = ({ children }: PropsWithChildren<Record<string, never>>) => { |
49 | 66 | return ( |
50 | 67 | <WrapperAsStorageManager isStorageManager={true}> |
51 | 68 | <ShellHooksProvider shellHooks={mockShellHooks} shellAlerts={mockShellAlerts}> |
52 | | - <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 69 | + <QueryClientProvider client={queryClient}> |
| 70 | + <_ManagementContext.Provider value={{ managementClient: TEST_MANAGEMENT_CLIENT }}> |
| 71 | + {children} |
| 72 | + </_ManagementContext.Provider> |
| 73 | + </QueryClientProvider> |
53 | 74 | </ShellHooksProvider> |
54 | 75 | </WrapperAsStorageManager> |
55 | 76 | ); |
@@ -431,4 +452,61 @@ describe('useListLocationsForCurrentAccount', () => { |
431 | 452 | }; |
432 | 453 | expect(result.current).toStrictEqual(expectedRes); |
433 | 454 | }); |
| 455 | + |
| 456 | + it('shows a location backed only by a freshly created bucket, resolving the bucket location name to its id', async () => { |
| 457 | + // S |
| 458 | + jest.spyOn(DSRProvider, 'useCurrentAccount').mockReturnValue({ |
| 459 | + account: { |
| 460 | + id: 'account-id-bucket-only', |
| 461 | + Name: 'BucketOnly', |
| 462 | + Roles: [], |
| 463 | + CreationDate: DEFAULT_METRICS_MESURED_ON, |
| 464 | + CanonicalId: 'canonical-id-bucket-only', |
| 465 | + }, |
| 466 | + }); |
| 467 | + // The account has no location metrics yet, but owns a bucket on `us-east-1` |
| 468 | + // whose location id (95dbedf5-...) differs from its name. |
| 469 | + server.use( |
| 470 | + rest.get(`${TEST_API_BASE_URL}/api/v1/instance/:instanceId/status`, (_req, res, ctx) => |
| 471 | + res( |
| 472 | + ctx.json({ |
| 473 | + metrics: { |
| 474 | + 'item-counts': { |
| 475 | + bucketList: [ |
| 476 | + { |
| 477 | + name: 'freshly-created-bucket', |
| 478 | + location: 'us-east-1', |
| 479 | + ownerCanonicalId: 'canonical-id-bucket-only', |
| 480 | + }, |
| 481 | + ], |
| 482 | + }, |
| 483 | + }, |
| 484 | + }), |
| 485 | + ), |
| 486 | + ), |
| 487 | + ); |
| 488 | + |
| 489 | + const { result, waitFor } = setupAndRenderHook(); |
| 490 | + |
| 491 | + // E |
| 492 | + await waitFor(() => { |
| 493 | + return result.current.locations.status === 'success'; |
| 494 | + }); |
| 495 | + |
| 496 | + // V |
| 497 | + if (result.current.locations.status !== 'success') { |
| 498 | + throw new Error('expected locations to be successfully loaded'); |
| 499 | + } |
| 500 | + const value = result.current.locations.value; |
| 501 | + // Keyed by the location id (objectId), not by the bucket location name. |
| 502 | + expect(Object.keys(value)).toEqual(['95dbedf5-9888-11ec-8565-1ac2af7d1e53']); |
| 503 | + const usEast1 = value['95dbedf5-9888-11ec-8565-1ac2af7d1e53']; |
| 504 | + expect(usEast1.name).toBe('us-east-1'); |
| 505 | + expect(usEast1.usedCapacity.status).toBe('success'); |
| 506 | + // A bucket-only location gets synthesized zero-usage capacity. |
| 507 | + expect(usEast1.usedCapacity).toMatchObject({ |
| 508 | + status: 'success', |
| 509 | + value: { type: 'hasMetrics', usedCapacity: { current: 0, nonCurrent: 0 } }, |
| 510 | + }); |
| 511 | + }); |
434 | 512 | }); |
0 commit comments