Skip to content

Commit

Permalink
Merge pull request #2191 from djnunez-aot/fix-cypress-test
Browse files Browse the repository at this point in the history
Make endpoints consistent for cypress tests
  • Loading branch information
djnunez-aot authored May 3, 2024
2 parents 1ff5bd1 + 9a02ab7 commit 8ee6da5
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 65 deletions.
15 changes: 12 additions & 3 deletions epictrack-web/cypress/support/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
export function setupIntercepts(endpoints: any[]) {
endpoints.forEach(({ method, url, body }) => {
const response: any = { body };
endpoints.forEach(({ method, url, response, name }) => {
// Add CORS headers specifically for OPTIONS requests
response = {
...response,
headers: {
"Access-Control-Allow-Origin": "*", // Allow all domains
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", // Specify allowed methods
"Access-Control-Allow-Headers": "Content-Type, Authorization", // Specify allowed headers
},
statusCode: 200, // Ensure the response is marked as successful
};

cy.intercept(method, url, response);
cy.intercept(method, url, response).as(name);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,43 @@ import {
mockStaffs,
createMockMasterContext,
} from "../../../../cypress/support/common";
import { setupIntercepts } from "../../../../cypress/support/utils";
import { AppConfig } from "config";
import { setupIntercepts } from "../../../../cypress/support/utils";

const endpoints = [
{
name: "getActiveStaffsOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
},
{
name: "getPIPTypeOptions",
method: "OPTIONS",
url: `{AppConfig.apiUrl}pip-org-types`,
url: `${AppConfig.apiUrl}codes/pip_org_types`,
},

{
name: "getFirstNationsOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}first_nations`,
},
{ method: "OPTIONS", url: `${AppConfig.apiUrl}first_nations` },
{
name: "getActiveStaffs",
method: "GET",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
body: { data: mockStaffs },
response: { body: mockStaffs },
},
{
name: "getPIPType",
method: "GET",
url: `${AppConfig.apiUrl}pip-org-types`,
body: [],
response: { body: [] },
},
{
name: "getFirstNations",
method: "GET",
url: `${AppConfig.apiUrl}first_nations`,
body: [],
response: { body: [] },
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MemoryRouter as Router } from "react-router-dom";
import ProjectList from "../ProjectList";
import { generateMockProject } from "../../../../cypress/support/common";
import { AppConfig } from "config";
import { setupIntercepts } from "../../../../cypress/support/utils";

const project1 = generateMockProject();
const project2 = generateMockProject();
Expand Down Expand Up @@ -32,12 +33,6 @@ const endpoints = [
},
];

function setupIntercepts(endpoints: any[]) {
endpoints.forEach(({ method, url, response, name }) => {
cy.intercept(method, url, response).as(name);
});
}

describe("ProjectList", () => {
beforeEach(() => {
// This assumes you have a route set up for your projects in your commands.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ProponentForm from "../ProponentForm";
import { Staff } from "models/staff";
import { defaultProponent } from "models/proponent";
import { AppConfig } from "config";
import { setupIntercepts } from "../../../../cypress/support/utils";

const staffs: Staff[] = [
{
Expand Down Expand Up @@ -33,28 +34,38 @@ const staffs: Staff[] = [

const endpoints = [
{
name: "getActiveStaffsOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
},
{
name: "getPIPTypeOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}pip-org-types`,
url: `${AppConfig.apiUrl}codes/pip_org_types`,
},
{ method: "OPTIONS", url: `${AppConfig.apiUrl}first_nations` },

{
name: "getFirstNationsOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}first_nations`,
},
{
name: "getActiveStaffs",
method: "GET",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
body: { data: staffs },
response: { body: staffs },
},
{
name: "getPIPType",
method: "GET",
url: `${AppConfig.apiUrl}pip-org-types`,
body: [],
response: { body: [] },
},
{
name: "getFirstNations",
method: "GET",
url: `${AppConfig.apiUrl}first_nations`,
body: [],
response: { body: [] },
},
];

Expand All @@ -80,17 +91,11 @@ function createMockContext() {
setDialogProps: cy.stub(),
};
}
function setupIntercepts(endpoints: any[]) {
endpoints.forEach(({ method, url, body }) => {
cy.intercept(method, url, { body });
});
}

describe("ProponentForm", () => {
beforeEach(() => {
const mockContext = createMockContext();
setupIntercepts(endpoints);

cy.mount(
<MasterContext.Provider value={mockContext}>
<ProponentForm />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
mockStaffs,
testTableFiltering,
} from "../../../../cypress/support/common";
import { setupIntercepts } from "../../../../cypress/support/utils";
import { AppConfig } from "config";
import { setupIntercepts } from "../../../../cypress/support/utils";

//ensure proponents are never the same by incrementing the counter
let proponentCounter = 0;

Expand All @@ -31,31 +32,38 @@ const proponents = [proponent1, proponent2];

const endpoints = [
{
name: "getActiveStaffsOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
},
{
name: "getPIPTypeOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}pip-org-types`,
url: `${AppConfig.apiUrl}codes/pip_org_types`,
},

{
name: "getFirstNationsOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}first_nations`,
},
{
name: "getActiveStaff",
method: "GET",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
body: { data: mockStaffs },
response: { body: mockStaffs },
},
{
name: "getPIPType",
method: "GET",
url: `${AppConfig.apiUrl}pip-org-types`,
body: [],
response: { body: [] },
},
{
name: "getFirstNations",
method: "GET",
url: `${AppConfig.apiUrl}first_nations`,
body: [],
response: { body: [] },
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
generateMockProject,
testTableFiltering,
} from "../../../../cypress/support/common";
import { setupIntercepts } from "../../../../cypress/support/utils";
import { defaultFirstNation } from "models/firstNation";
import { MasterContext } from "components/shared/MasterContext";
import { ETGridTitle } from "..";
Expand All @@ -19,6 +18,7 @@ import { FirstNation } from "models/firstNation";
import { MemoryRouter as Router } from "react-router-dom";
import { Project } from "models/project";
import { AppConfig } from "config";
import { setupIntercepts } from "../../../../cypress/support/utils";

// Mock reducer
declare global {
Expand All @@ -29,28 +29,22 @@ declare global {

const endpoints = [
{
method: "OPTIONS",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
},
{
method: "OPTIONS",
url: `${AppConfig.apiUrl}pip-org-types`,
},
{ method: "OPTIONS", url: `${AppConfig.apiUrl}first_nations` },
{
name: "getActiveStaffs",
method: "GET",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
body: { data: mockStaffs },
response: { body: { data: mockStaffs } },
},
{
name: "getPIPType",
method: "GET",
url: `${AppConfig.apiUrl}pip-org-types`,
body: [],
response: { body: [] },
},
{
name: "getFirstNations",
method: "GET",
url: `${AppConfig.apiUrl}first_nations`,
body: [],
response: { body: [] },
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
SpecialFieldEntityEnum,
SPECIAL_FIELDS,
} from "../../../constants/application-constant";

import { setupIntercepts } from "../../../../cypress/support/utils";
import { faker } from "@faker-js/faker";
import { AppConfig } from "config";
Expand Down
26 changes: 21 additions & 5 deletions epictrack-web/src/components/staff/__test__/StaffForm.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,49 @@ import {
createMockMasterContext,
mockStaffs,
} from "../../../../cypress/support/common";
import { setupIntercepts } from "../../../../cypress/support/utils";
import { AppConfig } from "config";
import { setupIntercepts } from "../../../../cypress/support/utils";

const endpoints = [
{
name: "getActiveStaffsOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
},
{
name: "getPIPTypeOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}codes/pip_org_types`,
},
{ method: "OPTIONS", url: `${AppConfig.apiUrl}first_nations` },

{
name: "getFirstNationsOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}first_nations`,
},
{
name: "getActiveStaffs",
method: "GET",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
body: { data: mockStaffs },
response: { body: { data: mockStaffs } },
},
{
name: "getPIPType",
method: "GET",
url: `${AppConfig.apiUrl}codes/pip_org_types`,
body: [],
response: { body: [] },
},
{
name: "getFirstNations",
method: "GET",
url: `${AppConfig.apiUrl}first_nations`,
body: [],
response: { body: [] },
},
{
name: "getPositions",
method: "GET",
url: `${AppConfig.apiUrl}positions`,
response: { body: [] },
},
];

Expand Down
15 changes: 11 additions & 4 deletions epictrack-web/src/components/staff/__test__/StaffList.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
mockStaffs,
testTableFiltering,
} from "../../../../cypress/support/common";
import { setupIntercepts } from "../../../../cypress/support/utils";
import { AppConfig } from "config";
import { setupIntercepts } from "../../../../cypress/support/utils";

//ensure staffs are never the same by incrementing the counter
let staffCounter = 0;
Expand All @@ -38,31 +38,38 @@ const staffs = [staff1, staff2];

const endpoints = [
{
name: "getActiveStaffsOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
},
{
name: "getPIPTypeOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}codes/pip_org_types`,
},

{
name: "getFirstNationsOptions",
method: "OPTIONS",
url: `${AppConfig.apiUrl}first_nations`,
},
{
name: "getInactiveStaffs",
method: "GET",
url: `${AppConfig.apiUrl}staffs?is_active=false`,
body: { data: mockStaffs },
response: { body: { data: mockStaffs } },
},
{
name: "getPIPType",
method: "GET",
url: `${AppConfig.apiUrl}codes/pip_org_types`,
body: [],
response: { body: [] },
},
{
name: "getFirstNations",
method: "GET",
url: `${AppConfig.apiUrl}first_nations`,
body: [],
response: { body: [] },
},
];

Expand Down
Loading

0 comments on commit 8ee6da5

Please sign in to comment.