Skip to content

Commit cd6b459

Browse files
authored
Fix unit tests and add GitHub action (#4107)
1 parent a2a21a5 commit cd6b459

File tree

6 files changed

+65
-11
lines changed

6 files changed

+65
-11
lines changed

.github/workflows/unit-tests.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Unit Tests
2+
on: [pull_request]
3+
4+
jobs:
5+
jest:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v4
9+
- uses: ./.github/workflows/yarn
10+
- name: Run unit tests
11+
run: yarn test:unit

src/__tests__/draggable.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
import { makeDraggable } from '../draggable.js'
22

33
describe('makeDraggable', () => {
4+
beforeAll(() => {
5+
Object.defineProperty(window, 'matchMedia', {
6+
writable: true,
7+
value: jest.fn().mockReturnValue({
8+
matches: false,
9+
addListener: jest.fn(),
10+
removeListener: jest.fn(),
11+
}),
12+
})
13+
if (typeof window.PointerEvent === 'undefined') {
14+
class FakePointerEvent extends MouseEvent {
15+
constructor(type: string, props: any) {
16+
super(type, props)
17+
}
18+
}
19+
// @ts-ignore
20+
window.PointerEvent = FakePointerEvent
21+
// @ts-ignore
22+
global.PointerEvent = FakePointerEvent
23+
}
24+
})
425
test('invokes callbacks on drag', () => {
526
const el = document.createElement('div')
627
document.body.appendChild(el)

src/__tests__/fetcher.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
import Fetcher from '../fetcher.js'
2+
import { TextEncoder } from 'util'
3+
import { Blob as NodeBlob } from 'buffer'
24

35
describe('Fetcher', () => {
46
test('fetchBlob returns blob and reports progress', async () => {
57
const data = 'hello'
6-
const response = new Response(data, {
7-
headers: { 'Content-Length': data.length.toString() },
8-
})
8+
const reader = {
9+
read: jest
10+
.fn()
11+
.mockResolvedValueOnce({ done: false, value: new TextEncoder().encode(data) })
12+
.mockResolvedValueOnce({ done: true, value: undefined }),
13+
}
14+
const response = {
15+
status: 200,
16+
statusText: 'OK',
17+
headers: new Headers({ 'Content-Length': data.length.toString() }),
18+
body: { getReader: () => reader },
19+
clone() {
20+
return this
21+
},
22+
blob: async () => new NodeBlob([data]),
23+
} as unknown as Response
24+
925
global.fetch = jest.fn().mockResolvedValue(response)
1026

1127
const progress = jest.fn()

src/__tests__/timer.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ describe('Timer', () => {
55
const timer = new Timer()
66
const tick = jest.fn()
77
timer.on('tick', tick)
8-
global.requestAnimationFrame = (cb: FrameRequestCallback) => {
9-
cb(0)
10-
return 1
11-
}
8+
const raf = jest
9+
.fn()
10+
.mockImplementationOnce((cb: FrameRequestCallback) => {
11+
cb(0)
12+
return 1
13+
})
14+
.mockImplementation(() => 1)
15+
global.requestAnimationFrame = raf
1216
timer.start()
1317
expect(tick).toHaveBeenCalledTimes(2)
1418
})

src/dom.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ function renderNode(tagName: string, content: TreeNode): HTMLElement | SVGElemen
1212

1313
for (const [key, value] of Object.entries(content)) {
1414
if (key === 'children') {
15-
for (const [key, value] of Object.entries(content)) {
16-
if (typeof value === 'string') {
17-
element.appendChild(document.createTextNode(value))
15+
const children = value as TreeNode
16+
for (const [childTag, childValue] of Object.entries(children)) {
17+
if (typeof childValue === 'string') {
18+
element.appendChild(document.createTextNode(childValue))
1819
} else {
19-
element.appendChild(renderNode(key, value as TreeNode))
20+
element.appendChild(renderNode(childTag, childValue as TreeNode))
2021
}
2122
}
2223
} else if (key === 'style') {

src/timer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Timer extends EventEmitter<TimerEvents> {
1818

1919
stop() {
2020
this.unsubscribe()
21+
this.unAll()
2122
}
2223

2324
destroy() {

0 commit comments

Comments
 (0)