1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Upgrade XTermJs to v5 to get snapshot testing working

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-10-17 10:22:25 -04:00
parent f11145045b
commit c9f1594907
7 changed files with 145 additions and 43 deletions

View File

@ -449,7 +449,7 @@
"webpack-cli": "^4.9.2", "webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.11.1", "webpack-dev-server": "^4.11.1",
"webpack-node-externals": "^3.0.0", "webpack-node-externals": "^3.0.0",
"xterm": "^4.19.0", "xterm": "^5.0.0",
"xterm-addon-fit": "^0.5.0" "xterm-addon-fit": "^0.5.0"
}, },
"peerDependencies": { "peerDependencies": {

File diff suppressed because one or more lines are too long

View File

@ -4,32 +4,22 @@
*/ */
import type { RenderResult } from "@testing-library/react"; import type { RenderResult } from "@testing-library/react";
import assert from "assert";
import { TypedRegEx } from "typed-regex";
import type { ApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder"; import type { ApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder";
import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder"; import { getApplicationBuilder } from "../../renderer/components/test-utils/get-application-builder";
import type { FindByTextWithMarkup } from "../../test-utils/findByTextWithMarkup";
const pngBase64Matcher = TypedRegEx("data:image/png;base64,(?<ENCODED>.+)"); import { findByTextWithMarkupFor } from "../../test-utils/findByTextWithMarkup";
function convertCanvasToPngBuffer(canvas: { toDataURL: () => string }): Buffer {
const content = canvas.toDataURL();
const match = pngBase64Matcher.captures(content);
assert(match);
return Buffer.from(match.ENCODED, "base64");
}
describe("test for opening terminal tab within cluster frame", () => { describe("test for opening terminal tab within cluster frame", () => {
let builder: ApplicationBuilder; let builder: ApplicationBuilder;
let result: RenderResult; let result: RenderResult;
let textCanvas: HTMLCanvasElement; let findByTextWithMarkup: FindByTextWithMarkup;
beforeEach(async () => { beforeEach(async () => {
builder = getApplicationBuilder(); builder = getApplicationBuilder();
builder.setEnvironmentToClusterFrame(); builder.setEnvironmentToClusterFrame();
result = await builder.render(); result = await builder.render();
findByTextWithMarkup = findByTextWithMarkupFor(result);
}); });
afterEach(() => { afterEach(() => {
@ -39,7 +29,6 @@ describe("test for opening terminal tab within cluster frame", () => {
describe("when new terminal tab is opened", () => { describe("when new terminal tab is opened", () => {
beforeEach(() => { beforeEach(() => {
result.getByTestId("dock-tab-for-terminal").click(); result.getByTestId("dock-tab-for-terminal").click();
textCanvas = result.baseElement.querySelector("canvas.xterm-text-layer") as HTMLCanvasElement;
}); });
it("renders", () => { it("renders", () => {
@ -53,7 +42,7 @@ describe("test for opening terminal tab within cluster frame", () => {
}); });
it("shows connecting message", async () => { it("shows connecting message", async () => {
expect(convertCanvasToPngBuffer(textCanvas)).toMatchImageSnapshot(); await findByTextWithMarkup("Connecting ...");
}); });
it.skip("connects websocket to main", () => { it.skip("connects websocket to main", () => {

View File

@ -91,6 +91,7 @@ export class Terminal {
this.api = api; this.api = api;
this.xterm = this.dependencies.createTerminalRenderer({ this.xterm = this.dependencies.createTerminalRenderer({
allowProposedApi: true,
cursorBlink: true, cursorBlink: true,
cursorStyle: "bar", cursorStyle: "bar",
fontSize: this.fontSize, fontSize: this.fontSize,
@ -164,11 +165,12 @@ export class Terminal {
}; };
onData = (data: string) => { onData = (data: string) => {
if (!this.api.isReady) return; if (this.api.isReady) {
this.api.sendMessage({ this.api.sendMessage({
type: TerminalChannels.STDIN, type: TerminalChannels.STDIN,
data, data,
}); });
}
}; };
onScroll = () => { onScroll = () => {
@ -176,7 +178,6 @@ export class Terminal {
}; };
onClear = () => { onClear = () => {
console.log("clearing");
this.xterm.clear(); this.xterm.clear();
}; };

View File

@ -0,0 +1,34 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { MatcherFunction, RenderResult, SelectorMatcherOptions, waitForOptions } from "@testing-library/react";
import { getDefaultNormalizer } from "@testing-library/react";
const hasTextFor = (text: string) => {
const normalize = getDefaultNormalizer();
return (
(node: HTMLElement | Element) => normalize(node.textContent ?? "") === text
);
};
export type FindByTextWithMarkup = (text: string, options?: SelectorMatcherOptions, waitForElementOptions?: waitForOptions) => Promise<void>;
export function findByTextWithMarkupFor(result: RenderResult): FindByTextWithMarkup {
return async (text, options, waitForOptions) => {
const hasText = hasTextFor(text);
const matcherFunction: MatcherFunction = (content, element): boolean => {
if (!element) {
return false;
}
const childrenDontHaveText = Array.from(element.children)
.every(child => !hasText(child));
return hasText(element) && childrenDontHaveText;
};
await result.findByText(matcherFunction, options, waitForOptions);
};
}

View File

@ -14000,9 +14000,9 @@ ws@^8.12.0:
integrity sha512-kU62emKIdKVeEIOIKVegvqpXMSTAMLJozpHZaJNDYqBjzlSYXQGviYwN1osDLJ9av68qHd4a2oSjd7yD4pacig== integrity sha512-kU62emKIdKVeEIOIKVegvqpXMSTAMLJozpHZaJNDYqBjzlSYXQGviYwN1osDLJ9av68qHd4a2oSjd7yD4pacig==
ws@^8.2.3, ws@^8.4.2, ws@^8.9.0: ws@^8.2.3, ws@^8.4.2, ws@^8.9.0:
version "8.11.0" version "8.10.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.11.0.tgz#6a0d36b8edfd9f96d8b25683db2f8d7de6e8e143" resolved "https://registry.yarnpkg.com/ws/-/ws-8.10.0.tgz#00a28c09dfb76eae4eb45c3b565f771d6951aa51"
integrity sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg== integrity sha512-+s49uSmZpvtAsd2h37vIPy1RBusaLawVe8of+GyEPsaJTCMpj/2v8NpeK1SHXjBlQ95lQTmQofOJnFiLoaN3yw==
xml-name-validator@^4.0.0: xml-name-validator@^4.0.0:
version "4.0.0" version "4.0.0"
@ -14041,11 +14041,16 @@ xterm-link-provider@^1.3.1:
dependencies: dependencies:
xterm "^4.6.0" xterm "^4.6.0"
xterm@^4.19.0, xterm@^4.6.0: xterm@^4.6.0:
version "4.19.0" version "4.19.0"
resolved "https://registry.yarnpkg.com/xterm/-/xterm-4.19.0.tgz#c0f9d09cd61de1d658f43ca75f992197add9ef6d" resolved "https://registry.yarnpkg.com/xterm/-/xterm-4.19.0.tgz#c0f9d09cd61de1d658f43ca75f992197add9ef6d"
integrity sha512-c3Cp4eOVsYY5Q839dR5IejghRPpxciGmLWWaP9g+ppfMeBChMeLa1DCA+pmX/jyDZ+zxFOmlJL/82qVdayVoGQ== integrity sha512-c3Cp4eOVsYY5Q839dR5IejghRPpxciGmLWWaP9g+ppfMeBChMeLa1DCA+pmX/jyDZ+zxFOmlJL/82qVdayVoGQ==
xterm@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/xterm/-/xterm-5.0.0.tgz#0af50509b33d0dc62fde7a4ec17750b8e453cc5c"
integrity sha512-tmVsKzZovAYNDIaUinfz+VDclraQpPUnAME+JawosgWRMphInDded/PuY0xmU5dOhyeYZsI0nz5yd8dPYsdLTA==
y18n@^5.0.5: y18n@^5.0.5:
version "5.0.8" version "5.0.8"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55" resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55"