Skip to content

Commit 462eaf4

Browse files
committed
Move "postMessage" helper into separate file
1 parent 798714c commit 462eaf4

3 files changed

Lines changed: 28 additions & 40 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { act } from "@testing-library/react";
2+
3+
/** Helper function used in tests */
4+
export async function postMessage<T>(msg: T): Promise<void> {
5+
await act(async () => {
6+
// window.postMessage doesn't set the origin correctly, see
7+
// https://github.com/jsdom/jsdom/issues/2745
8+
window.dispatchEvent(
9+
new MessageEvent("message", {
10+
source: window,
11+
origin: window.location.origin,
12+
data: msg,
13+
}),
14+
);
15+
16+
// The event is dispatched asynchronously, so we need to wait for it to be handled.
17+
await new Promise((resolve) => setTimeout(resolve, 0));
18+
});
19+
}

extensions/ql-vscode/src/view/results/__tests__/results.spec.tsx

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import { act, render as reactRender, screen } from "@testing-library/react";
2+
import { render as reactRender, screen } from "@testing-library/react";
33
import { ResultsApp } from "../results";
44
import {
55
Interpretation,
@@ -9,6 +9,7 @@ import {
99
import * as fs from "fs-extra";
1010
import { resolve } from "path";
1111
import { ColumnKindCode } from "../../../pure/bqrs-cli-types";
12+
import { postMessage } from "../../common/post-message";
1213

1314
const exampleSarif = fs.readJSONSync(
1415
resolve(
@@ -19,22 +20,6 @@ const exampleSarif = fs.readJSONSync(
1920

2021
describe(ResultsApp.name, () => {
2122
const render = () => reactRender(<ResultsApp />);
22-
const postMessage = async (msg: IntoResultsViewMsg) => {
23-
await act(async () => {
24-
// window.postMessage doesn't set the origin correctly, see
25-
// https://github.com/jsdom/jsdom/issues/2745
26-
window.dispatchEvent(
27-
new MessageEvent("message", {
28-
source: window,
29-
origin: window.location.origin,
30-
data: msg,
31-
}),
32-
);
33-
34-
// The event is dispatched asynchronously, so we need to wait for it to be handled.
35-
await new Promise((resolve) => setTimeout(resolve, 0));
36-
});
37-
};
3823

3924
it("renders results", async () => {
4025
render();
@@ -98,13 +83,13 @@ describe(ResultsApp.name, () => {
9883
},
9984
};
10085

101-
await postMessage(message);
86+
await postMessage<IntoResultsViewMsg>(message);
10287

10388
expect(
10489
screen.getByText("'x' is assigned a value but never used."),
10590
).toBeInTheDocument();
10691

107-
await postMessage({
92+
await postMessage<IntoResultsViewMsg>({
10893
...message,
10994
t: "showInterpretedPage",
11095
pageNumber: 1,
@@ -124,7 +109,7 @@ describe(ResultsApp.name, () => {
124109
it("renders results when switching between queries with different result set names", async () => {
125110
render();
126111

127-
await postMessage({
112+
await postMessage<IntoResultsViewMsg>({
128113
t: "setState",
129114
interpretation: undefined,
130115
origResultsPaths: {
@@ -162,7 +147,7 @@ describe(ResultsApp.name, () => {
162147

163148
expect(screen.getByText("foobar1")).toBeInTheDocument();
164149

165-
await postMessage({
150+
await postMessage<IntoResultsViewMsg>({
166151
t: "setState",
167152
interpretation: undefined,
168153
origResultsPaths: {

extensions/ql-vscode/src/view/variant-analysis/__tests__/VariantAnalysis.spec.tsx

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import { act, render as reactRender, screen } from "@testing-library/react";
2+
import { render as reactRender, screen } from "@testing-library/react";
33
import {
44
VariantAnalysisFailureReason,
55
VariantAnalysisStatus,
@@ -8,6 +8,7 @@ import { VariantAnalysis, VariantAnalysisProps } from "../VariantAnalysis";
88
import { createMockVariantAnalysis } from "../../../../test/factories/variant-analysis/shared/variant-analysis";
99
import { ToVariantAnalysisMessage } from "../../../pure/interface-types";
1010
import { FilterKey, SortKey } from "../../../pure/variant-analysis-filter-sort";
11+
import { postMessage } from "../../common/post-message";
1112

1213
describe(VariantAnalysis.name, () => {
1314
const render = (props: Partial<VariantAnalysisProps> = {}) =>
@@ -18,23 +19,6 @@ describe(VariantAnalysis.name, () => {
1819
/>,
1920
);
2021

21-
const postMessage = async (msg: ToVariantAnalysisMessage) => {
22-
await act(async () => {
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-
3822
it("renders a pending analysis", () => {
3923
const variantAnalysis = createMockVariantAnalysis({
4024
status: VariantAnalysisStatus.InProgress,
@@ -76,7 +60,7 @@ describe(VariantAnalysis.name, () => {
7660
expect(screen.getByDisplayValue("With results")).toBeInTheDocument();
7761
expect(screen.getByDisplayValue("Number of results")).toBeInTheDocument();
7862

79-
await postMessage({
63+
await postMessage<ToVariantAnalysisMessage>({
8064
t: "setVariantAnalysis",
8165
variantAnalysis,
8266
filterSortState: {

0 commit comments

Comments
 (0)