Skip to content

Commit 74a8dcd

Browse files
authored
Better types and tests for useKeyProps (#2468)
* Better types + tests for useKeyProps * Change files * Update base type * type KeyboardEvent * KeyboardEvent -> KeyPressEvent * More tests + type fix * More tests * Combine useKeyProps to one file * more * PR comments
1 parent 1dd84f4 commit 74a8dcd

17 files changed

+261
-296
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Better types + tests for useKeyProps",
4+
"packageName": "@fluentui-react-native/interactive-hooks",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
const { configureJest } = require('@fluentui-react-native/scripts');
2-
module.exports = configureJest();
1+
const { configureReactNativeJest } = require('@fluentui-react-native/scripts');
2+
module.exports = configureReactNativeJest('win32');

packages/utils/interactive-hooks/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@
3434
},
3535
"devDependencies": {
3636
"@fluentui-react-native/eslint-config-rules": "^0.1.1",
37+
"@fluentui-react-native/test-tools": ">=0.1.1 <1.0.0",
3738
"@office-iss/react-native-win32": "^0.68.0",
3839
"@types/invariant": "^2.2.0",
3940
"@types/jest": "^26.0.0",
4041
"@types/react": "^17.0.2",
4142
"@types/react-native": "^0.68.0",
4243
"react": "17.0.2",
43-
"react-native": "^0.68.0"
44+
"react-native": "^0.68.0",
45+
"react-native-macos": "^0.68.0",
46+
"react-native-windows": "^0.68.0"
4447
},
4548
"peerDependencies": {
4649
"react": "17.0.2",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Pressable with useKeyProps 1`] = `
4+
<View
5+
accessible={true}
6+
focusable={true}
7+
keyUpEvents={
8+
Array [
9+
Object {
10+
"key": " ",
11+
},
12+
Object {
13+
"key": "Enter",
14+
},
15+
]
16+
}
17+
onBlur={[Function]}
18+
onClick={[Function]}
19+
onFocus={[Function]}
20+
onKeyDown={[Function]}
21+
onKeyUp={[Function]}
22+
onMouseEnter={[Function]}
23+
onMouseLeave={[Function]}
24+
onResponderGrant={[Function]}
25+
onResponderMove={[Function]}
26+
onResponderRelease={[Function]}
27+
onResponderTerminate={[Function]}
28+
onResponderTerminationRequest={[Function]}
29+
onStartShouldSetResponder={[Function]}
30+
/>
31+
`;

packages/utils/interactive-hooks/src/events.types.test.ts renamed to packages/utils/interactive-hooks/src/__tests__/events.types.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AccessibilityActionEvent, GestureResponderEvent } from 'react-native';
2-
import { KeyPressEvent } from './Pressability/CoreEventTypes';
3-
import { isAccessibilityActionEvent, isGestureResponderEvent, isKeyPressEvent } from './events.types';
2+
import { KeyPressEvent } from '../useKeyProps.types';
3+
import { isAccessibilityActionEvent, isGestureResponderEvent, isKeyPressEvent } from '../events.types';
44

55
const createMockEvent = (nativeEvent) => {
66
return {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as React from 'react';
2+
import * as renderer from 'react-test-renderer';
3+
import { Pressable } from 'react-native';
4+
import { useKeyProps } from '../useKeyProps';
5+
import { checkRenderConsistency, checkReRender } from '@fluentui-react-native/test-tools';
6+
import { PressablePropsExtended } from '../usePressableState.types';
7+
8+
const dummyFunction = () => {
9+
console.log('dummy');
10+
};
11+
12+
// Simple wrapper function to let us use `PressablePropsExtended` to fix type errors
13+
const PressableWithDesktopProps = (props: PressablePropsExtended) => {
14+
return <Pressable {...props} />;
15+
};
16+
17+
it('Pressable with useKeyProps', () => {
18+
const keyboardProps = useKeyProps(dummyFunction, ' ', 'Enter');
19+
const tree = renderer.create(<PressableWithDesktopProps {...keyboardProps} />).toJSON();
20+
expect(tree).toMatchSnapshot();
21+
});
22+
23+
it('useKeyProps called twice', () => {
24+
const keyboardProps1 = useKeyProps(dummyFunction, ' ', 'Enter');
25+
const keyboardProps2 = useKeyProps(dummyFunction, ' ', 'Enter');
26+
expect(keyboardProps1).toBe(keyboardProps2);
27+
});
28+
29+
it('Simple Pressable with useKeyProps rendering does not invalidate styling', () => {
30+
const keyboardProps = useKeyProps(dummyFunction, ' ', 'Enter');
31+
checkRenderConsistency(() => <PressableWithDesktopProps {...keyboardProps} />, 2);
32+
});
33+
34+
it('Pressable with useKeyProps re-renders correctly', () => {
35+
const keyboardProps = useKeyProps(dummyFunction, ' ', 'Enter');
36+
checkReRender(() => <PressableWithDesktopProps {...keyboardProps} />, 2);
37+
});

packages/utils/interactive-hooks/src/events.types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AccessibilityActionEvent, GestureResponderEvent } from 'react-native';
2-
import { KeyPressEvent, MouseEvent } from './Pressability/CoreEventTypes';
1+
import { AccessibilityActionEvent, GestureResponderEvent, MouseEvent } from 'react-native';
2+
import { KeyPressEvent } from './useKeyProps.types';
33

44
export type InteractionEvent = GestureResponderEvent | MouseEvent | KeyPressEvent | AccessibilityActionEvent;
55

packages/utils/interactive-hooks/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export type {
4545
export type {
4646
BlurEvent,
4747
FocusEvent,
48-
KeyPressEvent,
4948
Layout,
5049
LayoutEvent,
5150
MouseEvent,
@@ -56,8 +55,8 @@ export type {
5655
TextLayout,
5756
TextLayoutEvent,
5857
} from './Pressability/CoreEventTypes';
58+
export type { KeyPressEvent, KeyCallback, KeyPressProps } from './useKeyProps.types';
5959
export { preferKeyDownForKeyEvents, useKeyCallback, useKeyDownProps, useKeyProps, useKeyUpProps } from './useKeyProps';
60-
export type { KeyCallback, KeyPressProps } from './useKeyProps';
6160
export { useOnPressWithFocus } from './useOnPressWithFocus';
6261
export type { OnPressCallback, OnPressWithFocusCallback } from './useOnPressWithFocus';
6362
export { getAccessibilityState } from './getAccessibilityState';

packages/utils/interactive-hooks/src/isModifierKey.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/utils/interactive-hooks/src/useKeyProps.macos.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)