forked from bamlab/react-native-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscription.test.tsx
More file actions
54 lines (49 loc) · 1.93 KB
/
Subscription.test.tsx
File metadata and controls
54 lines (49 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React from 'react';
import { fireEvent, waitForElement } from 'react-native-testing-library';
import fetchMock from 'fetch-mock';
import 'jest-styled-components';
import { Subscription } from '../Subscription';
import { renderPage, getPropsWithNavigation } from '../../../utils/tests/helpers';
import { EMAIL_API_ENDPOINT } from '../../../api/config';
import { wording } from '../../../utils/wording';
describe('[Page] Home', () => {
const props = getPropsWithNavigation();
const mockCallSubscribe = (status: number) => {
fetchMock.post(EMAIL_API_ENDPOINT, status);
};
beforeEach(() => {
fetchMock.reset();
});
it('should display succesful message on successful subscription', async () => {
// SETUP
mockCallSubscribe(200);
const page = renderPage(<Subscription {...props} />);
// GIVEN
const EmailInput = page.getByPlaceholder(wording.emailPlaceholder);
const ValidateButton = page.getByText(wording.validateEmail);
// Careful, getByPlaceholder might not work depending on the input you use.
// I don't think it works with react-native-paper for instance
// WHEN
fireEvent.changeText(EmailInput, 'hello@bam.com');
fireEvent.press(ValidateButton);
// THEN
const SuccessMessage = await waitForElement(() =>
page.queryByText(wording.subscriptionSuccessful)
);
expect(SuccessMessage).toBeTruthy();
});
it('should display error message on failed subscription', async () => {
// SETUP
mockCallSubscribe(400);
const page = renderPage(<Subscription {...props} />);
// GIVEN
const EmailInput = page.getByPlaceholder(wording.emailPlaceholder);
const ValidateButton = page.getByText(wording.validateEmail);
// WHEN
fireEvent.changeText(EmailInput, 'hello@bamom');
fireEvent.press(ValidateButton);
// THEN
const ErrorMessage = await waitForElement(() => page.queryByText(wording.basicError));
expect(ErrorMessage).toBeTruthy();
});
});