Skip to content

Commit

Permalink
Remove dependency to jsdom and update tests (#399)
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Fontorbe <[email protected]>
  • Loading branch information
gfontorbe authored Nov 9, 2023
1 parent f785425 commit 8dba94d
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 280 deletions.
4 changes: 1 addition & 3 deletions packages/sprotty/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@
"tinyqueue": "^2.0.3"
},
"devDependencies": {
"@types/chai": "^4.3.6",
"happy-dom": "^12.10.3",
"@types/file-saver": "^2.0.5",
"@types/jsdom": "^21.1.3",
"jsdom": "^22.1.0",
"snabbdom-to-html": "^7.1.0"
},
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions packages/sprotty/src/base/commands/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import 'reflect-metadata';
import { injectable } from "inversify";
import { Action } from "sprotty-protocol/lib/actions";
import { ILogger } from "../../utils/logging";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/** @jsx svg */
import { svg } from '../../lib/jsx';

import 'reflect-metadata';
import { expect, describe, it } from 'vitest';
import { CssClassPostprocessor } from './css-class-postprocessor';
import { SModelElementImpl } from '../model/smodel';
Expand Down
13 changes: 5 additions & 8 deletions packages/sprotty/src/base/views/thunk-view.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ import { init } from "snabbdom";
import { SModelElementImpl } from "../model/smodel";
import { ModelRenderer } from './viewer';
import { ThunkView } from './thunk-view';
import setup from '../../utils/test-helper';
import toHTML from 'snabbdom-to-html';


