@maratus-lib/props 0.0.0-pr.15.field-widgets.8daa97e-20261122224005
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/CHANGELOG.md +12 -0
- package/docs/test-design.md +8 -0
- package/package.json +27 -0
- package/src/index.ts +4 -0
- package/src/mergeRefs.test.ts +55 -0
- package/src/mergeRefs.ts +31 -0
- package/src/omitPropWithWarning.test.ts +39 -0
- package/src/omitPropWithWarning.ts +16 -0
- package/src/omitProps.test.ts +70 -0
- package/src/omitProps.ts +26 -0
- package/src/suppressActivation.test.ts +46 -0
- package/src/suppressActivation.ts +51 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# @maratus-lib/props
|
|
2
|
+
|
|
3
|
+
## 0.0.0-pr.15.field-widgets.8daa97e-20261122224005
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- da8e3ac: Adds `mergeRefs`, `omitProps`, `omitPropWithWarning`, and `suppressActivation` prop composition helpers
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [da8e3ac]
|
|
12
|
+
- @maratus-lib/development@0.0.0-pr.15.field-widgets.8daa97e-20261122224005
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Test design
|
|
2
|
+
|
|
3
|
+
How each surface is driven, and which boundaries are faked. A test that bypasses this is drift.
|
|
4
|
+
|
|
5
|
+
- **Surfaces:** `mergeRefs`, `omitProps`, `omitPropWithWarning`, and `suppressActivation`.
|
|
6
|
+
- **Driven by:** `bun test`, calling each function through the package entry point.
|
|
7
|
+
- **Faked:** refs and handlers are `mock()` functions or `createRef` objects, key events are plain objects with a `key` and a `preventDefault` mock, and `console.warn` is spied on and silenced.
|
|
8
|
+
- **No Cypress:** neither surface renders, so the template's component test setup is removed.
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@maratus-lib/props",
|
|
3
|
+
"version": "0.0.0-pr.15.field-widgets.8daa97e-20261122224005",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.ts"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test:unit": "bun test --preload @maratus/bun/unit-test src/**",
|
|
11
|
+
"typecheck": "tsgo -p tsconfig.src.json --noEmit && tsgo -p tsconfig.bun.json --noEmit",
|
|
12
|
+
"lint": "oxlint .",
|
|
13
|
+
"format": "oxfmt --write .",
|
|
14
|
+
"format:check": "oxfmt --check ."
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@maratus-lib/development": "workspace:*"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@maratus/bun": "workspace:*",
|
|
21
|
+
"@maratus/dotfiles": "workspace:*",
|
|
22
|
+
"@types/react": "^19.2.14"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"react": "^19.1.1"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { describe, expect, mock, test } from 'bun:test'
|
|
2
|
+
import { createRef } from 'react'
|
|
3
|
+
import * as entryPoint from './index'
|
|
4
|
+
import { mergeRefs } from './index'
|
|
5
|
+
|
|
6
|
+
test('PRD-001 exports the helpers from the entry point', () => {
|
|
7
|
+
expect([...Object.keys(entryPoint)].sort()).toEqual([
|
|
8
|
+
'mergeRefs',
|
|
9
|
+
'omitPropWithWarning',
|
|
10
|
+
'omitProps',
|
|
11
|
+
'suppressActivation',
|
|
12
|
+
])
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
describe(mergeRefs, () => {
|
|
16
|
+
test('PRD-002 attaches the element to every ref', () => {
|
|
17
|
+
const element = document.createElement('input')
|
|
18
|
+
const objectRef = createRef<HTMLInputElement>()
|
|
19
|
+
const callbackRef = mock()
|
|
20
|
+
|
|
21
|
+
mergeRefs(objectRef, callbackRef)(element)
|
|
22
|
+
|
|
23
|
+
expect(objectRef.current).toBe(element)
|
|
24
|
+
expect(callbackRef).toHaveBeenCalledWith(element)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('PRD-003 releases every ref when the element detaches', () => {
|
|
28
|
+
const element = document.createElement('input')
|
|
29
|
+
const objectRef = createRef<HTMLInputElement>()
|
|
30
|
+
const callbackRef = mock()
|
|
31
|
+
const cleanupRef = mock()
|
|
32
|
+
const callbackWithCleanup = mock(() => cleanupRef)
|
|
33
|
+
|
|
34
|
+
const detach = mergeRefs(
|
|
35
|
+
objectRef,
|
|
36
|
+
callbackRef,
|
|
37
|
+
callbackWithCleanup,
|
|
38
|
+
)(element)
|
|
39
|
+
detach?.()
|
|
40
|
+
|
|
41
|
+
expect(objectRef.current).toBe(null)
|
|
42
|
+
expect(callbackRef).toHaveBeenLastCalledWith(null)
|
|
43
|
+
expect(cleanupRef).toHaveBeenCalledTimes(1)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('PRD-004 skips missing refs', () => {
|
|
47
|
+
const element = document.createElement('input')
|
|
48
|
+
const callbackRef = mock()
|
|
49
|
+
|
|
50
|
+
const detach = mergeRefs(undefined, null, callbackRef)(element)
|
|
51
|
+
detach?.()
|
|
52
|
+
|
|
53
|
+
expect(callbackRef.mock.calls).toEqual([[element], [null]])
|
|
54
|
+
})
|
|
55
|
+
})
|
package/src/mergeRefs.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Ref, RefCallback } from 'react'
|
|
2
|
+
|
|
3
|
+
// Lets one element take several refs, such as a consumer ref and a
|
|
4
|
+
// library-owned ref.
|
|
5
|
+
export function mergeRefs<TElement>(
|
|
6
|
+
...refs: readonly (Ref<TElement> | undefined)[]
|
|
7
|
+
): RefCallback<TElement> {
|
|
8
|
+
return (element) => {
|
|
9
|
+
const cleanups = refs.map((ref) => attachRef(ref, element))
|
|
10
|
+
|
|
11
|
+
return () => {
|
|
12
|
+
for (const cleanup of cleanups) cleanup()
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function attachRef<TElement>(
|
|
18
|
+
ref: Ref<TElement> | undefined,
|
|
19
|
+
element: TElement | null,
|
|
20
|
+
): () => void {
|
|
21
|
+
if (typeof ref === 'function') {
|
|
22
|
+
const cleanup = ref(element)
|
|
23
|
+
return typeof cleanup === 'function' ? cleanup : () => ref(null)
|
|
24
|
+
}
|
|
25
|
+
if (!ref) return () => {}
|
|
26
|
+
|
|
27
|
+
ref.current = element
|
|
28
|
+
return () => {
|
|
29
|
+
ref.current = null
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, mock, test } from 'bun:test'
|
|
2
|
+
|
|
3
|
+
// The development check belongs to warnInDevelopment and is tested there, so
|
|
4
|
+
// this stubs it and checks only what omitPropWithWarning asks of it.
|
|
5
|
+
const warnInDevelopment = mock()
|
|
6
|
+
mock.module('@maratus-lib/development', () => ({ warnInDevelopment }))
|
|
7
|
+
|
|
8
|
+
const { omitPropWithWarning } = await import('./index')
|
|
9
|
+
|
|
10
|
+
const WARNING = 'readOnly does not apply here.'
|
|
11
|
+
|
|
12
|
+
describe('omitPropWithWarning', () => {
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
warnInDevelopment.mockClear()
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test('PRD-007 removes a present prop', () => {
|
|
18
|
+
const props = omitPropWithWarning(
|
|
19
|
+
{ id: 'item', readOnly: true },
|
|
20
|
+
'readOnly',
|
|
21
|
+
WARNING,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
expect(props).toEqual({ id: 'item' })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('PRD-007 passes the message to warnInDevelopment for a present prop', () => {
|
|
28
|
+
omitPropWithWarning({ readOnly: true }, 'readOnly', WARNING)
|
|
29
|
+
|
|
30
|
+
expect(warnInDevelopment).toHaveBeenCalledWith(WARNING)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('PRD-008 returns the props unchanged without warning when the prop is absent', () => {
|
|
34
|
+
const props = { id: 'item' }
|
|
35
|
+
|
|
36
|
+
expect(omitPropWithWarning(props, 'readOnly', WARNING)).toBe(props)
|
|
37
|
+
expect(warnInDevelopment).not.toHaveBeenCalled()
|
|
38
|
+
})
|
|
39
|
+
})
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { warnInDevelopment } from '@maratus-lib/development'
|
|
2
|
+
import { omitProps } from './omitProps'
|
|
3
|
+
|
|
4
|
+
// For a prop a component cannot honour: it is removed, and in development
|
|
5
|
+
// the consumer is told why.
|
|
6
|
+
export function omitPropWithWarning<TProps extends object, TKey extends string>(
|
|
7
|
+
props: TProps,
|
|
8
|
+
key: TKey,
|
|
9
|
+
message: string,
|
|
10
|
+
): Omit<TProps, TKey> {
|
|
11
|
+
if (!(key in props)) return props
|
|
12
|
+
|
|
13
|
+
warnInDevelopment(message)
|
|
14
|
+
|
|
15
|
+
return omitProps(props, [key])
|
|
16
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { omitProps } from './index'
|
|
3
|
+
|
|
4
|
+
const SYMBOL_KEY = Symbol('symbol-key')
|
|
5
|
+
const OMITTED_SYMBOL_KEY = Symbol('omitted-symbol-key')
|
|
6
|
+
|
|
7
|
+
// Every value type a props object can hold, so each one is checked to
|
|
8
|
+
// survive the copy as the same value.
|
|
9
|
+
const KEPT_VALUES = {
|
|
10
|
+
array: ['first', 'second'],
|
|
11
|
+
bigint: 1n,
|
|
12
|
+
boolean: false,
|
|
13
|
+
function: () => 'handler',
|
|
14
|
+
null: null,
|
|
15
|
+
number: 0,
|
|
16
|
+
object: { nested: true },
|
|
17
|
+
string: '',
|
|
18
|
+
symbolValue: Symbol('value'),
|
|
19
|
+
undefined: undefined,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe(omitProps, () => {
|
|
23
|
+
test('PRD-005 removes the given string and symbol keys', () => {
|
|
24
|
+
const props = { [OMITTED_SYMBOL_KEY]: true, id: 'item', readOnly: true }
|
|
25
|
+
|
|
26
|
+
const remainingProps = omitProps(props, ['readOnly', OMITTED_SYMBOL_KEY])
|
|
27
|
+
|
|
28
|
+
expect(Reflect.ownKeys(remainingProps)).toEqual(['id'])
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('PRD-005 matches a numeric key to its string key', () => {
|
|
32
|
+
const props = { 1: 'first', id: 'item' }
|
|
33
|
+
|
|
34
|
+
expect(omitProps(props, [1])).toEqual({ id: 'item' })
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
for (const [type, value] of Object.entries(KEPT_VALUES)) {
|
|
38
|
+
test(`PRD-006 keeps a ${type} value as the same value`, () => {
|
|
39
|
+
const props = { kept: value, readOnly: true }
|
|
40
|
+
|
|
41
|
+
expect(omitProps(props, ['readOnly']).kept).toBe(value)
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
test('PRD-006 keeps symbol-keyed props', () => {
|
|
46
|
+
const props = { [SYMBOL_KEY]: 'symbol', readOnly: true }
|
|
47
|
+
|
|
48
|
+
expect(omitProps(props, ['readOnly'])[SYMBOL_KEY]).toBe('symbol')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('PRD-006 leaves out non-enumerable and inherited props, as spread does', () => {
|
|
52
|
+
const props = Object.create(
|
|
53
|
+
{ inherited: true },
|
|
54
|
+
{
|
|
55
|
+
hidden: { enumerable: false, value: true },
|
|
56
|
+
shown: { enumerable: true, value: true },
|
|
57
|
+
},
|
|
58
|
+
) as object
|
|
59
|
+
|
|
60
|
+
expect(omitProps(props, [])).toEqual({ ...props })
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('PRD-006 leaves the given props unchanged', () => {
|
|
64
|
+
const props = { id: 'item', readOnly: true }
|
|
65
|
+
|
|
66
|
+
omitProps(props, ['readOnly'])
|
|
67
|
+
|
|
68
|
+
expect(props).toEqual({ id: 'item', readOnly: true })
|
|
69
|
+
})
|
|
70
|
+
})
|
package/src/omitProps.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Copies the way object spread does: own enumerable string and symbol keys,
|
|
2
|
+
// with every value kept by reference, so functions, arrays, objects, and
|
|
3
|
+
// refs stay the same instances.
|
|
4
|
+
export function omitProps<TProps extends object, TKey extends PropertyKey>(
|
|
5
|
+
props: TProps,
|
|
6
|
+
keys: readonly TKey[],
|
|
7
|
+
): Omit<TProps, TKey> {
|
|
8
|
+
const omittedKeys = new Set<PropertyKey>(keys.map(toPropertyKey))
|
|
9
|
+
const remainingProps: Record<PropertyKey, unknown> = {}
|
|
10
|
+
|
|
11
|
+
for (const key of Reflect.ownKeys(props)) {
|
|
12
|
+
if (omittedKeys.has(key) || !isEnumerable(props, key)) continue
|
|
13
|
+
remainingProps[key] = Reflect.get(props, key)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return remainingProps as Omit<TProps, TKey>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Object keys are strings or symbols, so a numeric key matches its string.
|
|
20
|
+
function toPropertyKey(key: PropertyKey): string | symbol {
|
|
21
|
+
return typeof key === 'number' ? String(key) : key
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isEnumerable(props: object, key: PropertyKey): boolean {
|
|
25
|
+
return Object.prototype.propertyIsEnumerable.call(props, key)
|
|
26
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { KeyboardEvent } from 'react'
|
|
2
|
+
import { describe, expect, mock, test } from 'bun:test'
|
|
3
|
+
import { suppressActivation } from './index'
|
|
4
|
+
|
|
5
|
+
function createKeyEvent(key: string) {
|
|
6
|
+
return { key, preventDefault: mock() } as unknown as KeyboardEvent<never>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe(suppressActivation, () => {
|
|
10
|
+
test('PRD-009 drops pointer activation handlers when suppressed', () => {
|
|
11
|
+
const itemProps = { onClick: mock(), onPointerDown: mock(), title: 'Item' }
|
|
12
|
+
|
|
13
|
+
const props: object = suppressActivation(itemProps, true)
|
|
14
|
+
|
|
15
|
+
expect(props).toEqual({ title: 'Item' })
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
for (const key of ['Enter', ' ']) {
|
|
19
|
+
test(`PRD-010 skips "${key}" in key handlers and prevents its default when suppressed`, () => {
|
|
20
|
+
const onKeyDown = mock()
|
|
21
|
+
const props = suppressActivation({ onKeyDown }, true)
|
|
22
|
+
const event = createKeyEvent(key)
|
|
23
|
+
|
|
24
|
+
props.onKeyDown?.(event)
|
|
25
|
+
|
|
26
|
+
expect(onKeyDown).not.toHaveBeenCalled()
|
|
27
|
+
expect(event.preventDefault).toHaveBeenCalled()
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test('PRD-010 passes other keys to key handlers when suppressed', () => {
|
|
32
|
+
const onKeyDown = mock()
|
|
33
|
+
const props = suppressActivation({ onKeyDown }, true)
|
|
34
|
+
const event = createKeyEvent('ArrowDown')
|
|
35
|
+
|
|
36
|
+
props.onKeyDown?.(event)
|
|
37
|
+
|
|
38
|
+
expect(onKeyDown).toHaveBeenCalledWith(event)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('PRD-011 returns the props unchanged when not suppressed', () => {
|
|
42
|
+
const props = { onClick: mock() }
|
|
43
|
+
|
|
44
|
+
expect(suppressActivation(props, false)).toBe(props)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { KeyboardEvent } from 'react'
|
|
2
|
+
|
|
3
|
+
type ActivationHandlers = {
|
|
4
|
+
onClick?: unknown
|
|
5
|
+
onKeyDown?: (event: KeyboardEvent<never>) => void
|
|
6
|
+
onKeyUp?: (event: KeyboardEvent<never>) => void
|
|
7
|
+
onMouseDown?: unknown
|
|
8
|
+
onPointerDown?: unknown
|
|
9
|
+
onTouchStart?: unknown
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const ACTIVATION_KEYS = new Set(['Enter', ' '])
|
|
13
|
+
|
|
14
|
+
// Lets a component suppress consumer activation, for example while it is
|
|
15
|
+
// loading or disabled. Pointer handlers are dropped, and key handlers skip
|
|
16
|
+
// Enter and Space, so other keys can still move focus.
|
|
17
|
+
export function suppressActivation<TProps extends ActivationHandlers>(
|
|
18
|
+
props: TProps,
|
|
19
|
+
isSuppressed: boolean,
|
|
20
|
+
): TProps {
|
|
21
|
+
if (!isSuppressed) return props
|
|
22
|
+
|
|
23
|
+
const {
|
|
24
|
+
onClick: _onClick,
|
|
25
|
+
onKeyDown,
|
|
26
|
+
onKeyUp,
|
|
27
|
+
onMouseDown: _onMouseDown,
|
|
28
|
+
onPointerDown: _onPointerDown,
|
|
29
|
+
onTouchStart: _onTouchStart,
|
|
30
|
+
...otherProps
|
|
31
|
+
} = props
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
...otherProps,
|
|
35
|
+
...(onKeyDown ? { onKeyDown: skipActivationKeys(onKeyDown) } : {}),
|
|
36
|
+
...(onKeyUp ? { onKeyUp: skipActivationKeys(onKeyUp) } : {}),
|
|
37
|
+
} as TProps
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function skipActivationKeys<TEvent extends KeyboardEvent<never>>(
|
|
41
|
+
handler: (event: TEvent) => void,
|
|
42
|
+
): (event: TEvent) => void {
|
|
43
|
+
return (event) => {
|
|
44
|
+
if (ACTIVATION_KEYS.has(event.key)) {
|
|
45
|
+
event.preventDefault()
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
handler(event)
|
|
50
|
+
}
|
|
51
|
+
}
|