|
| 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