Skip to content

Commit

Permalink
fixed unintentional removal of countryCode filter, handling an empty …
Browse files Browse the repository at this point in the history
…("") initial countryCode filter
  • Loading branch information
alexeh committed Jan 10, 2025
1 parent 06d3880 commit 0100ecd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
11 changes: 7 additions & 4 deletions client/src/containers/overview/table/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ const OMITTED_FIELDS = [
"costRange",
"abatementPotentialRange",
"costRangeSelector",
"priceType",
];

export const filtersToQueryParams = (
filters: z.infer<typeof filtersSchema | typeof scorecardFiltersSchema>,
) => {
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],
}),
{},
);
Expand Down
41 changes: 41 additions & 0 deletions e2e/tests/projects/projects-overview-table.spec.ts
Original file line number Diff line number Diff line change
@@ -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);

});
});


17 changes: 16 additions & 1 deletion shared/lib/e2e-test-manager.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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<User>) {
return createUser(this.dataSource, additionalData);
}
Expand All @@ -54,6 +67,8 @@ export class E2eTestManager {
return {
createUser: (additionalData?: Partial<User>) =>
createUser(this.getDataSource(), additionalData),
createProject: (additionalData?: Partial<ProjectType>) =>
createProject(this.getDataSource(), additionalData),
};
}

Expand Down

0 comments on commit 0100ecd

Please sign in to comment.