From 0100ecd69935b4e7e3a1027dedc5a1645d0dc0c4 Mon Sep 17 00:00:00 2001 From: alexeh Date: Thu, 9 Jan 2025 11:06:12 +0100 Subject: [PATCH] fixed unintentional removal of countryCode filter, handling an empty ("") initial countryCode filter --- client/src/containers/overview/table/utils.ts | 11 +++-- .../projects/projects-overview-table.spec.ts | 41 +++++++++++++++++++ shared/lib/e2e-test-manager.ts | 17 +++++++- 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 e2e/tests/projects/projects-overview-table.spec.ts diff --git a/client/src/containers/overview/table/utils.ts b/client/src/containers/overview/table/utils.ts index 296ff8d1..28c74968 100644 --- a/client/src/containers/overview/table/utils.ts +++ b/client/src/containers/overview/table/utils.ts @@ -11,19 +11,22 @@ const OMITTED_FIELDS = [ "costRange", "abatementPotentialRange", "costRangeSelector", + "priceType", ]; export const filtersToQueryParams = ( filters: z.infer, ) => { return Object.keys(filters) - .filter((key) => !OMITTED_FIELDS.includes(key)) + .filter( + (key) => + !OMITTED_FIELDS.includes(key) && + filters[key as keyof typeof filters] !== "", + ) .reduce( (acc, key) => ({ ...acc, - ...(Array.isArray(filters[key as keyof typeof filters]) && { - [`filter[${key}]`]: filters[key as keyof typeof filters], - }), + [`filter[${key}]`]: filters[key as keyof typeof filters], }), {}, ); diff --git a/e2e/tests/projects/projects-overview-table.spec.ts b/e2e/tests/projects/projects-overview-table.spec.ts new file mode 100644 index 00000000..5d42b206 --- /dev/null +++ b/e2e/tests/projects/projects-overview-table.spec.ts @@ -0,0 +1,41 @@ +import { expect, Page, test } from "@playwright/test"; +import { E2eTestManager } from "@shared/lib/e2e-test-manager"; +import { User } from "@shared/entities/users/user.entity"; +import {Country} from "@shared/entities/country.entity"; + +let testManager: E2eTestManager; +let page: Page; + +test.describe.configure({ mode: "serial" }); + +test.describe("Projects - Overview Table", () => { + test.beforeAll(async ({ browser }) => { + page = await browser.newPage(); + testManager = await E2eTestManager.load(page); + await testManager.ingestCountries() + }); + + test.afterAll(async () => { + await testManager.clearDatabase(); + await testManager.close(); + }); + + test('I can filter Projects by Country', async () => { + const china = await testManager.getDataSource().getRepository(Country).findOneOrFail({where: {name: 'China'}}); + const india = await testManager.getDataSource().getRepository(Country).findOneOrFail({where: {name: 'India'}}); + const chinaProject = await testManager.mocks().createProject({countryCode: china.code, projectName: 'China Mangrove Conservation Large'}); + const indiaProject = await testManager.mocks().createProject({countryCode: india.code, projectName: 'India Mangrove Conservation Large'}); + await page.goto('http://localhost:3000'); + await page.getByRole('button', { name: 'Filters' }).click(); + await page.locator('button').filter({ hasText: 'All countries' }).click(); + await page.getByText('China', {exact: true }).click(); + const projectsTable = page.locator('table tbody tr') + const projectsInTable = await projectsTable.count(); + expect(projectsInTable).toBe(1); + const firstRowCells = await projectsTable.nth(0).locator('td').allTextContents(); + expect(firstRowCells).toContain(chinaProject.projectName); + + }); +}); + + diff --git a/shared/lib/e2e-test-manager.ts b/shared/lib/e2e-test-manager.ts index ab3c207d..8bedc874 100644 --- a/shared/lib/e2e-test-manager.ts +++ b/shared/lib/e2e-test-manager.ts @@ -1,10 +1,14 @@ import { DataSource } from "typeorm"; import { User } from "@shared/entities/users/user.entity"; -import { createUser } from "@shared/lib/entity-mocks"; +import {createProject, createUser} from "@shared/lib/entity-mocks"; import { clearTestDataFromDatabase } from "@shared/lib/db-helpers"; import { JwtPayload, sign } from "jsonwebtoken"; import { TOKEN_TYPE_ENUM } from "@shared/schemas/auth/token-type.schema"; import { COMMON_DATABASE_ENTITIES } from "@shared/lib/db-entities"; +import {ProjectType} from "@shared/contracts/projects.contract"; +import * as fs from "fs"; +import * as path from "path"; + const AppDataSource = new DataSource({ type: "postgres", @@ -46,6 +50,15 @@ export class E2eTestManager { await this.dataSource.destroy(); } + async ingestCountries() { + const geoCountriesFilePath = path.join( + path.resolve(process.cwd(), '../'), + 'api/src/insert_countries.sql' + ); + const geoCountriesSql = fs.readFileSync(geoCountriesFilePath, 'utf8'); + await this.dataSource.query(geoCountriesSql); + } + async createUser(additionalData?: Partial) { return createUser(this.dataSource, additionalData); } @@ -54,6 +67,8 @@ export class E2eTestManager { return { createUser: (additionalData?: Partial) => createUser(this.getDataSource(), additionalData), + createProject: (additionalData?: Partial) => + createProject(this.getDataSource(), additionalData), }; }