/**
* @vitest-environment happy-dom
*/
describe('ThunkView', () => {

before(function () {
setup();
});

let renderCount = 0;

class Foo extends SModelElementImpl {
Expand Down Expand Up @@ -61,7 +57,8 @@ describe('ThunkView', () => {
element.foo = 'first';
const view = new FooView();
const vnode = view.render(element, context);
const domElement = document.createElement('div');
const domElement =
document.createElement('div');
domElement.setAttribute('id', 'sprotty');
patcher(domElement, vnode);
expect(toHTML(vnode)).to.be.equal('<g id="0"></g>');
Expand Down
3 changes: 1 addition & 2 deletions packages/sprotty/src/lib/jsx.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
/** @jsx svg */
import { svg } from './jsx';

import 'mocha';
import { expect } from 'chai';
import { expect, describe, it } from 'vitest';
import { h } from 'snabbdom';

const svgNS = 'http://www.w3.org/2000/svg';
Expand Down
84 changes: 43 additions & 41 deletions packages/sprotty/src/lib/virtualize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,50 @@
"use strict";

import { beforeAll, describe, expect, it } from 'vitest';
import { h } from 'snabbdom';
import { VNode, h } from 'snabbdom';
import virtualize from './virtualize';
import setup from '../utils/test-helper';

/**
* @vitest-environment happy-dom
*/
describe("virtualize (happy path)", () => {
beforeAll(() => {
setup();
});

it("should convert a single node with no children", () => {
expect(virtualize("<div />")).to.deep.equal(h("div"));
expect(virtualizeHelper("<div />")).to.deep.equal(h("DIV"));
});

it("should convert a node with text node", () => {
expect(virtualize("<div> Test. </div>")).to.deep.equal(
h("div", [" Test. "])
expect(virtualizeHelper("<div> Test. </div>")).to.deep.equal(
h("DIV", [" Test. "])
);
});

it("should convert nodes with children", () => {
expect(virtualize("<div><span>a</span><span>b</span></div>")).to.deep.equal(
h("div", [
h("span", ["a"]),
h("span", ["b"])
expect(virtualizeHelper("<div><span>a</span><span>b</span></div>")).to.deep.equal(
h("DIV", [
h("SPAN", ["a"]),
h("SPAN", ["b"])
])
);
});

it("should convert xml document", () => {
expect(
virtualize(
virtualizeHelper(
"<book><title>The Three-Body Problem</title><author>Liu Cixin</author></book>"
)
).to.deep.equal(
h("book", [
h("title", ["The Three-Body Problem"]),
h("author", ["Liu Cixin"]),
h("BOOK", [
h("TITLE", ["The Three-Body Problem"]),
h("AUTHOR", ["Liu Cixin"]),
])
);
});

it("should convert a single node with attributes", () => {
const element =
'<div class="sprotty1 sprotty2" style="display: none !important; background-color: blue; font-weight: bold" data-test="test" />';
expect(virtualize(element)).to.deep.equal(
h("div", {
expect(virtualizeHelper(element)).to.deep.equal(
h("DIV", {
class: {
sprotty1: true,
sprotty2: true,
Expand All @@ -80,16 +78,16 @@ describe("virtualize (happy path)", () => {
});

it("should ignore empty attributes", () => {
expect(virtualize("<span style='' />")).to.deep.equal(h("span"));
expect(virtualize("<span class='' />")).to.deep.equal(h("span"));
expect(virtualizeHelper("<span style='' />")).to.deep.equal(h("SPAN"));
expect(virtualizeHelper("<span class='' />")).to.deep.equal(h("SPAN"));
});

it("should handle control characters in attribute values", () => {
const input = "<textarea placeholder=' Test1, \n\n Test2 '></textarea>";
expect(virtualize(input)).to.deep.equal(
h("textarea", {
expect(virtualizeHelper(input)).to.deep.equal(
h("TEXTAREA", {
attrs: {
placeholder: " Test1, Test2 ",
placeholder: " Test1, \n\n Test2 ",
},
})
);
Expand All @@ -98,8 +96,8 @@ describe("virtualize (happy path)", () => {
it("should handle entities in attribute values", () => {
const input =
"<textarea placeholder='&amp; Test1, &gt; Test2 '></textarea>";
expect(virtualize(input)).to.deep.equal(
h("textarea", {
expect(virtualizeHelper(input)).to.deep.equal(
h("TEXTAREA", {
attrs: {
placeholder: "& Test1, > Test2 ",
},
Expand All @@ -109,32 +107,36 @@ describe("virtualize (happy path)", () => {

it("should ignore comments", () => {
expect(
virtualize(
virtualizeHelper(
"<div> <!-- comment A --> <span>Test1</span> <!-- Comment B --> Test2</div>"
)
).to.deep.equal(h("div", [" ", " ", h("span", ["Test1"]), " ", " Test2"]));
).to.deep.equal(h("DIV", [" ", " ", h("SPAN", ["Test1"]), " ", " Test2"]));
});
});

/**
* @vitest-environment happy-dom
*/
describe("virtualize (bad path)", () => {
beforeAll(() => {
setup();
});

it("should return null when given null or empty string", () => {
expect(virtualize()).to.be.null;
expect(virtualize("")).to.be.null;
});

it("should return parser error when given a single text node", () => {
const actual = virtualize("Text content!");
expect(actual?.sel).to.equal("parsererror");
});

it("should return parser error when gives multiple top-level nodes", () => {
const actual = virtualize(
"<div><h1>Something</h1></div><span>Something more</span>"
);
expect(actual?.sel).to.equal("parsererror");
const actual = virtualizeHelper("Text content!") as VNode;
expect(actual?.sel).to.equal(undefined);
});
});

function virtualizeHelper(html?: string) {
const dom = virtualize(html);

if (!dom) {
return null;
}

const element = (dom.children![1] as VNode).children![0];

return element;
}
30 changes: 0 additions & 30 deletions packages/sprotty/src/utils/test-helper.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/sprotty/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
"exclude": [
"**/*.spec.ts",
"**/*.spec.tsx",
"src/utils/test-helper.ts"
]
}
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default defineConfig({
deps: {
interopDefault: true
},
include: ['**/*.spec.ts'],
include: ['**/*.spec.ts', '**/*.spec.tsx'],
globals: true,
}
});
Loading

0 comments on commit 8dba94d

Please sign in to comment.