-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
caea2c6
commit 5e78818
Showing
32 changed files
with
82 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# testing-app | ||
# Testing App | ||
|
||
Demo for react native testing library | ||
|
||
Please don't judge me for coding practices using this code base. I am better than this; might be. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,35 +12,53 @@ import { | |
|
||
describe('<EmailPasswordForm />', () => { | ||
it('Expect to show password required', async () => { | ||
// Dummy email value | ||
const email = '[email protected]'; | ||
|
||
// Grabbing our parent component | ||
const { getByTestId } = render( | ||
<EmailPasswordForm | ||
onSubmit={() => null} | ||
onForgotPasswordPress={() => null} | ||
/>, | ||
); | ||
|
||
// Grabbing our input & button components | ||
const emailInput = getByTestId(TEST_ID_EMAIL_INPUT); | ||
const button = getByTestId(TEST_ID_SUBMIT_BUTTON); | ||
|
||
/** | ||
* We are changing text in inputs here; that requires a state change | ||
* Since setState is async in react we have to execute this tests | ||
* in async way. RNTL give waitFor API for this. | ||
*/ | ||
await waitFor(() => { | ||
fireEvent.changeText(emailInput, email); | ||
// Just making sure that value is updated in input | ||
expect(emailInput.props.value).toBe(email); | ||
fireEvent.press(button); | ||
|
||
// We have passwordInput_ERROR component that only renders when error is there | ||
expect(getByTestId('passwordInput_ERROR')).toBeDefined(); | ||
}); | ||
}); | ||
|
||
it('Expect to call handle submit with email & password', async () => { | ||
// Dummy inputs | ||
const email = '[email protected]'; | ||
const password = 'qwerty1234'; | ||
|
||
// Expected output | ||
const expectedOutput = { | ||
email, | ||
password, | ||
}; | ||
let output = {}; | ||
|
||
// Mock onSubmit method that we are expecting will be executed | ||
const onSubmit = jest.fn((data) => (output = data)); | ||
|
||
// Rendering our component & grabbing required nodes. | ||
const { getByTestId } = render( | ||
<EmailPasswordForm | ||
onSubmit={onSubmit} | ||
|
@@ -51,13 +69,18 @@ describe('<EmailPasswordForm />', () => { | |
const emailInput = getByTestId(TEST_ID_EMAIL_INPUT); | ||
const passwordInput = getByTestId(TEST_ID_PASSWORD_INPUT); | ||
|
||
// Testing behaviors | ||
await waitFor(() => { | ||
fireEvent.changeText(emailInput, email); | ||
expect(emailInput.props.value).toBe(email); | ||
|
||
fireEvent.changeText(passwordInput, password); | ||
expect(passwordInput.props.value).toBe(password); | ||
|
||
/** | ||
* Here we are asserting that onSubmit is not just called | ||
* but it is called with expected output. | ||
*/ | ||
fireEvent.press(button); | ||
expect(onSubmit).toHaveBeenCalledTimes(1); | ||
expect(output).toEqual(expectedOutput); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,28 +20,32 @@ import { | |
} from '../constants'; | ||
|
||
describe('Login Screen', () => { | ||
afterEach(() => { | ||
fetchMock.reset(); | ||
fetchMock.restore(); | ||
}); | ||
|
||
it('Expect to save token in AsyncStorage & navigate to home screen on successful login', async () => { | ||
// Setting up fetch mock before execution of any test | ||
beforeAll(() => { | ||
const endPoint = `${API_URL}${LOGIN_ENDPOINT}`; | ||
fetchMock.post(endPoint, { | ||
status: 200, | ||
body: JSON.stringify(LOGIN_EXPECTED_RESPONSE), | ||
}); | ||
}); | ||
|
||
// Testing complete flow | ||
it('Expect to save token in AsyncStorage & navigate to home screen on successful login', async () => { | ||
// Mocking navigate method | ||
const navigate = jest.fn(); | ||
const endPoint = `${API_URL}${LOGIN_ENDPOINT}`; | ||
|
||
// Dummy data to supply to form | ||
const email = '[email protected]'; | ||
const password = 'password'; | ||
|
||
// Getting element | ||
const screen = render(<LoginScreen navigation={{ navigate }} />); | ||
const emailInput = screen.getByTestId(TEST_ID_EMAIL_INPUT); | ||
const passwordInput = screen.getByTestId(TEST_ID_PASSWORD_INPUT); | ||
const button = screen.getByTestId(TEST_ID_SUBMIT_BUTTON); | ||
|
||
// Formik requires all state changes to be wrapper in async method. | ||
await waitFor(() => { | ||
fireEvent.changeText(emailInput, email); | ||
fireEvent.changeText(passwordInput, password); | ||
|
@@ -51,16 +55,27 @@ describe('Login Screen', () => { | |
fireEvent.press(button); | ||
}); | ||
|
||
// Asserting that API has been called | ||
expect(fetchMock).toHaveBeenCalledWith(endPoint, { | ||
body: `{"email":"${email}","password":"${password}"}`, | ||
headers: { 'Content-Type': 'application/json' }, | ||
method: 'POST', | ||
}); | ||
|
||
// Asserting screen navigation | ||
expect(navigate).toHaveBeenCalledTimes(1); | ||
expect(navigate).toHaveBeenCalledWith(HOME, {}); | ||
|
||
// Asserting token storage in local storage. | ||
expect(AsyncStorage.setItem).toHaveBeenCalledWith( | ||
AUTH_TOKEN_KEY, | ||
LOGIN_EXPECTED_RESPONSE.data.tokens.jwtToken, | ||
); | ||
}); | ||
|
||
// Cleaning up fetch mock | ||
afterEach(() => { | ||
fetchMock.reset(); | ||
fetchMock.restore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.