From 12a7a2aff2c77b88d4e339184bcefe3307a9b02f Mon Sep 17 00:00:00 2001 From: Georgi Parlakov Date: Sun, 20 Oct 2024 22:27:45 +0300 Subject: [PATCH] chore: e2e test for organizer edit and delete a file --- .../campaign-application-giver.spec.ts | 74 +++++++++++++++++++ .../CampaignApplicationsGrid.tsx | 3 +- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/e2e/tests/regression/campaign-application/campaign-application-giver.spec.ts b/e2e/tests/regression/campaign-application/campaign-application-giver.spec.ts index 1a916af60..558a0ab83 100644 --- a/e2e/tests/regression/campaign-application/campaign-application-giver.spec.ts +++ b/e2e/tests/regression/campaign-application/campaign-application-giver.spec.ts @@ -169,6 +169,41 @@ test.describe('Campaign application giver', () => { await expect(page.getByText(t.steps.application['campaign-end'].options.funds)).toBeVisible() await expect(page.getByText('goal')).toBeVisible() }) + + test('should see the edit campaign application and be able to delete a selected file ', async ({ + page, + baseURL, + }) => { + // arrange + await setupMeAndCampaignTypes(page) + await setupCampaignApplicationForEdit(page) + await page.goto(`${baseURL}/campaigns/application/1234`) + const t = await textLocalized().campaign.bg() + await page.getByRole('button', { name: t.cta.next }).click() + await page.getByRole('button', { name: t.cta.next }).click() + + // expect to see 2 files + await expect(page.getByText('1234.txt')).toBeVisible() + await expect(page.getByText('document.pdf')).toBeVisible() + + // act + // hit the delete button ... + await page.locator('li').filter({ hasText: '1234.txt' }).getByLabel('delete').click() + const [editCamAppReq, fileDeleteReq] = await Promise.all([ + // the edit request to edit the CamApp entity + page.waitForRequest((r) => r.method() === 'PATCH'), + // the delete request to remove one of the files + page.waitForRequest((r) => r.method() === 'DELETE'), + // ... and when submit + page.getByRole('button', { name: t.cta.submit }).click(), + ]) + + await expect(editCamAppReq.postDataJSON()).toBeDefined() + + const fileDelRes = await fileDeleteReq.response() + + await expect(fileDelRes?.json()).resolves.toEqual({ id: 'ok' }) + }) }) function defaultCampaignApplication() { @@ -232,3 +267,42 @@ async function setupMeAndCampaignTypes(page: Page) { }), ) } + +async function setupCampaignApplicationForEdit( + page: Page, + application: Partial> = {}, +) { + await page.route('*/**/api/v1/campaign-application/byId/*', (req) => + req.fulfill({ + json: { + ...defaultCampaignApplication(), + id: 'forEdit', + documents: [ + { filename: '1234.txt', id: '1234' }, + { filename: 'document.pdf', id: 'doc-id-123123' }, + ], + ...application, + }, + }), + ) + + // on submit at the end of edit this patch request needs to be sent + await page.route('*/**/api/v1/campaign-application/forEdit', (req) => + req.fulfill({ + json: { + ...defaultCampaignApplication(), + id: 'forEdit', + ...application, + }, + }), + ) + + // delete file successful + await page.route('*/**/api/v1/campaign-application/fileById/*', (req) => + req.fulfill({ + json: { + id: 'ok', + }, + }), + ) +} diff --git a/src/components/admin/campaign-applications/CampaignApplicationsGrid.tsx b/src/components/admin/campaign-applications/CampaignApplicationsGrid.tsx index 4d230a824..d6d3b3f78 100644 --- a/src/components/admin/campaign-applications/CampaignApplicationsGrid.tsx +++ b/src/components/admin/campaign-applications/CampaignApplicationsGrid.tsx @@ -115,7 +115,8 @@ export const useCampaignsList = () => { const { data, isLoading } = fetchMutation() return { - list: data?.sort((a, b) => b?.updatedAt?.localeCompare(a?.updatedAt ?? '') ?? 0), + // the data array is strict mode (sometimes) it throws a Readonly array error on the sort so create a shallow copy + list: [...(data ?? [])].sort((a, b) => b?.updatedAt?.localeCompare(a?.updatedAt ?? '') ?? 0), isLoading, } }