Skip to content

Commit 25b2436

Browse files
authored
testing improvements (#91)
* initial commit * added react17 and 18 as peer dependencies * only react18 as peer dependency * update prettier and eslint scripts * react as a peer dependency and test script improvements * temporarily remove depcheck * fix vite entry point for build * started unit tests for core and root * build options changes * update github workflow * regenerate package-lock.json * misc test update * improved build and testing * clean ups and legacy export * make types accessible in build * restructure testing folders * path fix; * another path fix * creared node scripts for copying test file * update path for test copy script * remove unneccesary root tests and update core tests * remove can-stache test and npm package and wrap tests in describe block * vite build test * update typecheck script * update tsconfig to exclude builds test folder * update buildTests to build vite example build * swc build * webpack build test * misc script fix * buildtests fix * clean up
1 parent b3c7317 commit 25b2436

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+32775
-801
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
node_modules/
22
dist/
3+
dist-swc/
4+
build/
35

46
src/tests/*/react-to-webcomponent.test.jsx
57

package-lock.json

Lines changed: 18 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "2.0.0-alpha.3",
44
"description": "Convert react components to native Web Components.",
55
"type": "module",
6+
"types": "dist/root/root.d.ts",
67
"exports": {
78
".": {
89
"import": "./dist/root.js",
@@ -36,8 +37,6 @@
3637
"@types/react": "^18.0.33",
3738
"@types/react-dom": "^18.0.11",
3839
"@webcomponents/custom-elements": "^1.2.4",
39-
"can-stache": "^4.17.20",
40-
"can-stache-bindings": "^4.10.9",
4140
"depcheck": "^1.4.3",
4241
"eslint": "^8.0.0",
4342
"eslint-plugin-react": "^7.21.4",
@@ -53,9 +52,9 @@
5352
"build": "vite build",
5453
"ci": "npm run test",
5554
"buildtests": "chmod +x src/tests/buildtests && ./src/tests/buildtests",
56-
"test": "vitest src/tests/**/react-to-webcomponent.test.jsx && npm run test:core && npm run test:root",
55+
"test": "vitest src/tests/environments/**/react-to-webcomponent.test.jsx && npm run test:core && npm run test:builds",
5756
"test:core": "vitest src/core/core.test.tsx",
58-
"test:root": "vitest src/root/root.test.tsx",
57+
"test:builds": "vitest src/tests/builds/builds.test.ts",
5958
"start": "http-server -c-1",
6059
"typecheck": "tsc --noEmit",
6160
"eslint": "eslint ./src/**/*.ts",

src/core/core.test.tsx

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,61 @@
1-
import { test, expect, vi } from "vitest"
1+
import { describe, test, expect, vi, afterEach } from "vitest"
2+
import React from "react"
23
import r2wc from "."
34

45
// mock renderer
56
const mount = vi.fn()
67
const unmount = vi.fn()
78

8-
test("mount and unmount is called", async () => {
9-
function TestComponent() {
10-
11-
return <div>hello</div>
12-
}
13-
const body = document.body
9+
describe("r2wc core", () => {
10+
afterEach(() => {
11+
// reset DOM
12+
document.body.innerHTML = ""
13+
// reset mock
14+
mount.mockReset()
15+
unmount.mockReset()
16+
})
17+
test("mount and unmount is called in functional component", async () => {
18+
function TestComponent() {
19+
return <div>hello</div>
20+
}
21+
const body = document.body
1422

15-
const TestElement = r2wc(TestComponent, {}, { mount, unmount })
16-
customElements.define("test-element", TestElement)
23+
const TestElement = r2wc(TestComponent, {}, { mount, unmount })
24+
customElements.define("test-func-element", TestElement)
1725

18-
const testEl = new TestElement();
26+
const testEl = new TestElement()
1927

20-
body.appendChild(testEl)
28+
body.appendChild(testEl)
2129

22-
expect(mount).toBeCalledTimes(1)
30+
expect(mount).toBeCalledTimes(1)
2331

24-
body.removeChild(testEl)
32+
body.removeChild(testEl)
2533

26-
expect(unmount).toBeCalledTimes(1)
34+
expect(unmount).toBeCalledTimes(1)
35+
})
2736

37+
test("mount and unmount is called in class component", async () => {
38+
class TestClassComponent extends React.Component {
39+
render() {
40+
return <div>hello</div>
41+
}
42+
}
43+
const body = document.body
44+
class TestClassElement extends r2wc(TestClassComponent, {}, {
45+
mount,
46+
unmount,
47+
}) {}
2848

49+
customElements.define("test-class-element", TestClassElement)
50+
51+
const testEl = new TestClassElement()
52+
53+
body.appendChild(testEl)
54+
55+
expect(mount).toBeCalledTimes(1)
56+
57+
body.removeChild(testEl)
58+
59+
expect(unmount).toBeCalledTimes(1)
60+
})
2961
})

src/core/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import React from "react"
3+
import { ComponentClass, FC, R2WCOptions, Renderer } from "../types"
34

45
const renderSymbol = Symbol.for("r2wc.reactRender")
56
const shouldRenderSymbol = Symbol.for("r2wc.shouldRender")

src/legacy/react-to-webcomponent.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
import {
2+
ComponentClass,
3+
Container,
4+
FC,
5+
R2WCOptions,
6+
ReactElement,
7+
RefObject,
8+
} from "../types"
9+
110
/* eslint-disable @typescript-eslint/no-explicit-any */
211
const renderSymbol = Symbol.for("r2wc.reactRender")
312
const shouldRenderSymbol = Symbol.for("r2wc.shouldRender")

0 commit comments

Comments
 (0)