Skip to content

Commit aa66438

Browse files
committed
Add tests
1 parent 3d19afc commit aa66438

14 files changed

+1572
-26
lines changed

cypress.config.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defineConfig } from "cypress";
2+
3+
export default defineConfig({
4+
component: {
5+
devServer: {
6+
framework: "next",
7+
bundler: "webpack",
8+
},
9+
},
10+
});

cypress/mocks/mountWithMocks.tsx

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { mount } from "cypress/react18";
2+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
3+
import { minutesToMillis } from "@/utils/dateUtils";
4+
import React from "react";
5+
import {
6+
getAktivitetskravVurderingForScenario,
7+
TestScenario,
8+
} from "@/utils/testScenarioUtils";
9+
10+
interface ReactQueryProps {
11+
children: React.JSX.Element;
12+
mockReactQuery?: boolean;
13+
}
14+
15+
const ReactQueryProvider = ({
16+
children,
17+
}: ReactQueryProps): React.JSX.Element => {
18+
return (
19+
<QueryClientProvider
20+
client={
21+
new QueryClient({
22+
defaultOptions: {
23+
queries: {
24+
refetchOnWindowFocus: false,
25+
staleTime: minutesToMillis(30),
26+
},
27+
},
28+
})
29+
}
30+
>
31+
{children}
32+
</QueryClientProvider>
33+
);
34+
};
35+
export const mountWithMocks = (
36+
componentUnderTest: React.JSX.Element,
37+
testScenario: TestScenario,
38+
) => {
39+
cy.intercept("/api/aktivitetsplikt", getAktivitetskravVurderingForScenario(testScenario)).as(
40+
"hentVurdering",
41+
);
42+
43+
return mount(<ReactQueryProvider>{componentUnderTest}</ReactQueryProvider>);
44+
};

cypress/support/commands.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************
3+
// This example commands.ts shows you how to
4+
// create various custom commands and overwrite
5+
// existing commands.
6+
//
7+
// For more comprehensive examples of custom
8+
// commands please read more here:
9+
// https://on.cypress.io/custom-commands
10+
// ***********************************************
11+
//
12+
//
13+
// -- This is a parent command --
14+
// Cypress.Commands.add('login', (email, password) => { ... })
15+
//
16+
//
17+
// -- This is a child command --
18+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
19+
//
20+
//
21+
// -- This is a dual command --
22+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
23+
//
24+
//
25+
// -- This will overwrite an existing command --
26+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
27+
//
28+
// declare global {
29+
// namespace Cypress {
30+
// interface Chainable {
31+
// login(email: string, password: string): Chainable<void>
32+
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
33+
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
34+
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
35+
// }
36+
// }
37+
// }

cypress/support/component-index.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
7+
<title>Components App</title>
8+
<!-- Used by Next.js to inject CSS. -->
9+
<div id="__next_css__DO_NOT_USE__"></div>
10+
</head>
11+
<body>
12+
<div data-cy-root></div>
13+
</body>
14+
</html>

cypress/support/component.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// ***********************************************************
2+
// This example support/component.ts is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import "./commands";
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')
21+
import { mount } from "cypress/react18";
22+
import { mountWithMocks } from "../mocks/mountWithMocks";
23+
import { TestScenario } from "@/utils/testScenarioUtils";
24+
import { MountReturn } from "cypress/react";
25+
26+
declare global {
27+
namespace Cypress {
28+
interface Chainable {
29+
mount: typeof mount;
30+
mountWithMocks: (
31+
componentUnderTest: JSX.Element,
32+
testScenario: TestScenario,
33+
) => Cypress.Chainable<MountReturn>;
34+
}
35+
}
36+
}
37+
38+
Cypress.Commands.add("mount", mount);
39+
Cypress.Commands.add("mountWithMocks", mountWithMocks);
40+
41+
// Example use:
42+
// cy.mount(<MyComponent />)

0 commit comments

Comments
 (0)