@rokkit/helpers 1.0.0-next.119 → 1.0.0-next.120
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -1
- package/spec/components/MockItem.spec.svelte.js +0 -28
- package/spec/components/StaticContent.spec.svelte.js +0 -10
- package/spec/components/__snapshots__/MockItem.spec.svelte.js.snap +0 -29
- package/spec/components/__snapshots__/StaticContent.spec.svelte.js.snap +0 -10
- package/spec/components/index.spec.js +0 -9
- package/spec/index.spec.js +0 -14
- package/spec/matchers/action.spec.js +0 -102
- package/spec/matchers/array.spec.js +0 -33
- package/spec/matchers/dataset.spec.js +0 -32
- package/spec/matchers/event.spec.js +0 -35
- package/spec/matchers/index.spec.js +0 -15
- package/spec/mocks/animate.spec.js +0 -24
- package/spec/mocks/element.spec.js +0 -173
- package/spec/mocks/index.spec.js +0 -17
- package/spec/mocks/match-media.spec.js +0 -75
- package/spec/mocks/resize-observer.spec.js +0 -52
- package/spec/simulator.spec.js +0 -96
- package/tsconfig.build.json +0 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rokkit/helpers",
|
|
3
|
-
"version": "1.0.0-next.
|
|
3
|
+
"version": "1.0.0-next.120",
|
|
4
4
|
"description": "Custom matchers for vitest, mocks and simulators for testing.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -14,6 +14,13 @@
|
|
|
14
14
|
"author": "Jerry Thomas<me@jerrythomas.name>",
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"type": "module",
|
|
17
|
+
"files": [
|
|
18
|
+
"src/**/*.js",
|
|
19
|
+
"src/**/*.svelte",
|
|
20
|
+
"dist/**/*.d.ts",
|
|
21
|
+
"README.md",
|
|
22
|
+
"package.json"
|
|
23
|
+
],
|
|
17
24
|
"exports": {
|
|
18
25
|
"./package.json": "./package.json",
|
|
19
26
|
"./mocks": "./src/mocks/index.js",
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { render } from '@testing-library/svelte'
|
|
3
|
-
import MockItem from '../../src/components/MockItem.svelte'
|
|
4
|
-
import { flushSync } from 'svelte'
|
|
5
|
-
|
|
6
|
-
describe('MockItem', () => {
|
|
7
|
-
it('should render a string', () => {
|
|
8
|
-
const props = $state({ value: 'Hello' })
|
|
9
|
-
const { container } = render(MockItem, { props })
|
|
10
|
-
expect(container).toMatchSnapshot()
|
|
11
|
-
|
|
12
|
-
props.value = 'World'
|
|
13
|
-
flushSync()
|
|
14
|
-
|
|
15
|
-
expect(container).toMatchSnapshot()
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
it('should render an object', () => {
|
|
19
|
-
const props = $state({ value: { text: 'Hello' } })
|
|
20
|
-
|
|
21
|
-
const { container } = render(MockItem, { props })
|
|
22
|
-
expect(container).toMatchSnapshot()
|
|
23
|
-
props.value = { text: 'World' }
|
|
24
|
-
flushSync()
|
|
25
|
-
|
|
26
|
-
expect(container).toMatchSnapshot()
|
|
27
|
-
})
|
|
28
|
-
})
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { render } from '@testing-library/svelte'
|
|
3
|
-
import StaticContent from '../../src/components/StaticContent.svelte'
|
|
4
|
-
|
|
5
|
-
describe('StaticContent', () => {
|
|
6
|
-
it('renders the content', () => {
|
|
7
|
-
const { container } = render(StaticContent)
|
|
8
|
-
expect(container).toMatchSnapshot()
|
|
9
|
-
})
|
|
10
|
-
})
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
-
|
|
3
|
-
exports[`MockItem > should render a string 1`] = `
|
|
4
|
-
<div>
|
|
5
|
-
Hello
|
|
6
|
-
|
|
7
|
-
</div>
|
|
8
|
-
`;
|
|
9
|
-
|
|
10
|
-
exports[`MockItem > should render a string 2`] = `
|
|
11
|
-
<div>
|
|
12
|
-
World
|
|
13
|
-
|
|
14
|
-
</div>
|
|
15
|
-
`;
|
|
16
|
-
|
|
17
|
-
exports[`MockItem > should render an object 1`] = `
|
|
18
|
-
<div>
|
|
19
|
-
Hello
|
|
20
|
-
|
|
21
|
-
</div>
|
|
22
|
-
`;
|
|
23
|
-
|
|
24
|
-
exports[`MockItem > should render an object 2`] = `
|
|
25
|
-
<div>
|
|
26
|
-
World
|
|
27
|
-
|
|
28
|
-
</div>
|
|
29
|
-
`;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
// skipcq: JS-C1003 - Importing all components for verification
|
|
3
|
-
import * as components from '../../src/components/index'
|
|
4
|
-
|
|
5
|
-
describe('components', () => {
|
|
6
|
-
it('should contain all exported components', () => {
|
|
7
|
-
expect(Object.keys(components)).toEqual(['MockItem', 'StaticContent'])
|
|
8
|
-
})
|
|
9
|
-
})
|
package/spec/index.spec.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
// skipcq: JS-C1003 - Importing all components for verification
|
|
3
|
-
import * as functions from '../src'
|
|
4
|
-
|
|
5
|
-
describe('functions', () => {
|
|
6
|
-
it('should contain all exported functions', () => {
|
|
7
|
-
expect(Object.keys(functions)).toEqual([
|
|
8
|
-
'simulateMouseEvent',
|
|
9
|
-
'simulateTouchEvent',
|
|
10
|
-
'simulateTouchSwipe',
|
|
11
|
-
'simulateMouseSwipe'
|
|
12
|
-
])
|
|
13
|
-
})
|
|
14
|
-
})
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
2
|
-
import { toOnlyTrigger, toUseHandlersFor } from '../../src/matchers/action'
|
|
3
|
-
|
|
4
|
-
describe('action', () => {
|
|
5
|
-
describe('toOnlyTrigger', () => {
|
|
6
|
-
const handler = {
|
|
7
|
-
a: vi.fn(),
|
|
8
|
-
b: vi.fn(),
|
|
9
|
-
c: vi.fn()
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
expect.extend({ toOnlyTrigger })
|
|
13
|
-
afterEach(() => {
|
|
14
|
-
vi.resetAllMocks()
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
it('should pass if only the specified events are triggered', () => {
|
|
18
|
-
let result = toOnlyTrigger(handler, 'a')
|
|
19
|
-
expect(result.pass).toBe(false)
|
|
20
|
-
expect(result.message()).toBe(
|
|
21
|
-
'Expected only [a] to be called once and the other handlers to not be called'
|
|
22
|
-
)
|
|
23
|
-
expect(handler).not.toOnlyTrigger('a')
|
|
24
|
-
|
|
25
|
-
handler.a()
|
|
26
|
-
result = toOnlyTrigger(handler, 'a')
|
|
27
|
-
expect(result.pass).toBe(true)
|
|
28
|
-
expect(result.message()).toBe(
|
|
29
|
-
'Expected other handlers besides [a] to be called, but none were'
|
|
30
|
-
)
|
|
31
|
-
expect(handler).toOnlyTrigger('a')
|
|
32
|
-
|
|
33
|
-
handler.b()
|
|
34
|
-
result = toOnlyTrigger(handler, ['a', 'b'])
|
|
35
|
-
expect(result.pass).toBe(true)
|
|
36
|
-
expect(result.message()).toBe(
|
|
37
|
-
'Expected other handlers besides [a, b] to be called, but none were'
|
|
38
|
-
)
|
|
39
|
-
expect(handler).toOnlyTrigger(['a', 'b'])
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
it('should fail if no/other events are triggered', () => {
|
|
43
|
-
let result = toOnlyTrigger(handler, ['a', 'b'])
|
|
44
|
-
expect(result.pass).toBe(false)
|
|
45
|
-
|
|
46
|
-
handler.c()
|
|
47
|
-
result = toOnlyTrigger(handler, ['a', 'b'])
|
|
48
|
-
expect(result.pass).toBe(false)
|
|
49
|
-
expect(result.message()).toBe(
|
|
50
|
-
'Expected only [a, b] to be called once and the other handlers to not be called'
|
|
51
|
-
)
|
|
52
|
-
expect(handler).not.toOnlyTrigger(['a', 'b'])
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('should fail if event is not in handler', () => {
|
|
56
|
-
const result = toOnlyTrigger(handler, ['x'])
|
|
57
|
-
expect(result.pass).toBe(false)
|
|
58
|
-
expect(result.message()).toBe('Expected events from [a, b, c] but got unexpected events [x]')
|
|
59
|
-
expect(handler).not.toOnlyTrigger(['a', 'b'])
|
|
60
|
-
})
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
describe('toUseHandlersFor', () => {
|
|
64
|
-
const mockAction = (node, options) => {
|
|
65
|
-
const handler = vi.fn()
|
|
66
|
-
node.addEventListener('click', handler)
|
|
67
|
-
if (options.touch) node.addEventListener('touchstart', handler)
|
|
68
|
-
return {
|
|
69
|
-
destroy: () => {
|
|
70
|
-
node.removeEventListener('click', handler)
|
|
71
|
-
if (options.touch) node.removeEventListener('touchstart', handler)
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
expect.extend({ toUseHandlersFor })
|
|
76
|
-
|
|
77
|
-
it('should pass if all events are registered and cleanued up', () => {
|
|
78
|
-
let result = toUseHandlersFor(mockAction, {}, 'click')
|
|
79
|
-
expect(result.pass).toBe(true)
|
|
80
|
-
expect(result.message()).toBe(
|
|
81
|
-
'Expected action not to manage handlers for [click] but result is [{"event":"click","created":true,"destroyed":true,"pass":true}]'
|
|
82
|
-
)
|
|
83
|
-
expect(mockAction).toUseHandlersFor({}, 'click')
|
|
84
|
-
|
|
85
|
-
result = toUseHandlersFor(mockAction, { touch: true }, ['click', 'touchstart'])
|
|
86
|
-
expect(result.pass).toBe(true)
|
|
87
|
-
expect(result.message()).toBe(
|
|
88
|
-
'Expected action not to manage handlers for [click,touchstart] but result is [{"event":"click","created":true,"destroyed":true,"pass":true},{"event":"touchstart","created":true,"destroyed":true,"pass":true}]'
|
|
89
|
-
)
|
|
90
|
-
expect(mockAction).toUseHandlersFor({ touch: true }, ['click', 'touchstart'])
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
it('should fail if all events are not registered or not cleaned up', () => {
|
|
94
|
-
const result = toUseHandlersFor(mockAction, {}, ['click', 'touchstart'])
|
|
95
|
-
expect(result.pass).toBe(false)
|
|
96
|
-
expect(result.message()).toBe(
|
|
97
|
-
'Expected action to manage handlers for [click,touchstart] but result is [{"event":"click","created":true,"destroyed":true,"pass":true},{"event":"touchstart","created":false,"destroyed":true,"pass":false}]'
|
|
98
|
-
)
|
|
99
|
-
expect(mockAction).not.toUseHandlersFor({}, ['click', 'touchstart'])
|
|
100
|
-
})
|
|
101
|
-
})
|
|
102
|
-
})
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
2
|
-
import { toIncludeAll } from '../../src/matchers/array'
|
|
3
|
-
|
|
4
|
-
describe('event', () => {
|
|
5
|
-
describe('toIncludeAll', () => {
|
|
6
|
-
expect.extend({ toIncludeAll })
|
|
7
|
-
afterEach(() => {
|
|
8
|
-
vi.resetAllMocks()
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
it('should pass if the array includes all the values', () => {
|
|
12
|
-
const input = [1, 2, 3]
|
|
13
|
-
const expected = [1, 2]
|
|
14
|
-
const result = toIncludeAll(input, expected)
|
|
15
|
-
expect(result.pass).toBe(true)
|
|
16
|
-
expect(result.message()).toBe(
|
|
17
|
-
`expected ${JSON.stringify(input)} to not include all of ${JSON.stringify(expected)}`
|
|
18
|
-
)
|
|
19
|
-
expect(input).toIncludeAll(expected)
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it('should fail if the array does not include all the values', () => {
|
|
23
|
-
const input = [1, 2, 3]
|
|
24
|
-
const expected = [1, 2, 4]
|
|
25
|
-
const result = toIncludeAll(input, expected)
|
|
26
|
-
expect(result.pass).toBe(false)
|
|
27
|
-
expect(result.message()).toBe(
|
|
28
|
-
`expected ${JSON.stringify(input)} to include all of ${JSON.stringify(expected)}`
|
|
29
|
-
)
|
|
30
|
-
expect(input).not.toIncludeAll(expected)
|
|
31
|
-
})
|
|
32
|
-
})
|
|
33
|
-
})
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
2
|
-
import { toHaveValidData } from '../../src/matchers/dataset'
|
|
3
|
-
|
|
4
|
-
describe('event', () => {
|
|
5
|
-
describe('toHaveValidData', () => {
|
|
6
|
-
expect.extend({ toHaveValidData })
|
|
7
|
-
afterEach(() => {
|
|
8
|
-
vi.resetAllMocks()
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
it('should pass if the element dataset matches expected', () => {
|
|
12
|
-
const input = { dataset: { a: 1, b: 2 } }
|
|
13
|
-
const expected = { a: 1, b: 2 }
|
|
14
|
-
const result = toHaveValidData(input, expected)
|
|
15
|
-
expect(result.pass).toBe(true)
|
|
16
|
-
expect(result.message()).toBe(
|
|
17
|
-
`expected ${JSON.stringify(input.dataset)} to not deeply equal ${JSON.stringify(expected)}`
|
|
18
|
-
)
|
|
19
|
-
expect(input).toHaveValidData(expected)
|
|
20
|
-
})
|
|
21
|
-
it('should fail if the element dataset does not match expected', () => {
|
|
22
|
-
const input = { dataset: { a: 1, b: 2 } }
|
|
23
|
-
const expected = { a: 1, b: 3 }
|
|
24
|
-
const result = toHaveValidData(input, expected)
|
|
25
|
-
expect(result.pass).toBe(false)
|
|
26
|
-
expect(result.message()).toBe(
|
|
27
|
-
`expected ${JSON.stringify(input.dataset)} to deeply equal ${JSON.stringify(expected)}`
|
|
28
|
-
)
|
|
29
|
-
expect(input).not.toHaveValidData(expected)
|
|
30
|
-
})
|
|
31
|
-
})
|
|
32
|
-
})
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
2
|
-
import { toHaveBeenDispatchedWith } from '../../src/matchers/event'
|
|
3
|
-
|
|
4
|
-
describe('event', () => {
|
|
5
|
-
describe('toHaveBeenDispatchedWith', () => {
|
|
6
|
-
const detail = { foo: 'bar' }
|
|
7
|
-
const spy = vi.fn()
|
|
8
|
-
|
|
9
|
-
expect.extend({ toHaveBeenDispatchedWith })
|
|
10
|
-
afterEach(() => {
|
|
11
|
-
vi.resetAllMocks()
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
it('should pass if the event was dispatched with the correct data', () => {
|
|
15
|
-
spy({ detail })
|
|
16
|
-
const result = toHaveBeenDispatchedWith(spy, detail)
|
|
17
|
-
|
|
18
|
-
expect(result.pass).toBe(true)
|
|
19
|
-
expect(result.message()).toBe(
|
|
20
|
-
`expected ${JSON.stringify(detail)} to not deeply equal ${JSON.stringify(detail)}`
|
|
21
|
-
)
|
|
22
|
-
expect(spy).toHaveBeenDispatchedWith(detail)
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('should fail if the event was dispatched with the incorrect data', () => {
|
|
26
|
-
spy({ detail: 'baz' })
|
|
27
|
-
const result = toHaveBeenDispatchedWith(spy, detail)
|
|
28
|
-
expect(result.pass).toBe(false)
|
|
29
|
-
expect(result.message()).toBe(
|
|
30
|
-
`expected ${JSON.stringify('baz')} to deeply equal ${JSON.stringify(detail)}`
|
|
31
|
-
)
|
|
32
|
-
expect(spy).not.toHaveBeenDispatchedWith(detail)
|
|
33
|
-
})
|
|
34
|
-
})
|
|
35
|
-
})
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
// skipcq: JS-C1003 - Importing all components for verification
|
|
3
|
-
import * as validators from '../../src/matchers/index'
|
|
4
|
-
|
|
5
|
-
describe('validators', () => {
|
|
6
|
-
it('should contain all exported validators', () => {
|
|
7
|
-
expect(Object.keys(validators)).toEqual([
|
|
8
|
-
'toIncludeAll',
|
|
9
|
-
'toUseHandlersFor',
|
|
10
|
-
'toOnlyTrigger',
|
|
11
|
-
'toHaveValidData',
|
|
12
|
-
'toHaveBeenDispatchedWith'
|
|
13
|
-
])
|
|
14
|
-
})
|
|
15
|
-
})
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import '../../src/mocks/animate'
|
|
3
|
-
|
|
4
|
-
describe('animate', () => {
|
|
5
|
-
it('should mock global.Element.prototype.animate', () => {
|
|
6
|
-
// Verify the mock is attached
|
|
7
|
-
expect(typeof global.Element.prototype.animate).toBe('function')
|
|
8
|
-
|
|
9
|
-
// Create an element and call animate
|
|
10
|
-
const el = document.createElement('div')
|
|
11
|
-
const animation = el.animate([{ opacity: 0 }, { opacity: 1 }], 1000)
|
|
12
|
-
|
|
13
|
-
// Check that animate was called
|
|
14
|
-
expect(global.Element.prototype.animate).toHaveBeenCalledTimes(1)
|
|
15
|
-
|
|
16
|
-
// animation should be the mock return object
|
|
17
|
-
expect(animation).toHaveProperty('play')
|
|
18
|
-
expect(animation.play).toBeInstanceOf(Function)
|
|
19
|
-
|
|
20
|
-
// Optional: you could further exercise the mock
|
|
21
|
-
animation.play()
|
|
22
|
-
expect(animation.play).toHaveBeenCalledTimes(1)
|
|
23
|
-
})
|
|
24
|
-
})
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
|
-
import { describe, it, expect, vi } from 'vitest'
|
|
3
|
-
import {
|
|
4
|
-
getMockNode,
|
|
5
|
-
createNestedElement,
|
|
6
|
-
elementsWithSize,
|
|
7
|
-
mixedSizeElements,
|
|
8
|
-
mockFormRequestSubmit
|
|
9
|
-
} from '../../src/mocks/element'
|
|
10
|
-
|
|
11
|
-
describe('element', () => {
|
|
12
|
-
describe('getMockNode', () => {
|
|
13
|
-
it('should return a mock node', () => {
|
|
14
|
-
const node = getMockNode(['click'])
|
|
15
|
-
expect(node).toEqual({
|
|
16
|
-
node: {
|
|
17
|
-
scrollTo: expect.any(Function),
|
|
18
|
-
querySelector: expect.any(Function),
|
|
19
|
-
querySelectorAll: expect.any(Function),
|
|
20
|
-
dispatchEvent: expect.any(Function),
|
|
21
|
-
addEventListener: expect.any(Function),
|
|
22
|
-
removeEventListener: expect.any(Function)
|
|
23
|
-
},
|
|
24
|
-
listeners: { click: 0 }
|
|
25
|
-
})
|
|
26
|
-
})
|
|
27
|
-
it('should get a mock node for querySelector', () => {
|
|
28
|
-
const { node } = getMockNode(['click'])
|
|
29
|
-
|
|
30
|
-
expect(typeof node.querySelector('div')).toEqual('object')
|
|
31
|
-
expect(Array.isArray(node.querySelectorAll('div'))).toBeTruthy()
|
|
32
|
-
})
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
describe('createNestedElement', () => {
|
|
36
|
-
it('should return a mock nested node', () => {
|
|
37
|
-
const item = {
|
|
38
|
-
name: 'div'
|
|
39
|
-
}
|
|
40
|
-
const node = createNestedElement(item)
|
|
41
|
-
expect(node.getAttribute('id')).toBeFalsy()
|
|
42
|
-
expect(node.getAttribute('data-path')).toBeFalsy()
|
|
43
|
-
expect(node.scrollIntoView).toEqual(expect.any(Function))
|
|
44
|
-
expect(node.children.length).toBe(0)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
it('should return a mock nested node with id', () => {
|
|
48
|
-
const item = {
|
|
49
|
-
name: 'div',
|
|
50
|
-
id: 'foo'
|
|
51
|
-
}
|
|
52
|
-
const node = createNestedElement(item)
|
|
53
|
-
expect(node.getAttribute('id')).toBe('foo')
|
|
54
|
-
expect(node.getAttribute('data-path')).toBeFalsy()
|
|
55
|
-
expect(node.scrollIntoView).toEqual(expect.any(Function))
|
|
56
|
-
expect(node.children.length).toBe(0)
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
it('should return a mock nested node with data-path', () => {
|
|
60
|
-
const item = {
|
|
61
|
-
name: 'div',
|
|
62
|
-
dataPath: 'foo'
|
|
63
|
-
}
|
|
64
|
-
const node = createNestedElement(item)
|
|
65
|
-
expect(node.getAttribute('id')).toBeFalsy()
|
|
66
|
-
expect(node.getAttribute('data-path')).toBe('foo')
|
|
67
|
-
expect(node.scrollIntoView).toEqual(expect.any(Function))
|
|
68
|
-
expect(node.children.length).toBe(0)
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
it('should return a mock nested node with children', () => {
|
|
72
|
-
const item = {
|
|
73
|
-
name: 'div',
|
|
74
|
-
children: [
|
|
75
|
-
{
|
|
76
|
-
name: 'div'
|
|
77
|
-
}
|
|
78
|
-
]
|
|
79
|
-
}
|
|
80
|
-
const node = createNestedElement(item)
|
|
81
|
-
expect(node.getAttribute('id')).toBeFalsy()
|
|
82
|
-
expect(node.getAttribute('data-path')).toBeFalsy()
|
|
83
|
-
expect(node.scrollIntoView).toEqual(expect.any(Function))
|
|
84
|
-
expect(node.children.length).toBe(1)
|
|
85
|
-
})
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
describe('elementsWithSize', () => {
|
|
89
|
-
it('should return an array of elements with offsetHeight property', () => {
|
|
90
|
-
const result = elementsWithSize(10, 20)
|
|
91
|
-
expect(result.length).toBe(10)
|
|
92
|
-
result.forEach((element) => {
|
|
93
|
-
expect(element.offsetHeight).toBe(20)
|
|
94
|
-
})
|
|
95
|
-
})
|
|
96
|
-
it('should return an array of elements with offsetWidth property', () => {
|
|
97
|
-
const result = elementsWithSize(10, 20, 'offsetWidth')
|
|
98
|
-
expect(result.length).toBe(10)
|
|
99
|
-
result.forEach((element) => {
|
|
100
|
-
expect(element.offsetWidth).toBe(20)
|
|
101
|
-
})
|
|
102
|
-
})
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
describe('mixedSizeElements', () => {
|
|
106
|
-
it('should create an array of mixed size elements', () => {
|
|
107
|
-
const result = mixedSizeElements([
|
|
108
|
-
{ count: 10, size: 20 },
|
|
109
|
-
{ count: 5, size: 10 }
|
|
110
|
-
])
|
|
111
|
-
expect(result.length).toBe(15)
|
|
112
|
-
result.forEach((element, index) => {
|
|
113
|
-
if (index < 10) {
|
|
114
|
-
expect(element.offsetHeight).toBe(20)
|
|
115
|
-
} else {
|
|
116
|
-
expect(element.offsetHeight).toBe(10)
|
|
117
|
-
}
|
|
118
|
-
})
|
|
119
|
-
})
|
|
120
|
-
it('should create an array of mixed size elements with offsetWidth', () => {
|
|
121
|
-
const result = mixedSizeElements(
|
|
122
|
-
[
|
|
123
|
-
{ count: 10, size: 20 },
|
|
124
|
-
{ count: 5, size: 10 }
|
|
125
|
-
],
|
|
126
|
-
'offsetWidth'
|
|
127
|
-
)
|
|
128
|
-
expect(result.length).toBe(15)
|
|
129
|
-
result.forEach((element, index) => {
|
|
130
|
-
if (index < 10) {
|
|
131
|
-
expect(element.offsetWidth).toBe(20)
|
|
132
|
-
} else {
|
|
133
|
-
expect(element.offsetWidth).toBe(10)
|
|
134
|
-
}
|
|
135
|
-
})
|
|
136
|
-
})
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
describe('mockFormRequestSubmit', () => {
|
|
140
|
-
it('should detect unimplemented requestSubmit before mocking', () => {
|
|
141
|
-
console.error = vi.fn()
|
|
142
|
-
// Create a form and try to use requestSubmit
|
|
143
|
-
const form = document.createElement('form')
|
|
144
|
-
form.requestSubmit()
|
|
145
|
-
expect(console.error).toHaveBeenCalled()
|
|
146
|
-
expect(console.error.mock.calls[0][0]).toContain('Not implemented')
|
|
147
|
-
|
|
148
|
-
vi.resetAllMocks()
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
it('should mock form requestSubmit', () => {
|
|
152
|
-
// Apply the mock
|
|
153
|
-
const mockWasApplied = mockFormRequestSubmit()
|
|
154
|
-
|
|
155
|
-
// Verify the function returns true (indicating mock was applied)
|
|
156
|
-
expect(mockWasApplied).toBe(true)
|
|
157
|
-
// eslint-disable-next-line no-undef
|
|
158
|
-
expect(vi.isMockFunction(HTMLFormElement.prototype.requestSubmit)).toBeTruthy()
|
|
159
|
-
|
|
160
|
-
// Test the mock implementation works correctly
|
|
161
|
-
const form = document.createElement('form')
|
|
162
|
-
const submitSpy = vi.fn()
|
|
163
|
-
form.addEventListener('submit', submitSpy)
|
|
164
|
-
|
|
165
|
-
form.requestSubmit()
|
|
166
|
-
expect(submitSpy).toHaveBeenCalled()
|
|
167
|
-
// eslint-disable-next-line no-undef
|
|
168
|
-
expect(HTMLFormElement.prototype.requestSubmit).toHaveBeenCalled()
|
|
169
|
-
|
|
170
|
-
vi.resetAllMocks()
|
|
171
|
-
})
|
|
172
|
-
})
|
|
173
|
-
})
|
package/spec/mocks/index.spec.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
// skipcq: JS-C1003 - Importing all components for verification
|
|
3
|
-
import * as mocks from '../../src/mocks'
|
|
4
|
-
|
|
5
|
-
describe('mocks', () => {
|
|
6
|
-
it('should contain all exported mocks', () => {
|
|
7
|
-
expect(Object.keys(mocks)).toEqual([
|
|
8
|
-
'matchMediaMock',
|
|
9
|
-
'updateMedia',
|
|
10
|
-
'elementsWithSize',
|
|
11
|
-
'mixedSizeElements',
|
|
12
|
-
'getMockNode',
|
|
13
|
-
'createNestedElement',
|
|
14
|
-
'mockFormRequestSubmit'
|
|
15
|
-
])
|
|
16
|
-
})
|
|
17
|
-
})
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { matchMediaMock, updateMedia } from '../../src/mocks/match-media'
|
|
3
|
-
|
|
4
|
-
describe('match-media', () => {
|
|
5
|
-
describe('matchMediaMock', () => {
|
|
6
|
-
it('should return a mock media query', () => {
|
|
7
|
-
const query = matchMediaMock('(min-width: 100px)')
|
|
8
|
-
expect(query.media).toBe('(min-width: 100px)')
|
|
9
|
-
expect(query.matches).toBe(true)
|
|
10
|
-
expect(query.addListener).toEqual(expect.any(Function))
|
|
11
|
-
expect(query.removeListener).toEqual(expect.any(Function))
|
|
12
|
-
})
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
describe('updateMedia', () => {
|
|
16
|
-
it('should mock min-width media query', () => {
|
|
17
|
-
const query = matchMediaMock('(min-width: 100px)')
|
|
18
|
-
|
|
19
|
-
window.innerWidth = 50
|
|
20
|
-
updateMedia()
|
|
21
|
-
expect(query.matches).toBe(false)
|
|
22
|
-
|
|
23
|
-
window.innerWidth = 200
|
|
24
|
-
updateMedia()
|
|
25
|
-
expect(query.matches).toBe(true)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('should mock max-width media query', () => {
|
|
29
|
-
const query = matchMediaMock('(max-width: 100px)')
|
|
30
|
-
|
|
31
|
-
window.innerWidth = 500
|
|
32
|
-
updateMedia()
|
|
33
|
-
expect(query.matches).toBe(false)
|
|
34
|
-
|
|
35
|
-
window.innerWidth = 100
|
|
36
|
-
updateMedia()
|
|
37
|
-
expect(query.matches).toBe(true)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('should mock the media queries', () => {
|
|
41
|
-
const query = matchMediaMock('(min-width: 100px) and (max-width: 200px)')
|
|
42
|
-
|
|
43
|
-
window.innerWidth = 50
|
|
44
|
-
updateMedia()
|
|
45
|
-
expect(query.matches).toBe(false)
|
|
46
|
-
|
|
47
|
-
window.innerWidth = 500
|
|
48
|
-
updateMedia()
|
|
49
|
-
expect(query.matches).toBe(false)
|
|
50
|
-
|
|
51
|
-
window.innerWidth = 150
|
|
52
|
-
updateMedia()
|
|
53
|
-
expect(query.matches).toBe(true)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it('should mock multiple media queries', () => {
|
|
57
|
-
const query1 = matchMediaMock('(min-width: 100px)')
|
|
58
|
-
const query2 = matchMediaMock('(max-width: 100px)')
|
|
59
|
-
|
|
60
|
-
window.innerWidth = 50
|
|
61
|
-
updateMedia()
|
|
62
|
-
expect(query1.matches).toBe(false)
|
|
63
|
-
expect(query2.matches).toBe(true)
|
|
64
|
-
|
|
65
|
-
window.innerWidth = 500
|
|
66
|
-
updateMedia()
|
|
67
|
-
expect(query1.matches).toBe(true)
|
|
68
|
-
expect(query2.matches).toBe(false)
|
|
69
|
-
|
|
70
|
-
// should remove the listener
|
|
71
|
-
query1.removeListener()
|
|
72
|
-
query2.removeListener()
|
|
73
|
-
})
|
|
74
|
-
})
|
|
75
|
-
})
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
|
2
|
-
import { ResizeObserver } from '../../src/mocks/resize-observer'
|
|
3
|
-
|
|
4
|
-
global.ResizeObserver = ResizeObserver
|
|
5
|
-
|
|
6
|
-
describe('ResizeObserver', () => {
|
|
7
|
-
let resizeObserver = null
|
|
8
|
-
let callback = null
|
|
9
|
-
let element = null
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
callback = vi.fn()
|
|
13
|
-
resizeObserver = new ResizeObserver(callback)
|
|
14
|
-
element = document.createElement('div')
|
|
15
|
-
document.body.appendChild(element)
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
it('should observe an element and trigger the callback with initial size', () => {
|
|
19
|
-
resizeObserver.observe(element)
|
|
20
|
-
|
|
21
|
-
expect(callback).toHaveBeenCalledTimes(1)
|
|
22
|
-
const [entry] = callback.mock.calls[0][0]
|
|
23
|
-
expect(entry.target).toBe(element)
|
|
24
|
-
expect(entry.contentRect).toEqual(element.getBoundingClientRect())
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
it('should unobserve an element', () => {
|
|
28
|
-
resizeObserver.observe(element)
|
|
29
|
-
resizeObserver.unobserve(element)
|
|
30
|
-
|
|
31
|
-
expect(resizeObserver.elements.has(element)).toBe(false)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it('should disconnect and clear all observed elements', () => {
|
|
35
|
-
resizeObserver.observe(element)
|
|
36
|
-
resizeObserver.disconnect()
|
|
37
|
-
|
|
38
|
-
expect(resizeObserver.elements.size).toBe(0)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('should simulate a resize event', () => {
|
|
42
|
-
resizeObserver.observe(element)
|
|
43
|
-
const newSize = { width: 500, height: 300, top: 0, left: 0, right: 500, bottom: 300 }
|
|
44
|
-
|
|
45
|
-
resizeObserver.simulateResize(element, newSize)
|
|
46
|
-
|
|
47
|
-
expect(callback).toHaveBeenCalledTimes(2)
|
|
48
|
-
const [entry] = callback.mock.calls[1][0]
|
|
49
|
-
expect(entry.target).toBe(element)
|
|
50
|
-
expect(entry.contentRect).toEqual(newSize)
|
|
51
|
-
})
|
|
52
|
-
})
|
package/spec/simulator.spec.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
2
|
-
import { toHaveBeenDispatchedWith } from '../src/matchers/event'
|
|
3
|
-
import {
|
|
4
|
-
simulateMouseEvent,
|
|
5
|
-
simulateTouchEvent,
|
|
6
|
-
simulateMouseSwipe,
|
|
7
|
-
simulateTouchSwipe
|
|
8
|
-
} from '../src/simulators'
|
|
9
|
-
|
|
10
|
-
expect.extend({ toHaveBeenDispatchedWith })
|
|
11
|
-
describe('events', () => {
|
|
12
|
-
const node = document.createElement('div')
|
|
13
|
-
|
|
14
|
-
describe('simulateMouseEvent', () => {
|
|
15
|
-
it('should simulate a mouse event', () => {
|
|
16
|
-
const event = simulateMouseEvent(10, 10)
|
|
17
|
-
expect(event).toEqual({
|
|
18
|
-
clientX: 10,
|
|
19
|
-
clientY: 10,
|
|
20
|
-
stopPropagation: expect.any(Function),
|
|
21
|
-
preventDefault: expect.any(Function)
|
|
22
|
-
})
|
|
23
|
-
})
|
|
24
|
-
})
|
|
25
|
-
describe('simulateTouchEvent', () => {
|
|
26
|
-
it('should simulate a touch event', () => {
|
|
27
|
-
const event = simulateTouchEvent(10, 10)
|
|
28
|
-
|
|
29
|
-
expect(event).toEqual({
|
|
30
|
-
touches: [{ clientX: 10, clientY: 10 }],
|
|
31
|
-
stopPropagation: expect.any(Function),
|
|
32
|
-
preventDefault: expect.any(Function)
|
|
33
|
-
})
|
|
34
|
-
})
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
describe('simulateMouseSwipe', () => {
|
|
38
|
-
const events = ['mousedown', 'mouseup']
|
|
39
|
-
|
|
40
|
-
const handlers = {}
|
|
41
|
-
|
|
42
|
-
beforeEach(() => {
|
|
43
|
-
vi.useFakeTimers()
|
|
44
|
-
events.forEach((event) => {
|
|
45
|
-
handlers[event] = vi.fn()
|
|
46
|
-
node.addEventListener(event, handlers[event])
|
|
47
|
-
})
|
|
48
|
-
})
|
|
49
|
-
afterEach(() => {
|
|
50
|
-
vi.useRealTimers()
|
|
51
|
-
events.forEach((event) => node.removeEventListener(event, handlers[event]))
|
|
52
|
-
})
|
|
53
|
-
it('should simulate a mouse swipe', () => {
|
|
54
|
-
simulateMouseSwipe(node, { x: 10, y: 20 })
|
|
55
|
-
expect(handlers.mousedown).toHaveBeenCalledOnce()
|
|
56
|
-
expect(handlers.mouseup).toHaveBeenCalledOnce()
|
|
57
|
-
|
|
58
|
-
let mouseEvent = handlers.mousedown.mock.calls[0][0]
|
|
59
|
-
expect(mouseEvent.clientX).toEqual(0)
|
|
60
|
-
expect(mouseEvent.clientY).toEqual(0)
|
|
61
|
-
|
|
62
|
-
mouseEvent = handlers.mouseup.mock.calls[0][0]
|
|
63
|
-
expect(mouseEvent.clientX).toEqual(10)
|
|
64
|
-
expect(mouseEvent.clientY).toEqual(20)
|
|
65
|
-
})
|
|
66
|
-
})
|
|
67
|
-
describe('simulateTouchSwipe', () => {
|
|
68
|
-
const events = ['touchstart', 'touchend']
|
|
69
|
-
|
|
70
|
-
const handlers = {}
|
|
71
|
-
|
|
72
|
-
beforeEach(() => {
|
|
73
|
-
vi.useFakeTimers()
|
|
74
|
-
events.forEach((event) => {
|
|
75
|
-
handlers[event] = vi.fn()
|
|
76
|
-
node.addEventListener(event, handlers[event])
|
|
77
|
-
})
|
|
78
|
-
})
|
|
79
|
-
afterEach(() => {
|
|
80
|
-
events.forEach((event) => node.removeEventListener(event, handlers[event]))
|
|
81
|
-
vi.useRealTimers()
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
it('should simulate a touch swipe', () => {
|
|
85
|
-
simulateTouchSwipe(node, { x: 100, y: 50 })
|
|
86
|
-
expect(handlers.touchstart).toHaveBeenCalledOnce()
|
|
87
|
-
expect(handlers.touchend).toHaveBeenCalledOnce()
|
|
88
|
-
expect(handlers.touchstart.mock.calls[0][0].touches).toEqual([
|
|
89
|
-
{ clientX: 0, clientY: 0, identifier: 0, target: node }
|
|
90
|
-
])
|
|
91
|
-
expect(handlers.touchend.mock.calls[0][0].changedTouches).toEqual([
|
|
92
|
-
{ clientX: 100, clientY: 50, identifier: 0, target: node }
|
|
93
|
-
])
|
|
94
|
-
})
|
|
95
|
-
})
|
|
96
|
-
})
|
package/tsconfig.build.json
DELETED