Skip to content

Commit 4c687b4

Browse files
Merge pull request #269 from GetStream/testing-lib-upgrade
React Testing Library upgrade
2 parents 10f9455 + 3af204b commit 4c687b4

File tree

11 files changed

+952
-857
lines changed

11 files changed

+952
-857
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"no-var": 2,
2727
"linebreak-style": [2, "unix"],
2828
"semi": [1, "always"],
29+
"jest/expect-expect": 0,
2930
"jsx-quotes": ["error", "prefer-single"]
3031
},
3132
"env": {

jest-setup.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
import { registerNativeHandlers } from './src/native';
22

3+
// eslint-disable-next-line no-underscore-dangle
4+
const _consoleE = console.error;
5+
console.error = (e) => {
6+
// For some reason following error gets rased from waitFor function.
7+
// Source of the error is in react-test-renderer, although cause of the
8+
// issue is unknown. Until we figure out the proper way to fix this issue,
9+
// following hack should work. We just suppress this particular error.
10+
// It should be safe since the error is only raised in dev environment
11+
// https://github.com/facebook/react/blob/b683c07ccce340b9d687683d5dd7347a4c866787/packages/react-dom/src/test-utils/ReactTestUtilsAct.js#L121
12+
if (e.indexOf('You called act(async () => ...) without await') === -1) {
13+
_consoleE(e);
14+
}
15+
};
16+
317
registerNativeHandlers({
418
NetInfo: {
519
addEventListener: () => {},

jest.config.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const jestPreset = require('@testing-library/react-native/jest-preset');
2-
3-
module.exports = Object.assign(jestPreset, {
1+
// eslint-disable-next-line no-undef
2+
module.exports = {
3+
verbose: true,
44
testRegex: [
55
/**
66
* If you want to test single file, mention it here
@@ -9,11 +9,13 @@ module.exports = Object.assign(jestPreset, {
99
* "src/components/MessageList/__tests__/MessageList.test.js"
1010
*/
1111
],
12-
testPathIgnorePatterns: ['/node_modules/', '/examples/', '__snapshots__'],
1312
moduleNameMapper: {
1413
'mock-builders(.*)$': '<rootDir>/src/mock-builders$1',
1514
'@stream-io/styled-components':
1615
'<rootDir>/node_modules/@stream-io/styled-components/native/dist/styled-components.native.cjs.js',
1716
},
18-
setupFiles: [...jestPreset.setupFiles, require.resolve('./jest-setup.js')],
19-
});
17+
preset: 'react-native',
18+
setupFiles: [require.resolve('./jest-setup.js')],
19+
setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
20+
testPathIgnorePatterns: ['/node_modules/', '/examples/', '__snapshots__'],
21+
};

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"@babel/preset-flow": "^7.10.4",
105105
"@babel/preset-react": "^7.0.0",
106106
"@testing-library/jest-native": "^3.3.0",
107-
"@testing-library/react-native": "^5.0.3",
107+
"@testing-library/react-native": "^7.0.1",
108108
"@types/react": "^16.8.23",
109109
"@types/react-native": "^0.57.0",
110110
"ast-pretty-print": "^2.0.1",
@@ -116,11 +116,11 @@
116116
"babel-preset-expo": "^5.0.0",
117117
"eslint": "^5.16.0",
118118
"eslint-plugin-babel": "^5.3.0",
119-
"eslint-plugin-jest": "^22.5.1",
119+
"eslint-plugin-jest": "^23.20.0",
120120
"eslint-plugin-markdown": "^1.0.0",
121121
"eslint-plugin-react": "^7.12.4",
122122
"husky": "^2.1.0",
123-
"jest": "^25.1.0",
123+
"jest": "^26.2.2",
124124
"prettier": "^1.16.4",
125125
"react": "^16.5.0",
126126
"react-dom": "^16.8.6",

src/components/Attachment/__tests__/Attachment.test.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import React from 'react';
2-
import {
3-
fireEvent,
4-
render,
5-
wait,
6-
waitForElement,
7-
} from '@testing-library/react-native';
8-
import '@testing-library/jest-native/extend-expect';
2+
import { fireEvent, render, waitFor } from '@testing-library/react-native';
93
import { v4 as uuidv4 } from 'uuid';
104

115
import {
@@ -29,7 +23,7 @@ describe('Attachment', () => {
2923
const attachment = generateAudioAttachment();
3024
const { getByTestId } = render(getAttachmentComponent({ attachment }));
3125

32-
await wait(() => {
26+
await waitFor(() => {
3327
expect(getByTestId('file-attachment')).toBeTruthy();
3428
});
3529
});
@@ -38,7 +32,7 @@ describe('Attachment', () => {
3832
const attachment = generateFileAttachment();
3933
const { getByTestId } = render(getAttachmentComponent({ attachment }));
4034

41-
await wait(() => {
35+
await waitFor(() => {
4236
expect(getByTestId('file-attachment')).toBeTruthy();
4337
});
4438
});
@@ -47,7 +41,7 @@ describe('Attachment', () => {
4741
const attachment = generateImgurAttachment();
4842
const { getByTestId } = render(getAttachmentComponent({ attachment }));
4943

50-
await wait(() => {
44+
await waitFor(() => {
5145
expect(getByTestId('card-attachment')).toBeTruthy();
5246
});
5347
});
@@ -56,7 +50,7 @@ describe('Attachment', () => {
5650
const attachment = generateGiphyAttachment();
5751
const { getByTestId } = render(getAttachmentComponent({ attachment }));
5852

59-
await wait(() => {
53+
await waitFor(() => {
6054
expect(getByTestId('card-attachment')).toBeTruthy();
6155
});
6256
});
@@ -65,7 +59,7 @@ describe('Attachment', () => {
6559
const attachment = generateImageAttachment();
6660
const { getByTestId } = render(getAttachmentComponent({ attachment }));
6761

68-
await wait(() => {
62+
await waitFor(() => {
6963
expect(getByTestId('card-attachment')).toBeTruthy();
7064
});
7165
});
@@ -77,7 +71,7 @@ describe('Attachment', () => {
7771
});
7872
const { getByTestId } = render(getAttachmentComponent({ attachment }));
7973

80-
await wait(() => {
74+
await waitFor(() => {
8175
expect(getByTestId('image-attachment-single')).toBeTruthy();
8276
});
8377
});
@@ -91,7 +85,7 @@ describe('Attachment', () => {
9185
getAttachmentComponent({ attachment, actionHandler: () => {} }),
9286
);
9387

94-
await wait(() => {
88+
await waitFor(() => {
9589
expect(getByTestId('attachment-actions')).toBeTruthy();
9690
});
9791
});
@@ -106,7 +100,7 @@ describe('Attachment', () => {
106100
}),
107101
);
108102

109-
await waitForElement(() =>
103+
await waitFor(() =>
110104
getByTestId(`attachment-actions-button-${action.name}`),
111105
);
112106

@@ -117,7 +111,7 @@ describe('Attachment', () => {
117111
fireEvent.press(getByTestId(`attachment-actions-button-${action.name}`));
118112
fireEvent.press(getByTestId(`attachment-actions-button-${action.name}`));
119113

120-
await wait(() => {
114+
await waitFor(() => {
121115
expect(actionHandler).toHaveBeenCalledTimes(2);
122116
});
123117
});
@@ -128,7 +122,7 @@ describe('Attachment', () => {
128122
attachment: generateCardAttachment({ type: uuidv4() }),
129123
}),
130124
);
131-
await wait(() => {
125+
await waitFor(() => {
132126
expect(getByTestId('card-attachment')).toBeTruthy();
133127
});
134128
});

src/components/Avatar/__tests__/Avatar.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { getNodeText, render, wait } from '@testing-library/react-native';
2+
import { render, waitFor } from '@testing-library/react-native';
33

44
import Avatar from '../Avatar';
55

@@ -9,7 +9,7 @@ describe('Avatar', () => {
99
<Avatar image='https://pbs.twimg.com/profile_images/897621870069112832/dFGq6aiE_400x400.jpg' />,
1010
);
1111

12-
await wait(() => {
12+
await waitFor(() => {
1313
expect(queryByTestId('avatar-image')).toBeTruthy();
1414
expect(queryByTestId('avatar-text')).toBeFalsy();
1515
});
@@ -23,7 +23,7 @@ describe('Avatar', () => {
2323
/>,
2424
);
2525

26-
await wait(() => {
26+
await waitFor(() => {
2727
expect(queryByTestId('avatar-image')).toBeTruthy();
2828
expect(queryByTestId('avatar-text')).toBeFalsy();
2929
});
@@ -37,7 +37,7 @@ describe('Avatar', () => {
3737
/>,
3838
);
3939

40-
await wait(() => {
40+
await waitFor(() => {
4141
expect(queryByTestId('avatar-image')).toBeTruthy();
4242
expect(queryByTestId('avatar-text')).toBeFalsy();
4343
});
@@ -46,10 +46,10 @@ describe('Avatar', () => {
4646
it('should render an avatar with no image but a name and default size', async () => {
4747
const { getByTestId, queryByTestId } = render(<Avatar name='Test User' />);
4848

49-
await wait(() => {
49+
await waitFor(() => {
5050
expect(queryByTestId('avatar-image')).toBeFalsy();
5151
expect(queryByTestId('avatar-text')).toBeTruthy();
52-
expect(getNodeText(getByTestId('avatar-text'))).toBe('TU');
52+
expect(getByTestId('avatar-text')).toHaveTextContent('TU');
5353
});
5454
});
5555

@@ -58,10 +58,10 @@ describe('Avatar', () => {
5858
<Avatar name='Test User' size={20} />,
5959
);
6060

61-
await wait(() => {
61+
await waitFor(() => {
6262
expect(queryByTestId('avatar-image')).toBeFalsy();
6363
expect(queryByTestId('avatar-text')).toBeTruthy();
64-
expect(getNodeText(getByTestId('avatar-text'))).toBe('TU');
64+
expect(getByTestId('avatar-text')).toHaveTextContent('TU');
6565
});
6666
});
6767
});

src/components/ChannelPreview/__tests__/ChannelPreview.test.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import React from 'react';
2-
import {
3-
act,
4-
getNodeText,
5-
render,
6-
wait,
7-
waitForElement,
8-
} from '@testing-library/react-native';
2+
import { act, render, waitFor } from '@testing-library/react-native';
93

104
import {
115
dispatchMessageDeletedEvent,
@@ -77,7 +71,7 @@ describe('ChannelPreview', () => {
7771
});
7872
await initializeChannel(c);
7973
const { queryByText } = render(getComponent());
80-
await waitForElement(() => queryByText(message.text));
74+
await waitFor(() => queryByText(message.text));
8175
});
8276

8377
it('should mark channel as read, when message.read event is received for current user', async () => {
@@ -87,14 +81,16 @@ describe('ChannelPreview', () => {
8781

8882
const { getByTestId } = render(getComponent());
8983

90-
await waitForElement(() => getByTestId('channel-id'));
91-
expect(getNodeText(getByTestId('unread-count'))).toBe('20');
84+
await waitFor(() => getByTestId('channel-id'));
85+
86+
expect(getByTestId('unread-count')).toHaveTextContent('20');
87+
9288
act(() => {
9389
dispatchMessageReadEvent(chatClient, clientUser, channel);
9490
});
9591

96-
await wait(() => {
97-
expect(getNodeText(getByTestId('unread-count'))).toBe('0');
92+
await waitFor(() => {
93+
expect(getByTestId('unread-count')).toHaveTextContent('0');
9894
});
9995
});
10096

@@ -112,7 +108,7 @@ describe('ChannelPreview', () => {
112108

113109
const { getByTestId } = render(getComponent());
114110

115-
await waitForElement(() => getByTestId('channel-id'));
111+
await waitFor(() => getByTestId('channel-id'));
116112

117113
const message = generateMessage({
118114
user: clientUser,
@@ -122,8 +118,8 @@ describe('ChannelPreview', () => {
122118
dispatcher(chatClient, message, channel);
123119
});
124120

125-
await wait(() => {
126-
expect(getNodeText(getByTestId('last-event-message'))).toBe(
121+
await waitFor(() => {
122+
expect(getByTestId('last-event-message')).toHaveTextContent(
127123
message.text,
128124
);
129125
});
@@ -136,7 +132,7 @@ describe('ChannelPreview', () => {
136132

137133
const { getByTestId } = render(getComponent());
138134

139-
await waitForElement(() => getByTestId('channel-id'));
135+
await waitFor(() => getByTestId('channel-id'));
140136

141137
const message = generateMessage({
142138
user: clientUser,
@@ -148,8 +144,8 @@ describe('ChannelPreview', () => {
148144
dispatchMessageNewEvent(chatClient, message, channel);
149145
});
150146

151-
await wait(() => {
152-
expect(getNodeText(getByTestId('unread-count'))).toBe('10');
147+
await waitFor(() => {
148+
expect(getByTestId('unread-count')).toHaveTextContent('10');
153149
});
154150
});
155151
});

src/components/ChannelPreview/__tests__/ChannelPreviewMessenger.test.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import React from 'react';
2-
import {
3-
fireEvent,
4-
render,
5-
wait,
6-
waitForElement,
7-
} from '@testing-library/react-native';
2+
import { fireEvent, render, waitFor } from '@testing-library/react-native';
83
import { v4 as uuidv4 } from 'uuid';
94
import truncate from 'lodash/truncate';
105

@@ -66,10 +61,10 @@ describe('ChannelPreviewMessenger', () => {
6661
}),
6762
);
6863

69-
await waitForElement(() => getByTestId('channel-preview-button'));
64+
await waitFor(() => getByTestId('channel-preview-button'));
7065
fireEvent.press(getByTestId('channel-preview-button'));
7166

72-
await wait(() => {
67+
await waitFor(() => {
7368
// eslint-disable-next-line jest/prefer-called-with
7469
expect(setActiveChannel).toHaveBeenCalledTimes(1);
7570
});
@@ -85,7 +80,7 @@ describe('ChannelPreviewMessenger', () => {
8580
}),
8681
);
8782
const { queryByText } = render(getComponent());
88-
await waitForElement(() => queryByText(channelName));
83+
await waitFor(() => queryByText(channelName));
8984
});
9085

9186
it('should render comma separated names of other members, if channel has no name', async () => {
@@ -102,7 +97,7 @@ describe('ChannelPreviewMessenger', () => {
10297
const { queryByText } = render(getComponent());
10398
const expectedDisplayName = `${m1.user.name}, ${m2.user.name}, ${m3.user.name}`;
10499

105-
await waitForElement(() => queryByText(expectedDisplayName));
100+
await waitFor(() => queryByText(expectedDisplayName));
106101
});
107102

108103
it('should render latest message, truncated to length given by latestMessageLength', async () => {
@@ -117,6 +112,6 @@ describe('ChannelPreviewMessenger', () => {
117112
);
118113

119114
const expectedMessagePreview = truncate(message.text, { length: 6 });
120-
await waitForElement(() => queryByText(expectedMessagePreview));
115+
await waitFor(() => queryByText(expectedMessagePreview));
121116
});
122117
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
2-
import { render, wait } from '@testing-library/react-native';
2+
import { render, waitFor } from '@testing-library/react-native';
33

44
import CloseButton from '../CloseButton';
55

66
describe('CloseButton', () => {
77
it('should render a CloseButton', async () => {
88
const { queryByTestId } = render(<CloseButton />);
99

10-
await wait(() => expect(queryByTestId('close-button')).toBeTruthy());
10+
await waitFor(() => expect(queryByTestId('close-button')).toBeTruthy());
1111
});
1212
});

src/utils/__tests__/Streami18n.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const customDayjsLocaleConfig = {
5757
};
5858

5959
describe('Streami18n instance - default', () => {
60-
const streami18nOptions = {};
60+
const streami18nOptions = { logger: () => {} };
6161
const streami18n = new Streami18n(streami18nOptions);
6262

6363
it('should provide default english translator', async () => {

0 commit comments

Comments
 (0)