Skip to content

Commit

Permalink
fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jackiequach committed Aug 27, 2024
1 parent f2e6d1d commit e01c916
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 53 deletions.
20 changes: 12 additions & 8 deletions mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { http, HttpResponse, passthrough } from "msw";
import {
collectionItem,
oneCollectionListData,
} from "~/src/__tests__/fixtures/CollectionFixture";
import { oneCollectionListData } from "~/src/__tests__/fixtures/CollectionFixture";
import { workDetailWithUp } from "~/src/__tests__/fixtures/WorkDetailFixture";
import {
API_URL,
COLLECTION_LIST_PATH,
COLLECTION_PATH,
INVALID_COLLECTION_PATH,
DOWNLOAD_PATH,
FULFILL_PATH,
LIMITED_ACCESS_WORK_PATH,
Expand All @@ -22,7 +19,10 @@ const workUrl = new URL(LIMITED_ACCESS_WORK_PATH, API_URL).toString();
const fulfillUrl = new URL(FULFILL_PATH, API_URL).toString();
const downloadUrl = new URL(DOWNLOAD_PATH, API_URL).toString();
const collectionListUrl = new URL(COLLECTION_LIST_PATH, API_URL).toString();
const collectionUrl = new URL(COLLECTION_PATH, API_URL).toString();
const invalidCollectionUrl = new URL(
INVALID_COLLECTION_PATH,
API_URL
).toString();

/** A collection of handlers to be used by default for all tests. */
const handlers = [
Expand Down Expand Up @@ -68,8 +68,12 @@ const handlers = [
return HttpResponse.json(oneCollectionListData);
}),

http.get(collectionUrl, () => {
return HttpResponse.json(collectionItem);
http.get(invalidCollectionUrl, () => {
return HttpResponse.json({
status: 500,
title: "Something went wrong",
detail: "An unknown error occurred on the server",
});
}),
];

Expand Down
4 changes: 1 addition & 3 deletions mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
async function initMocks() {
export async function initMocks() {
if (typeof window === "undefined") {
const { server } = await import("./server");
server.listen({
Expand All @@ -12,6 +12,4 @@ async function initMocks() {
}
}

initMocks();

export {};
3 changes: 2 additions & 1 deletion mocks/mockEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const DOWNLOAD_PATH = "/test-download-pdf";
export const HOME_PATH = "/";
export const COLLECTION_LIST_PATH = "/collection/list";
export const COLLECTION_PATH =
"/collection/978ea0e0-8ecc-4de2-bfe8-032fea641d8e";
"/collection/978ea0e0-8ecc-4de2-bfe8-032fea641d8e?page=1";
export const INVALID_COLLECTION_PATH = "/collection/invalid-collection";
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@
"prettier": "^2.2.1",
"ts-node": "^10.9.1"
},
"resolutions": {
"@types/react": "^17.0.2"
},
"msw": {
"workerDirectory": "public"
}
Expand Down
33 changes: 13 additions & 20 deletions playwright/integration/landing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { test, expect } from "../support/test-utils";
import { API_URL, COLLECTION_PATH, HOME_PATH } from "~/mocks/mockEnv";
import {
INVALID_COLLECTION_PATH,
HOME_PATH,
COLLECTION_PATH,
} from "~/mocks/mockEnv";
import { server } from "~/mocks/server";
import { HttpResponse } from "msw";

test.afterEach(() => server.resetHandlers());
test.afterAll(() => server.close());
Expand All @@ -13,26 +16,16 @@ test("View landing page with collection", async ({ page, port }) => {
level: 2,
});
expect(collectionHeading).toBeVisible();
const collectionLink = page.getByRole("link", {
name: /Baseball: A Collection by Mike Benowitz/,
});
await expect(collectionLink).toHaveAttribute("href", COLLECTION_PATH);
const collectionLink = await page
.getByRole("link", {
name: /Baseball: A Collection by Mike Benowitz/,
})
.getAttribute("href");
expect(collectionLink).toContain(COLLECTION_PATH);
});

test("Shows error boundary on error", async ({ page, port, http }) => {
await page.goto(`http://localhost:${port}${HOME_PATH}`);
server.use(
http.get(new RegExp(API_URL), async () => {
return HttpResponse.json(
{
status: 500,
title: "Something went wrong",
detail: "An unknown error occurred on the server",
},
{ status: 500 }
);
})
);
test("Shows error boundary for invalid collection", async ({ page, port }) => {
await page.goto(`http://localhost:${port}${INVALID_COLLECTION_PATH}`);

const alert = page.getByRole("alert");
const errorText = alert.getByText("An error 500 occurred on server");
Expand Down
8 changes: 6 additions & 2 deletions src/components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class ErrorBoundary extends React.Component<
ErrorBoundaryProps,
ErrorBoundaryState
> {
state = { error: undefined, info: undefined };
constructor(props) {
super(props);

this.state = { error: undefined, info: undefined };
}

static getDerivedStateFromError(error: Error) {
return { error };
Expand All @@ -32,7 +36,7 @@ class ErrorBoundary extends React.Component<
const { error } = this.state;

if (error) {
return <Error statusCode={error.statusCode} />;
return <Error statusCode={error} />;
}

return this.props.children;
Expand Down
5 changes: 3 additions & 2 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import NewRelicSnippet from "../lib/newrelic/NewRelic";
import ErrorBoundary from "../components/ErrorBoundary";

if (process.env.APP_ENV === "testing") {
require("mocks");
const { initMocks } = await import("mocks");
await initMocks();
}

/**
Expand Down Expand Up @@ -91,8 +92,8 @@ const MyApp = ({ Component, pageProps }: AppProps) => {

<link rel="icon" href={appConfig.favIconPath} />
</Head>
<NewRelicSnippet />
<ErrorBoundary>
<NewRelicSnippet />
<FeatureFlagProvider>
<Component {...pageProps} />
</FeatureFlagProvider>
Expand Down
18 changes: 4 additions & 14 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"target": "es2017",
"lib": ["dom", "dom.iterable", "esnext"],
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"~/*": [
"*"
]
"~/*": ["*"]
},
"allowJs": true,
"skipLibCheck": true,
Expand Down Expand Up @@ -46,9 +40,5 @@
"src/components/RequestDigital/RequestDigital.jsx",
"src/__tests__/index.test.tsx"
],
"exclude": [
"node_modules",
"jest.config.ts",
"setupTests.js"
]
"exclude": ["node_modules", "jest.config.ts", "setupTests.js"]
}

0 comments on commit e01c916

Please sign in to comment.