Skip to content

Commit e136f8a

Browse files
committed
Test importing and separete jsdom setup
1 parent a8b65df commit e136f8a

File tree

6 files changed

+34
-40
lines changed

6 files changed

+34
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
"scripts": {
3939
"test:ssr": "node --experimental-strip-types --import solid-node-register --test tests/ssr.test.tsx",
40-
"test:client": "node --experimental-strip-types --conditions=browser --import solid-node-register/client --test tests/client.test.tsx",
40+
"test:client": "node --experimental-strip-types --conditions=browser --import solid-node-register/client --import ./tests/jsdom.ts --test tests/client.test.tsx",
4141
"build": "tsc --build",
4242
"prepublishOnly": "pnpm run build"
4343
},

tests/client.test.tsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
11
import * as test from 'node:test'
22
import * as assert from 'node:assert/strict'
3-
import * as s from 'solid-js'
43
import * as sw from 'solid-js/web'
5-
import * as jsdom from 'jsdom'
64

7-
// Setup JSDOM environment
8-
const dom = new jsdom.JSDOM('<!DOCTYPE html><div id="app"></div>')
9-
global.window = dom.window as any
10-
global.document = dom.window.document
11-
global.DocumentFragment = dom.window.DocumentFragment
5+
import {TestComponent} from './component.tsx'
126

137
const app = document.getElementById('app')!
148

159
test.test('Solid Client-side Rendering', () => {
1610

17-
const [count, setCount] = s.createSignal(0)
11+
const dispose = sw.render(() => <TestComponent message='Hello Client!'/>, app)
1812

19-
let counter!: HTMLParagraphElement
20-
21-
const dispose = sw.render(() => <>
22-
<div>
23-
<h1>Client Test</h1>
24-
<p ref={counter}>Count: {count()}</p>
25-
</div>
26-
</>, app)
27-
28-
assert.equal(app.querySelector('h1')!.textContent, 'Client Test', 'Should render h1 with correct text')
13+
assert.equal(app.querySelector('h1')!.textContent, 'Hello Client!', 'Should render h1 with correct text')
2914

15+
const counter = app.querySelector('button')!;
3016
assert.equal(counter.textContent, 'Count: 0', 'Should render initial count value')
3117

32-
setCount(1)
18+
counter.click();
3319

34-
assert.equal(counter.textContent, 'Count: 1', 'Count should increment after setting count to 1')
20+
assert.equal(counter.textContent, 'Count: 1', 'Count should increment after button click')
3521

3622
dispose()
3723
})

tests/component.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as s from 'solid-js'
2+
3+
export const TestComponent = (props: { message: string }) => {
4+
const [count, setCount] = s.createSignal(0)
5+
6+
return (
7+
<div>
8+
<h1>{props.message}</h1>
9+
<button onClick={() => setCount(count() + 1)}>
10+
Count: {count()}
11+
</button>
12+
</div>
13+
)
14+
}

tests/jsdom.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as jsdom from 'jsdom'
2+
3+
// Setup JSDOM environment
4+
const dom = new jsdom.JSDOM('<!DOCTYPE html><div id="app"></div>')
5+
global.window = dom.window as any
6+
global.document = dom.window.document
7+
global.DocumentFragment = dom.window.DocumentFragment

tests/ssr.test.tsx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
import * as test from 'node:test'
22
import * as assert from 'node:assert/strict'
3-
import * as s from 'solid-js'
43
import * as sw from 'solid-js/web'
54

6-
test.test('Solid SSR', () => {
7-
8-
const TestComponent = (props: { message: string }) => {
9-
const [count] = s.createSignal(0)
5+
import {TestComponent} from './component.tsx'
106

11-
return (
12-
<div>
13-
<h1>SSR Test</h1>
14-
<p>{props.message}</p>
15-
<button>
16-
Count: {count()}
17-
</button>
18-
</div>
19-
)
20-
}
7+
test.test('Solid SSR', () => {
218

229
let html = sw.renderToString(() => (
2310
<TestComponent message="Hello from SSR!" />
2411
))
2512

26-
assert.ok(html.includes('SSR Test'), 'Should contain the component title')
2713
assert.ok(html.includes('Hello from SSR!'), 'Should contain the message prop')
2814
assert.ok(html.includes('Count: 0'), 'Should contain the initial signal value')
2915
assert.ok(html.startsWith('<div>'), 'Should start with opening div tag')

tests/tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"noEmit": true,
55
"jsx": "preserve",
66
"jsxImportSource": "solid-js",
7-
"rootDir": "."
7+
"rootDir": ".",
8+
"allowImportingTsExtensions": true
89
},
9-
"include": ["."],
10-
}
10+
"include": [".", "component/.tsx"],
11+
}

0 commit comments

Comments
 (0)