-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from fede-rodes/fede
tests for loginApiCall
- Loading branch information
Showing
2 changed files
with
62 additions
and
17 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,8 +1,7 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { mount, shallow } from 'enzyme'; | ||
import { mount } from 'enzyme'; | ||
import { ApolloProvider } from 'react-apollo'; | ||
import { MockedProvider } from 'react-apollo/test-utils'; | ||
import wait from 'waait'; | ||
import mockClient from '../../../graphql/apollo-mock-client'; | ||
import LoginApiCall from '.'; | ||
|
@@ -24,7 +23,35 @@ describe('LoginApiCall', () => { | |
}); | ||
|
||
it('calls login mutation passing email and passcode', async () => { | ||
jest.spyOn(mockClient, 'mutate'); | ||
const spy = jest.spyOn(mockClient, 'mutate'); | ||
|
||
const wrapper = mount( | ||
<ApolloProvider client={mockClient} addTypename={false}> | ||
<LoginApiCall email="[email protected]"> | ||
{({ loginUser }) => ( | ||
<button | ||
type="button" | ||
onClick={() => { loginUser({ passcode: '123456' }); }} | ||
> | ||
Click | ||
</button> | ||
)} | ||
</LoginApiCall> | ||
</ApolloProvider>, | ||
); | ||
|
||
wrapper.find('button').simulate('click'); | ||
await wait(0); | ||
|
||
expect(spy).toBeCalled(); | ||
expect(spy.mock.calls[0][0].variables).toMatchObject({ | ||
email: '[email protected]', | ||
passcode: parseInt('123456', 10), | ||
}); | ||
spy.mockRestore(); | ||
}); | ||
|
||
it('calls onLoginSuccess when passing valid email and passcode', async () => { | ||
const handleLoginSuccess = jest.fn(); | ||
const handleLoginError = jest.fn(); | ||
|
||
|
@@ -49,28 +76,41 @@ describe('LoginApiCall', () => { | |
|
||
wrapper.find('button').simulate('click'); | ||
await wait(0); | ||
// await new Promise(resolve => setTimeout(resolve)); | ||
// wrapper.update(); | ||
|
||
console.log(mockClient.mutate); | ||
|
||
expect(mockClient.mutate).toBeCalled(); | ||
expect(mockClient.mutate.mock.calls[0][0].variables).toMatchObject({ | ||
email: '[email protected]', | ||
passcode: parseInt('123456', 10), | ||
}); | ||
|
||
expect(handleLoginSuccess).toBeCalled(); | ||
expect(handleLoginSuccess).toBeCalledWith( | ||
expect.objectContaining({ token: 'xyz123' }), | ||
); | ||
expect(handleLoginError).not.toBeCalled(); | ||
}); | ||
|
||
it('calls onLoginSuccess when passing valid email and passcode', () => { | ||
it('calls onLoginError when passing invalid email and/or passcode', async () => { | ||
const handleLoginSuccess = jest.fn(); | ||
const handleLoginError = jest.fn(); | ||
|
||
}); | ||
const wrapper = mount( | ||
<ApolloProvider client={mockClient} addTypename={false}> | ||
<LoginApiCall | ||
email="[email protected]" | ||
onLoginError={handleLoginError} | ||
onLoginSuccess={handleLoginSuccess} | ||
> | ||
{({ loginUser }) => ( | ||
<button | ||
type="button" | ||
onClick={() => { loginUser({ passcode: '123456' }); }} | ||
> | ||
Click | ||
</button> | ||
)} | ||
</LoginApiCall> | ||
</ApolloProvider>, | ||
); | ||
|
||
it('calls onLoginError when passing invalid email and/or passcode', () => { | ||
wrapper.find('button').simulate('click'); | ||
await wait(0); | ||
|
||
expect(handleLoginSuccess).not.toBeCalled(); | ||
expect(handleLoginError).toBeCalled(); | ||
}); | ||
}); |
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,6 +1,11 @@ | ||
const resolvers = { | ||
Mutation: { | ||
login: () => ({ _id: '123', token: 'xyz123' }), | ||
login: (parent, { email, passcode }) => { | ||
if (email && email === '[email protected]' && passcode && passcode === 123456) { | ||
return { _id: '123', token: 'xyz123' }; | ||
} | ||
throw new Error(); | ||
}, | ||
}, | ||
}; | ||
|
||
|