Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Commit 623345f

Browse files
author
Brandon Carroll
committed
fix: jest preset was not working
1 parent d1641d2 commit 623345f

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

jest-preset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ module.exports = Object.assign(jestPreset, {
55
...jestPreset.transformIgnorePatterns,
66
'node_modules/(?!(react-native.*|@?react-navigation.*)/)',
77
],
8-
setupFilesAfterEnv: [require.resolve('./dist/preset/setup.js')],
8+
setupFiles: [...jestPreset.setupFiles, require.resolve('./dist/preset/setup.js')],
99
});

src/__tests__/events.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,34 +86,34 @@ test('calling a custom event works as well', () => {
8686
});
8787
});
8888

89-
test('calling a handler when there is no valid target throws', () => {
89+
test('calling a handler when there is no valid target does not work', () => {
9090
const handleEvent = jest.fn();
9191
const { getByTestId } = render(<Image onPress={handleEvent} testID="image" />);
92-
expect(() => fireEvent.press(getByTestId('image'))).toThrow();
92+
expect(() => fireEvent.press(getByTestId('image'))).not.toThrow();
9393
expect(handleEvent).toBeCalledTimes(0);
9494
});
9595

96-
test('calling a handler if a Button is disabled throws', () => {
96+
test('calling a handler if a Button is disabled does not work', () => {
9797
const handleEvent = jest.fn();
9898
const { getByText } = render(<Button disabled onPress={handleEvent} title="button" />);
99-
expect(() => fireEvent.press(getByText('button'))).toThrow();
99+
expect(() => fireEvent.press(getByText('button'))).not.toThrow();
100100
expect(handleEvent).toBeCalledTimes(0);
101101
});
102102

103-
test('calling a handler if a Touchable is disabled throws', () => {
103+
test('calling a handler if a Touchable is disabled does not work', () => {
104104
const handleEvent = jest.fn();
105105
const { getByText } = render(
106106
<TouchableHighlight disabled onPress={jest.fn()}>
107107
<Text>touchable</Text>
108108
</TouchableHighlight>,
109109
);
110-
expect(() => fireEvent.press(getByText('touchable'))).toThrow();
110+
expect(() => fireEvent.press(getByText('touchable'))).not.toThrow();
111111
expect(handleEvent).toBeCalledTimes(0);
112112
});
113113

114114
test('calling an event that has no defined handler throws', () => {
115115
const { getByText } = render(<Text>test</Text>);
116-
expect(() => fireEvent.press(getByText('test'))).toThrow();
116+
expect(() => fireEvent.press(getByText('test'))).not.toThrow();
117117
});
118118

119119
test('calling an event sets nativeEvent properly', () => {

src/lib/__tests__/to-json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('it converts to json', () => {
2222
<View>
2323
<MiddleComponent>hello</MiddleComponent>
2424
<View>
25-
<Text>world</Text>
25+
<Text onPress={jest.fn()}>world</Text>
2626
<Text>foo bar</Text>
2727
</View>
2828
<View />

src/lib/events.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,14 @@ function isDisabled(element) {
202202

203203
function findEventHandler(element, event) {
204204
const { typeArg } = event;
205-
const eventHandler = getEventHandlerName(typeArg);
206-
const valid = isValidTarget(element, event);
207-
const disabled = isDisabled(element);
208-
209-
if (typeof element.props[eventHandler] === 'function' && valid) {
210-
if (disabled) {
211-
throw new Error(`A target was found for event: "${typeArg}", but the target is disabled`);
212-
}
213-
return element.props[eventHandler];
214-
}
205+
const handlerName = getEventHandlerName(typeArg);
206+
const eventHandler = element.props[handlerName];
215207

216-
if (element.parent === null) {
217-
throw new Error(`No target found for event: "${typeArg}"`);
208+
if (eventHandler && !isDisabled(element) && isValidTarget(element, event)) {
209+
return eventHandler;
218210
}
219211

220-
return findEventHandler(element.parent, event);
212+
return element.parent ? findEventHandler(element.parent, event) : i => i;
221213
}
222214

223215
function fireEvent(element, event) {

src/lib/query-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function proxyUnsafeProperties(node) {
6060
return function(...args) {
6161
return ref
6262
.apply(this, args)
63-
.filter(node => validComponentFilter(node))
63+
.filter(validComponentFilter)
6464
.map(proxyUnsafeProperties);
6565
};
6666
} else if (key === 'getProp') {

src/lib/to-json.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@ function toJSON(node) {
2727
return renderedChildren;
2828
}
2929

30+
// Function props get noisy in debug output, so we'll exclude them
31+
let renderedProps = {};
32+
Object.keys(props).filter(name => {
33+
if (typeof props[name] !== 'function') {
34+
renderedProps[name] = props[name];
35+
}
36+
});
37+
3038
// Finally, create the JSON object
3139
return {
3240
$$typeof: Symbol.for('react.test.json'),
3341
parent: node.parent,
3442
type: node.type,
35-
props,
43+
props: renderedProps,
3644
children: renderedChildren,
3745
};
3846
} catch (e) {

0 commit comments

Comments
 (0)