EmptyBucketModal.test.tsx96 lines · main
| 1 | import { faker } from '@faker-js/faker' |
| 2 | import { fireEvent, screen, waitFor } from '@testing-library/dom' |
| 3 | import userEvent from '@testing-library/user-event' |
| 4 | import { useState } from 'react' |
| 5 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | |
| 7 | import { EmptyBucketModal } from '../EmptyBucketModal' |
| 8 | import { ProjectContextProvider } from '@/components/layouts/ProjectLayout/ProjectContext' |
| 9 | import { Bucket } from '@/data/storage/buckets-query' |
| 10 | import { render } from '@/tests/helpers' |
| 11 | import { addAPIMock } from '@/tests/lib/msw' |
| 12 | import { routerMock } from '@/tests/lib/route-mock' |
| 13 | |
| 14 | const bucket: Bucket = { |
| 15 | id: faker.string.uuid(), |
| 16 | name: `test`, |
| 17 | owner: faker.string.uuid(), |
| 18 | public: faker.datatype.boolean(), |
| 19 | allowed_mime_types: faker.helpers.multiple(() => faker.system.mimeType(), { |
| 20 | count: { min: 1, max: 5 }, |
| 21 | }), |
| 22 | file_size_limit: faker.number.int({ min: 0, max: 25165824 }), |
| 23 | type: faker.helpers.arrayElement(['STANDARD', 'ANALYTICS', undefined]), |
| 24 | created_at: faker.date.recent().toISOString(), |
| 25 | updated_at: faker.date.recent().toISOString(), |
| 26 | } |
| 27 | |
| 28 | const Page = ({ onClose }: { onClose: () => void }) => { |
| 29 | const [open, setOpen] = useState(false) |
| 30 | return ( |
| 31 | <ProjectContextProvider projectRef="default"> |
| 32 | <button onClick={() => setOpen(true)}>Open</button> |
| 33 | |
| 34 | <EmptyBucketModal |
| 35 | visible={open} |
| 36 | bucket={bucket} |
| 37 | onClose={() => { |
| 38 | setOpen(false) |
| 39 | onClose() |
| 40 | }} |
| 41 | /> |
| 42 | </ProjectContextProvider> |
| 43 | ) |
| 44 | } |
| 45 | |
| 46 | describe(`EmptyBucketModal`, () => { |
| 47 | beforeEach(() => { |
| 48 | // useParams |
| 49 | routerMock.setCurrentUrl(`/project/default/storage/buckets/test`) |
| 50 | // useSelectedProject -> Project |
| 51 | addAPIMock({ |
| 52 | method: `get`, |
| 53 | path: `/platform/projects/:ref`, |
| 54 | // @ts-expect-error |
| 55 | response: { |
| 56 | cloud_provider: 'localhost', |
| 57 | id: 1, |
| 58 | inserted_at: '2021-08-02T06:40:40.646Z', |
| 59 | name: 'Default Project', |
| 60 | organization_id: 1, |
| 61 | ref: 'default', |
| 62 | region: 'local', |
| 63 | status: 'ACTIVE_HEALTHY', |
| 64 | }, |
| 65 | }) |
| 66 | // useBucketEmptyMutation |
| 67 | addAPIMock({ |
| 68 | method: `post`, |
| 69 | path: `/platform/storage/:ref/buckets/:id/empty`, |
| 70 | }) |
| 71 | // Called by useStorageExplorerStateSnapshot but seems |
| 72 | // to be unnecessary for successful test? |
| 73 | // |
| 74 | // useProjectSettingsV2Query -> ProjectSettings |
| 75 | // GET /platform/projects/:ref/settings |
| 76 | // useAPIKeysQuery -> APIKey[] |
| 77 | // GET /v1/projects/:ref/api-keys |
| 78 | // listBucketObjects -> ListBucketObjectsData |
| 79 | // POST /platform/storage/:ref/buckets/:id/objects/list |
| 80 | }) |
| 81 | |
| 82 | it(`renders a confirmation dialog`, async () => { |
| 83 | const onClose = vi.fn() |
| 84 | render(<Page onClose={onClose} />) |
| 85 | |
| 86 | const openButton = screen.getByRole(`button`, { name: `Open` }) |
| 87 | await userEvent.click(openButton) |
| 88 | await screen.findByRole(`dialog`) |
| 89 | |
| 90 | const confirmButton = screen.getByRole(`button`, { name: `Empty bucket` }) |
| 91 | |
| 92 | fireEvent.click(confirmButton) |
| 93 | |
| 94 | await waitFor(() => expect(onClose).toHaveBeenCalledOnce()) |
| 95 | }) |
| 96 | }) |