@rokkit/helpers 1.0.0-next.105 → 1.0.0-next.106
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/dist/matchers/dataset.d.ts +1 -1
- package/dist/simulators/index.d.ts +1 -0
- package/dist/simulators/touch.d.ts +32 -8
- package/dist/src/components/index.d.ts +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/matchers/action.d.ts +27 -0
- package/dist/src/matchers/array.d.ts +10 -0
- package/dist/src/matchers/dataset.d.ts +10 -0
- package/dist/src/matchers/event.d.ts +10 -0
- package/dist/src/matchers/index.d.ts +4 -0
- package/dist/src/matchers/internal.d.ts +1 -0
- package/dist/src/mocks/animate.d.ts +1 -0
- package/dist/src/mocks/element.d.ts +56 -0
- package/dist/src/mocks/index.d.ts +2 -0
- package/dist/src/mocks/match-media.d.ts +30 -0
- package/dist/src/mocks/resize-observer.d.ts +9 -0
- package/dist/src/simulators/touch.d.ts +16 -0
- package/dist/vitest.config.d.ts +1 -0
- package/package.json +2 -2
- package/spec/components/MockItem.spec.svelte.js +21 -3
- package/spec/components/StaticContent.spec.svelte.js +10 -0
- package/spec/components/__snapshots__/MockItem.spec.svelte.js.snap +29 -0
- package/spec/components/__snapshots__/StaticContent.spec.svelte.js.snap +10 -0
- package/spec/components/index.spec.js +1 -1
- package/spec/mocks/animate.spec.js +1 -1
- package/spec/mocks/resize-observer.spec.js +3 -3
- package/spec/simulator.spec.js +1 -1
- package/src/components/MockItem.svelte +3 -2
- package/src/components/StaticContent.svelte +1 -0
- package/src/components/index.js +1 -0
- package/src/index.js +1 -0
- package/src/matchers/index.js +4 -0
- package/src/mocks/index.js +2 -0
- package/src/simulators/index.js +2 -0
- package/src/simulators/touch.js +25 -12
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./touch";
|
|
@@ -1,16 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Simulates a mouse event on a given node.
|
|
3
|
+
* @param {number} x
|
|
4
|
+
* @param {number} y
|
|
5
|
+
* @returns
|
|
6
|
+
*/
|
|
7
|
+
export function simulateMouseEvent(x: number, y: number): {
|
|
8
|
+
clientX: number;
|
|
9
|
+
clientY: number;
|
|
4
10
|
stopPropagation: import("vitest").Mock<(...args: any[]) => any>;
|
|
5
11
|
preventDefault: import("vitest").Mock<(...args: any[]) => any>;
|
|
6
12
|
};
|
|
7
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Simulates a touch event on a given node.
|
|
15
|
+
* @param {number} clientX
|
|
16
|
+
* @param {number} clientY
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
export function simulateTouchEvent(clientX: number, clientY: number): {
|
|
8
20
|
touches: {
|
|
9
|
-
clientX:
|
|
10
|
-
clientY:
|
|
21
|
+
clientX: number;
|
|
22
|
+
clientY: number;
|
|
11
23
|
}[];
|
|
12
24
|
preventDefault: import("vitest").Mock<(...args: any[]) => any>;
|
|
13
25
|
stopPropagation: import("vitest").Mock<(...args: any[]) => any>;
|
|
14
26
|
};
|
|
15
|
-
|
|
16
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Simulates a touch swipe on a given node.
|
|
29
|
+
* @param {HTMLElement} node
|
|
30
|
+
* @param {number} distance
|
|
31
|
+
* @param {number} [delay=0]
|
|
32
|
+
*/
|
|
33
|
+
export function simulateTouchSwipe(node: HTMLElement, distance: number, delay?: number): void;
|
|
34
|
+
/**
|
|
35
|
+
* Simulates a mouse swipe on a given node.
|
|
36
|
+
* @param {HTMLElement} node
|
|
37
|
+
* @param {number} distance
|
|
38
|
+
* @param {number} [delay=0]
|
|
39
|
+
*/
|
|
40
|
+
export function simulateMouseSwipe(node: HTMLElement, distance: number, delay?: number): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./simulators/touch";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if all the given events are registered by the action and cleaned up on destroy.
|
|
3
|
+
*
|
|
4
|
+
* @param {*} action
|
|
5
|
+
* @param {Object<string,any>} options
|
|
6
|
+
* @param {string|Array<string>} events
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export function toUseHandlersFor(action: any, options: {
|
|
10
|
+
[x: string]: any;
|
|
11
|
+
}, events: string | Array<string>): {
|
|
12
|
+
message: () => string;
|
|
13
|
+
pass: boolean;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Verifies that only the specified events are triggered. Expects an object of spies with key as event names.
|
|
17
|
+
*
|
|
18
|
+
* @param {Object<string,any>} handler : Object with keys as event names and values as spies
|
|
19
|
+
* @param {string|Array<string>} events : An event name or an array of event names
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
export function toOnlyTrigger(handler: {
|
|
23
|
+
[x: string]: any;
|
|
24
|
+
}, events: string | Array<string>): {
|
|
25
|
+
message: () => string;
|
|
26
|
+
pass: boolean;
|
|
27
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verify that an array contains all of the expected values
|
|
3
|
+
*
|
|
4
|
+
* @param {Array} received - the array to inspect
|
|
5
|
+
* @param {Array} expected - the values to check for
|
|
6
|
+
*/
|
|
7
|
+
export function toIncludeAll(received: any[], expected: any[]): {
|
|
8
|
+
message: () => string;
|
|
9
|
+
pass: boolean;
|
|
10
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if the element has valid data attributes
|
|
3
|
+
*
|
|
4
|
+
* @param {HTMLElement} received - HTML element to be checked
|
|
5
|
+
* @param {Object} expected - data to be compared
|
|
6
|
+
*/
|
|
7
|
+
export function toHaveValidData(received: HTMLElement, expected: Object): {
|
|
8
|
+
message: () => string;
|
|
9
|
+
pass: any;
|
|
10
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verify that a spy event has been dispatched with data in event detail
|
|
3
|
+
*
|
|
4
|
+
* @param {Function} spy - the event handler to inspect
|
|
5
|
+
* @param {Object} data - data expected inthe event detail
|
|
6
|
+
*/
|
|
7
|
+
export function toHaveBeenDispatchedWith(spy: Function, data: Object): {
|
|
8
|
+
message: () => string;
|
|
9
|
+
pass: boolean;
|
|
10
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function getMessage(actual: any, expected: any, pass: any, condition?: string): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array of elements with the specified size
|
|
3
|
+
*
|
|
4
|
+
* @param {number} count
|
|
5
|
+
* @param {number} size
|
|
6
|
+
* @param {string} [prop='offsetHeight']
|
|
7
|
+
* @returns {Array<Object<string, number>>}
|
|
8
|
+
*/
|
|
9
|
+
export function elementsWithSize(count: number, size: number, prop?: string): Array<{
|
|
10
|
+
[x: string]: number;
|
|
11
|
+
}>;
|
|
12
|
+
/**
|
|
13
|
+
* Creates an array of elements with mixed sizes
|
|
14
|
+
*
|
|
15
|
+
* @param {Array<{count: number, size: number}>} data
|
|
16
|
+
* @param {string} prop
|
|
17
|
+
* @returns {Array<Object<string, number>>}
|
|
18
|
+
*/
|
|
19
|
+
export function mixedSizeElements(data: Array<{
|
|
20
|
+
count: number;
|
|
21
|
+
size: number;
|
|
22
|
+
}>, prop: string): Array<{
|
|
23
|
+
[x: string]: number;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a mock node with functions to add and remove event handlers
|
|
27
|
+
*
|
|
28
|
+
* @param {Array<string} events
|
|
29
|
+
* @returns {{node: HTMLElement, listeners: Object<string, integer>}}
|
|
30
|
+
*/
|
|
31
|
+
export function getMockNode(events: Array<string>): {
|
|
32
|
+
node: HTMLElement;
|
|
33
|
+
listeners: {
|
|
34
|
+
[x: string]: integer;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* @typedef {Object} NestedItem
|
|
39
|
+
* @property {string} name
|
|
40
|
+
* @property {string} [dataPath]
|
|
41
|
+
* @property {string} [id]
|
|
42
|
+
* @property {Array<NestedItem>} [children]
|
|
43
|
+
*/
|
|
44
|
+
/**
|
|
45
|
+
* Creates a nested HTML element structure using the provided data
|
|
46
|
+
*
|
|
47
|
+
* @param {NestedItem} item
|
|
48
|
+
* @returns {HTMLElement}
|
|
49
|
+
*/
|
|
50
|
+
export function createNestedElement(item: NestedItem): HTMLElement;
|
|
51
|
+
export type NestedItem = {
|
|
52
|
+
name: string;
|
|
53
|
+
dataPath?: string | undefined;
|
|
54
|
+
id?: string | undefined;
|
|
55
|
+
children?: NestedItem[] | undefined;
|
|
56
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Updates the media query matches
|
|
3
|
+
* @returns {void}
|
|
4
|
+
*/
|
|
5
|
+
export function updateMedia(): void;
|
|
6
|
+
/**
|
|
7
|
+
* Mocks the window.matchMedia function
|
|
8
|
+
* @param {string} query
|
|
9
|
+
* @returns {Object}
|
|
10
|
+
*/
|
|
11
|
+
export const matchMediaMock: import("vitest").Mock<(...args: any[]) => any>;
|
|
12
|
+
export type MediaQuery = {
|
|
13
|
+
"min-width"?: integer;
|
|
14
|
+
"max-width"?: integer;
|
|
15
|
+
"min-height"?: integer;
|
|
16
|
+
"max-height"?: integer;
|
|
17
|
+
width?: integer;
|
|
18
|
+
height?: integer;
|
|
19
|
+
orientation?: integer;
|
|
20
|
+
"aspect-ratio"?: integer;
|
|
21
|
+
"min-aspect-ratio"?: integer;
|
|
22
|
+
"max-aspect-ratio"?: integer;
|
|
23
|
+
resolution?: integer;
|
|
24
|
+
"min-resolution"?: integer;
|
|
25
|
+
"max-resolution"?: integer;
|
|
26
|
+
scan?: integer;
|
|
27
|
+
grid?: integer;
|
|
28
|
+
update?: integer;
|
|
29
|
+
"overflow-block"?: integer;
|
|
30
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function simulateMouseEvent(x: any, y: any): {
|
|
2
|
+
clientX: any;
|
|
3
|
+
clientY: any;
|
|
4
|
+
stopPropagation: import("vitest").Mock<(...args: any[]) => any>;
|
|
5
|
+
preventDefault: import("vitest").Mock<(...args: any[]) => any>;
|
|
6
|
+
};
|
|
7
|
+
export function simulateTouchEvent(clientX: any, clientY: any): {
|
|
8
|
+
touches: {
|
|
9
|
+
clientX: any;
|
|
10
|
+
clientY: any;
|
|
11
|
+
}[];
|
|
12
|
+
preventDefault: import("vitest").Mock<(...args: any[]) => any>;
|
|
13
|
+
stopPropagation: import("vitest").Mock<(...args: any[]) => any>;
|
|
14
|
+
};
|
|
15
|
+
export function simulateTouchSwipe(node: any, distance: any, delay?: number): void;
|
|
16
|
+
export function simulateMouseSwipe(node: any, distance: any, delay?: number): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default defineConfig;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rokkit/helpers",
|
|
3
|
-
"version": "1.0.0-next.
|
|
4
|
-
"description": "Custom matchers for vitest.",
|
|
3
|
+
"version": "1.0.0-next.106",
|
|
4
|
+
"description": "Custom matchers for vitest, mocks and simulators for testing.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -1,10 +1,28 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest'
|
|
2
2
|
import { render } from '@testing-library/svelte'
|
|
3
3
|
import MockItem from '../../src/components/MockItem.svelte'
|
|
4
|
+
import { flushSync } from 'svelte'
|
|
4
5
|
|
|
5
6
|
describe('MockItem', () => {
|
|
6
|
-
it('should render', () => {
|
|
7
|
-
const
|
|
8
|
-
|
|
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()
|
|
9
27
|
})
|
|
10
28
|
})
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
`;
|
|
@@ -4,6 +4,6 @@ import * as components from '../../src/components/index'
|
|
|
4
4
|
|
|
5
5
|
describe('components', () => {
|
|
6
6
|
it('should contain all exported components', () => {
|
|
7
|
-
expect(Object.keys(components)).toEqual(['MockItem'])
|
|
7
|
+
expect(Object.keys(components)).toEqual(['MockItem', 'StaticContent'])
|
|
8
8
|
})
|
|
9
9
|
})
|
|
@@ -4,9 +4,9 @@ import { ResizeObserver } from '../../src/mocks/resize-observer'
|
|
|
4
4
|
global.ResizeObserver = ResizeObserver
|
|
5
5
|
|
|
6
6
|
describe('ResizeObserver', () => {
|
|
7
|
-
let resizeObserver
|
|
8
|
-
let callback
|
|
9
|
-
let element
|
|
7
|
+
let resizeObserver = null
|
|
8
|
+
let callback = null
|
|
9
|
+
let element = null
|
|
10
10
|
|
|
11
11
|
beforeEach(() => {
|
|
12
12
|
callback = vi.fn()
|
package/spec/simulator.spec.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<p>This is a static content component.</p>
|
package/src/components/index.js
CHANGED
package/src/index.js
CHANGED
package/src/matchers/index.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
// skipcq: JS-E1004 - Needed for exposing all functions
|
|
1
2
|
export * from './array'
|
|
3
|
+
// skipcq: JS-E1004 - Needed for exposing all functions
|
|
2
4
|
export * from './action'
|
|
5
|
+
// skipcq: JS-E1004 - Needed for exposing all functions
|
|
3
6
|
export * from './dataset'
|
|
7
|
+
// skipcq: JS-E1004 - Needed for exposing all functions
|
|
4
8
|
export * from './event'
|
package/src/mocks/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import './animate'
|
|
2
2
|
import { ResizeObserver } from './resize-observer'
|
|
3
3
|
|
|
4
|
+
// skipcq: JS-E1004 - Needed for exposing all functions
|
|
4
5
|
export * from './match-media'
|
|
6
|
+
// skipcq: JS-E1004 - Needed for exposing all functions
|
|
5
7
|
export * from './element'
|
|
6
8
|
|
|
7
9
|
global.ResizeObserver = ResizeObserver
|
package/src/simulators/touch.js
CHANGED
|
@@ -2,6 +2,12 @@ import { vi } from 'vitest'
|
|
|
2
2
|
|
|
3
3
|
global.Touch = vi.fn().mockImplementation((input) => input)
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Simulates a mouse event on a given node.
|
|
7
|
+
* @param {number} x
|
|
8
|
+
* @param {number} y
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
5
11
|
export function simulateMouseEvent(x, y) {
|
|
6
12
|
return {
|
|
7
13
|
clientX: x,
|
|
@@ -10,6 +16,13 @@ export function simulateMouseEvent(x, y) {
|
|
|
10
16
|
preventDefault: vi.fn()
|
|
11
17
|
}
|
|
12
18
|
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Simulates a touch event on a given node.
|
|
22
|
+
* @param {number} clientX
|
|
23
|
+
* @param {number} clientY
|
|
24
|
+
* @returns
|
|
25
|
+
*/
|
|
13
26
|
export function simulateTouchEvent(clientX, clientY) {
|
|
14
27
|
return {
|
|
15
28
|
touches: [{ clientX, clientY }],
|
|
@@ -18,6 +31,12 @@ export function simulateTouchEvent(clientX, clientY) {
|
|
|
18
31
|
}
|
|
19
32
|
}
|
|
20
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Simulates a touch swipe on a given node.
|
|
36
|
+
* @param {HTMLElement} node
|
|
37
|
+
* @param {number} distance
|
|
38
|
+
* @param {number} [delay=0]
|
|
39
|
+
*/
|
|
21
40
|
export function simulateTouchSwipe(node, distance, delay = 0) {
|
|
22
41
|
const touchStart = new Touch({
|
|
23
42
|
identifier: 0,
|
|
@@ -42,6 +61,12 @@ export function simulateTouchSwipe(node, distance, delay = 0) {
|
|
|
42
61
|
node.dispatchEvent(touchEndEvent)
|
|
43
62
|
}
|
|
44
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Simulates a mouse swipe on a given node.
|
|
66
|
+
* @param {HTMLElement} node
|
|
67
|
+
* @param {number} distance
|
|
68
|
+
* @param {number} [delay=0]
|
|
69
|
+
*/
|
|
45
70
|
export function simulateMouseSwipe(node, distance, delay = 0) {
|
|
46
71
|
node.dispatchEvent(new MouseEvent('mousedown', { clientX: 0, clientY: 0 }))
|
|
47
72
|
node.dispatchEvent(
|
|
@@ -53,15 +78,3 @@ export function simulateMouseSwipe(node, distance, delay = 0) {
|
|
|
53
78
|
vi.advanceTimersByTime(delay)
|
|
54
79
|
node.dispatchEvent(new MouseEvent('mouseup', { clientX: distance.x, clientY: distance.y }))
|
|
55
80
|
}
|
|
56
|
-
|
|
57
|
-
// export function getCustomEventMock() {
|
|
58
|
-
// return vi.fn().mockImplementation((eventType, eventInit) => {
|
|
59
|
-
// class CustomEvent extends Event {
|
|
60
|
-
// constructor(type, init) {
|
|
61
|
-
// super(type, init)
|
|
62
|
-
// this.detail = init
|
|
63
|
-
// }
|
|
64
|
-
// }
|
|
65
|
-
// return new CustomEvent(eventType, eventInit)
|
|
66
|
-
// })
|
|
67
|
-
// }
|