Skip to content

Commit

Permalink
[change] React 17 peer dependency
Browse files Browse the repository at this point in the history
React 17 changed how blur/focus events are implemented, which requires a small
change to several unit tests to ensure the relevant DOM events are dispatched.
  • Loading branch information
necolas committed Feb 5, 2021
1 parent afea44a commit c835ce9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"react-timer-mixin": "^0.13.4"
},
"peerDependencies": {
"react": ">=16.5.1",
"react-dom": ">=16.5.1"
"react": ">=17.0.1",
"react-dom": ">=17.0.1"
},
"author": "Nicolas Gallagher",
"license": "MIT",
Expand Down
3 changes: 2 additions & 1 deletion src/exports/ActivityIndicator/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ describe('components/ActivityIndicator', () => {
render(<ActivityIndicator onBlur={onBlur} ref={ref} />);
});
const target = createEventTarget(ref.current);
const body = createEventTarget(document.body);
act(() => {
target.focus();
target.blur();
body.focus({ relatedTarget: target.node });
});
expect(onBlur).toBeCalled();
});
Expand Down
3 changes: 2 additions & 1 deletion src/exports/CheckBox/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ describe('CheckBox', () => {
render(<CheckBox onBlur={onBlur} ref={ref} />);
});
const target = createEventTarget(ref.current);
const body = createEventTarget(document.body);
act(() => {
target.focus();
target.blur();
body.focus({ relatedTarget: target.node });
});
expect(onBlur).toBeCalled();
});
Expand Down
3 changes: 2 additions & 1 deletion src/exports/Pressable/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ describe('components/Pressable', () => {
));
});
const target = createEventTarget(ref.current);
const body = createEventTarget(document.body);
expect(container.firstChild).toMatchSnapshot();
act(() => {
target.focus();
});
expect(onFocus).toBeCalled();
expect(container.firstChild).toMatchSnapshot();
act(() => {
target.blur();
body.focus({ relatedTarget: target.node });
});
expect(onBlur).toBeCalled();
expect(container.firstChild).toMatchSnapshot();
Expand Down
4 changes: 2 additions & 2 deletions src/exports/Text/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ describe('components/Text', () => {
render(<Text onBlur={onBlur} ref={ref} />);
});
const target = createEventTarget(ref.current);
const body = createEventTarget(document.body);
act(() => {
target.focus();
target.blur();
body.focus({ relatedTarget: target.node });
});
expect(onBlur).toBeCalled();
});
Expand All @@ -94,7 +95,6 @@ describe('components/Text', () => {
const target = createEventTarget(ref.current);
act(() => {
target.focus();
target.blur();
});
expect(onFocus).toBeCalled();
});
Expand Down
28 changes: 21 additions & 7 deletions src/exports/TextInput/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import React from 'react';
import TextInput from '..';
import { act } from 'react-dom/test-utils';
import { createEventTarget } from 'dom-event-testing-library';
import { render } from '@testing-library/react';

function findInput(container) {
Expand Down Expand Up @@ -238,9 +240,16 @@ describe('components/TextInput', () => {

test('prop "onBlur"', () => {
const onBlur = jest.fn();
const { container } = render(<TextInput onBlur={onBlur} />);
const input = findInput(container);
input.dispatchEvent(new window.FocusEvent('blur', {}));
const ref = React.createRef();
act(() => {
render(<TextInput onBlur={onBlur} ref={ref} />);
});
const target = createEventTarget(ref.current);
const body = createEventTarget(document.body);
act(() => {
target.focus();
body.focus({ relatedTarget: target.node });
});
expect(onBlur).toHaveBeenCalledTimes(1);
expect(TextInput.State.currentlyFocusedField()).toBe(null);
});
Expand All @@ -267,11 +276,16 @@ describe('components/TextInput', () => {

test('prop "onFocus"', () => {
const onFocus = jest.fn();
const { container } = render(<TextInput onFocus={onFocus} />);
const input = findInput(container);
input.focus();
const ref = React.createRef();
act(() => {
render(<TextInput onFocus={onFocus} ref={ref} />);
});
const target = createEventTarget(ref.current);
act(() => {
target.focus();
});
expect(onFocus).toHaveBeenCalledTimes(1);
expect(TextInput.State.currentlyFocusedField()).toBe(input);
expect(TextInput.State.currentlyFocusedField()).toBe(target.node);
});

describe('prop "onKeyPress"', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/exports/View/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ describe('components/View', () => {
render(<View onBlur={onBlur} ref={ref} />);
});
const target = createEventTarget(ref.current);
const body = createEventTarget(document.body);
act(() => {
target.focus();
target.blur();
body.focus({ relatedTarget: target.node });
});
expect(onBlur).toBeCalled();
});
Expand Down

0 comments on commit c835ce9

Please sign in to comment.