-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathsetupTests.jsx
More file actions
114 lines (94 loc) · 3.86 KB
/
setupTests.jsx
File metadata and controls
114 lines (94 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const http = require("http");
import "vitest-location-mock";
const {spawn} = require("child_process");
import "whatwg-fetch"; // https://github.com/jsdom/jsdom/issues/1724
//import failOnConsole from "jest-fail-on-console";
import {memoryStore} from "react-admin";
import {afterAll, beforeAll, beforeEach, expect} from "vitest";
import {configure, render, screen} from "@testing-library/react";
//import * as structuredClone from "@ungap/structured-clone";
import {App} from "../src/App";
let pythonProcess;
let STATE;
/*jest.setTimeout(300000); // 5 mins
configure({"asyncUtilTimeout": 10000});
jest.mock("react-admin", () => {
const originalModule = jest.requireActual("react-admin");
return {
...originalModule,
downloadCSV: jest.fn(), // Mock downloadCSV to test export button.
};
});*/
// https://github.com/jsdom/jsdom/issues/3363#issuecomment-1387439541
//global.structuredClone = structuredClone.default;
// To render full-width
window.matchMedia = (query) => ({
matches: true,
addListener: () => {},
removeListener: () => {}
});
// Ignore not implemented errors
//window.scrollTo = jest.fn();
global.sleep = (delay_s) => new Promise((resolve) => setTimeout(resolve, delay_s * 1000));
/*failOnConsole({
silenceMessage: (msg) => {
return (
// Suppress act() warnings, because there's too many async changes happening.
msg.includes("inside a test was not wrapped in act(...).")
// Error in react-admin which doesn't actually break anything.
// https://github.com/marmelab/react-admin/issues/8849
|| msg.includes("Fetched record's id attribute")
|| msg.includes("The above error occurred in the <EditBase> component")
);
}
});*/
beforeAll(async() => {
if (!global.pythonProcessPath)
return;
if (global.__coverage__)
pythonProcess = spawn("coverage", ["run", "--append", "--source=examples/,aiohttp_admin/", global.pythonProcessPath], {"cwd": ".."});
else
pythonProcess = spawn("python3", ["-u", global.pythonProcessPath], {"cwd": ".."});
pythonProcess.stderr.on("data", (data) => {console.error(`stderr: ${data}`);});
//pythonProcess.stdout.on("data", (data) => {console.log(`stdout: ${data}`);});
// Wait till server accepts requests
await new Promise(resolve => {
const cutoff = Date.now() + 10000;
function alive() {
http.get("http://localhost:8080/", resolve).on("error", e => Date.now() < cutoff && setTimeout(alive, 100));
}
alive();
});
await new Promise(resolve => {
http.get("http://localhost:8080/admin", resp => {
if (resp.statusCode !== 200)
throw new Error("Request failed");
let html = "";
resp.on("data", (chunk) => { html += chunk; });
resp.on("end", () => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
STATE = JSON.parse(doc.querySelector("body").dataset.state);
resolve();
});
});
});
}, 10000);
afterAll(() => {
if (pythonProcess)
pythonProcess.kill("SIGINT");
});
let login = {"username": "admin", "password": "admin"};
global.setLogin = (username, password) => { login = {username, password}; };
beforeEach(async () => {
location.href = "/";
window.history.replaceState({}, "", "/");
localStorage.clear();
if (STATE) {
const resp = await fetch("http://localhost:8080/admin/token", {"method": "POST", "body": JSON.stringify(login)});
localStorage.setItem("identity", resp.headers.get("X-Token"));
render(<App aiohttpState={STATE} store={memoryStore()} />);
const profile = await screen.findByText(login["username"], {"exact": false});
expect(profile).toHaveAccessibleName("Profile");
}
});