@rokkit/helpers 1.0.0-next.104 → 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.
Files changed (35) hide show
  1. package/dist/components/index.d.ts +1 -0
  2. package/dist/index.d.ts +1 -0
  3. package/dist/matchers/action.d.ts +27 -0
  4. package/dist/matchers/array.d.ts +10 -0
  5. package/dist/matchers/dataset.d.ts +10 -0
  6. package/dist/matchers/event.d.ts +10 -0
  7. package/dist/matchers/index.d.ts +4 -0
  8. package/dist/matchers/internal.d.ts +1 -0
  9. package/dist/mocks/animate.d.ts +1 -0
  10. package/dist/mocks/element.d.ts +56 -0
  11. package/dist/mocks/index.d.ts +2 -0
  12. package/dist/mocks/match-media.d.ts +30 -0
  13. package/dist/mocks/resize-observer.d.ts +9 -0
  14. package/dist/simulators/index.d.ts +1 -0
  15. package/dist/simulators/touch.d.ts +40 -0
  16. package/dist/src/components/index.d.ts +1 -0
  17. package/package.json +9 -7
  18. package/spec/components/MockItem.spec.svelte.js +28 -0
  19. package/spec/components/StaticContent.spec.svelte.js +10 -0
  20. package/spec/components/__snapshots__/MockItem.spec.svelte.js.snap +29 -0
  21. package/spec/components/__snapshots__/StaticContent.spec.svelte.js.snap +10 -0
  22. package/spec/components/index.spec.js +9 -0
  23. package/spec/mocks/animate.spec.js +1 -1
  24. package/spec/mocks/resize-observer.spec.js +3 -3
  25. package/spec/simulator.spec.js +1 -1
  26. package/src/components/MockItem.svelte +3 -2
  27. package/src/components/StaticContent.svelte +1 -0
  28. package/src/components/index.js +2 -0
  29. package/src/index.js +1 -0
  30. package/src/matchers/index.js +4 -0
  31. package/src/mocks/index.js +2 -0
  32. package/src/simulators/index.js +2 -0
  33. package/src/simulators/touch.js +25 -12
  34. package/LICENSE +0 -21
  35. package/vitest.config.js +0 -2
@@ -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: boolean;
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,4 @@
1
+ export * from "./array";
2
+ export * from "./action";
3
+ export * from "./dataset";
4
+ export * from "./event";
@@ -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,2 @@
1
+ export * from "./match-media";
2
+ export * from "./element";
@@ -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,9 @@
1
+ export class ResizeObserver {
2
+ constructor(callback: any);
3
+ callback: any;
4
+ elements: Map<any, any>;
5
+ observe(element: any): void;
6
+ unobserve(element: any): void;
7
+ disconnect(): void;
8
+ simulateResize(element: any, contentRect: any): void;
9
+ }
@@ -0,0 +1 @@
1
+ export * from "./touch";
@@ -0,0 +1,40 @@
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;
10
+ stopPropagation: import("vitest").Mock<(...args: any[]) => any>;
11
+ preventDefault: import("vitest").Mock<(...args: any[]) => any>;
12
+ };
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): {
20
+ touches: {
21
+ clientX: number;
22
+ clientY: number;
23
+ }[];
24
+ preventDefault: import("vitest").Mock<(...args: any[]) => any>;
25
+ stopPropagation: import("vitest").Mock<(...args: any[]) => any>;
26
+ };
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 {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rokkit/helpers",
3
- "version": "1.0.0-next.104",
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
  },
@@ -19,17 +19,19 @@
19
19
  "./mocks": "./src/mocks/index.js",
20
20
  "./matchers": "./src/matchers/index.js",
21
21
  "./simulators": "./src/simulators/index.js",
22
+ "./components": "./src/components/index.js",
22
23
  ".": {
23
24
  "types": "./dist/index.d.ts",
24
25
  "import": "./src/index.js"
25
26
  }
26
27
  },
28
+ "scripts": {
29
+ "prepublishOnly": "tsc --project tsconfig.build.json",
30
+ "clean": "rm -rf dist",
31
+ "build": "bun clean && bun prepublishOnly"
32
+ },
27
33
  "dependencies": {
28
34
  "@vitest/expect": "^2.1.8",
29
35
  "ramda": "^0.30.1"
30
- },
31
- "scripts": {
32
- "clean": "rm -rf dist",
33
- "build": "pnpm clean && pnpm prepublishOnly"
34
36
  }
35
- }
37
+ }
@@ -0,0 +1,28 @@
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
+ })
@@ -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
+ `;
@@ -0,0 +1,10 @@
1
+ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
+
3
+ exports[`StaticContent > renders the content 1`] = `
4
+ <div>
5
+ <p>
6
+ This is a static content component.
7
+ </p>
8
+
9
+ </div>
10
+ `;
@@ -0,0 +1,9 @@
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
+ })
@@ -1,4 +1,4 @@
1
- import { describe, it, expect, vi } from 'vitest'
1
+ import { describe, it, expect } from 'vitest'
2
2
  import '../../src/mocks/animate'
3
3
 
4
4
  describe('animate', () => {
@@ -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()
@@ -5,7 +5,7 @@ import {
5
5
  simulateTouchEvent,
6
6
  simulateMouseSwipe,
7
7
  simulateTouchSwipe
8
- } from '../src/simulators/touch'
8
+ } from '../src/simulators'
9
9
 
10
10
  expect.extend({ toHaveBeenDispatchedWith })
11
11
  describe('events', () => {
@@ -1,5 +1,6 @@
1
1
  <script>
2
- let { value } = $props()
2
+ let { value = null } = $props()
3
+ let text = $derived(typeof value === 'object' ? value?.text : value)
3
4
  </script>
4
5
 
5
- {value}
6
+ {text}
@@ -0,0 +1 @@
1
+ <p>This is a static content component.</p>
@@ -0,0 +1,2 @@
1
+ export { default as MockItem } from './MockItem.svelte'
2
+ export { default as StaticContent } from './StaticContent.svelte'
package/src/index.js CHANGED
@@ -1 +1,2 @@
1
+ // skipcq: JS-E1004 - Needed for exposing all functions
1
2
  export * from './simulators/touch'
@@ -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'
@@ -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
@@ -0,0 +1,2 @@
1
+ // skipcq: JS-E1004 - Needed for exposing all functions
2
+ export * from './touch'
@@ -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
- // }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Jerry Thomas
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
package/vitest.config.js DELETED
@@ -1,2 +0,0 @@
1
- import defineConfig from 'shared-config/vitest.config.js'
2
- export default defineConfig