Skip to content

Commit d5610c1

Browse files
committed
Add simple regression unit test for the results view
This adds a really simple regression unit test for the results view which checks that the results view can render a SARIF file. This is in preparation for the upgrade to React 18 to ensure that we don't break the basic functionality of the results view.
1 parent dd9f6bf commit d5610c1

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
lines changed

extensions/ql-vscode/src/view/jest.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ const config: Config = {
8383
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
8484
moduleNameMapper: {
8585
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
86-
"<rootDir>/test/__mocks__/fileMock.ts",
87-
"\\.(css|less)$": "<rootDir>/test/__mocks__/styleMock.ts",
86+
"<rootDir>/../../test/__mocks__/fileMock.ts",
87+
"\\.(css|less)$": "<rootDir>/../../test/__mocks__/styleMock.ts",
8888
},
8989

9090
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
@@ -186,7 +186,7 @@ const config: Config = {
186186
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
187187
transformIgnorePatterns: [
188188
// These use ES modules, so need to be transformed
189-
"node_modules/(?!(?:@vscode/webview-ui-toolkit|@microsoft/.+|exenv-es6)/.*)",
189+
"node_modules/(?!(?:@vscode/webview-ui-toolkit|@microsoft/.+|exenv-es6|d3|d3-(.*)|internmap|delaunator|robust-predicates)/.*)",
190190
],
191191

192192
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import * as React from "react";
2+
import { render as reactRender, screen } from "@testing-library/react";
3+
import { ResultsApp } from "../results";
4+
import {
5+
Interpretation,
6+
IntoResultsViewMsg,
7+
SortDirection,
8+
} from "../../../pure/interface-types";
9+
import * as fs from "fs-extra";
10+
import { resolve } from "path";
11+
import { ColumnKindCode } from "../../../pure/bqrs-cli-types";
12+
13+
const exampleSarif = fs.readJSONSync(
14+
resolve(
15+
__dirname,
16+
"../../../../test/vscode-tests/no-workspace/data/sarif/validSarif.sarif",
17+
),
18+
);
19+
20+
describe(ResultsApp.name, () => {
21+
const render = () => reactRender(<ResultsApp />);
22+
const postMessage = async (msg: IntoResultsViewMsg) => {
23+
// window.postMessage doesn't set the origin correctly, see
24+
// https://github.com/jsdom/jsdom/issues/2745
25+
window.dispatchEvent(
26+
new MessageEvent("message", {
27+
source: window,
28+
origin: window.location.origin,
29+
data: msg,
30+
}),
31+
);
32+
33+
// The event is dispatched asynchronously, so we need to wait for it to be handled.
34+
await new Promise((resolve) => setTimeout(resolve, 0));
35+
};
36+
37+
it("renders results", async () => {
38+
render();
39+
40+
const interpretation: Interpretation = {
41+
sourceLocationPrefix: "/a/b/c",
42+
numTruncatedResults: 0,
43+
numTotalResults: 1,
44+
data: {
45+
t: "SarifInterpretationData",
46+
sortState: undefined,
47+
...exampleSarif,
48+
},
49+
};
50+
const message: IntoResultsViewMsg = {
51+
t: "setState",
52+
resultsPath: "/a/b/c/results",
53+
origResultsPaths: {
54+
resultsPath: "/a/b/c/results.bqrs",
55+
interpretedResultsPath: "/a/b/c/interpreted-results.sarif",
56+
},
57+
sortedResultsMap: {
58+
"1": {
59+
resultsPath: "/a/b/c/results.bqrs",
60+
sortState: {
61+
columnIndex: 1,
62+
sortDirection: SortDirection.asc,
63+
},
64+
},
65+
},
66+
interpretation,
67+
database: {
68+
name: "test-db",
69+
databaseUri: "test-db-uri",
70+
},
71+
metadata: undefined, // TODO
72+
queryName: "test-query",
73+
queryPath: "/a/b/c/query.ql",
74+
shouldKeepOldResultsWhileRendering: false,
75+
parsedResultSets: {
76+
pageNumber: 1,
77+
pageSize: 1,
78+
numPages: 1,
79+
numInterpretedPages: 1,
80+
resultSetNames: ["#select"],
81+
resultSet: {
82+
t: "InterpretedResultSet",
83+
schema: {
84+
name: "#select",
85+
rows: 1,
86+
columns: [
87+
{
88+
name: "Path",
89+
kind: ColumnKindCode.STRING,
90+
},
91+
],
92+
},
93+
name: "#select",
94+
interpretation,
95+
},
96+
},
97+
};
98+
await postMessage(message);
99+
100+
expect(
101+
screen.getByText("'x' is assigned a value but never used."),
102+
).toBeInTheDocument();
103+
104+
await postMessage({
105+
...message,
106+
t: "showInterpretedPage",
107+
pageNumber: 1,
108+
numPages: 1,
109+
pageSize: 1,
110+
resultSetNames: ["#select"],
111+
queryName: "test-query",
112+
queryPath: "/a/b/c/query.ql",
113+
interpretation,
114+
});
115+
116+
expect(
117+
screen.getByText("'x' is assigned a value but never used."),
118+
).toBeInTheDocument();
119+
});
120+
});

0 commit comments

Comments
 (0)