Created
June 22, 2020 13:43
-
-
Save rafaelcavalcante/9a4953dede0f9e3814464446b25eb65a to your computer and use it in GitHub Desktop.
Mocked react-native-contacts
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
jest.mock('react-native-gesture-handler') | |
const contacts = [ | |
{ | |
displayName: 'Abel Tesfaye', | |
cellphone: '11971033230' | |
}, | |
{ | |
givenName: 'Chancelor ', | |
familyName: 'Bennett', | |
cellphone: '11971033231' | |
} | |
] | |
export default { | |
getAll: fn => fn(null, contacts) | |
} |
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
import '@testing-library/jest-native/extend-expect' | |
import React from 'react' | |
import { useFocusEffect } from '@react-navigation/native' | |
import { ApolloProvider } from '@apollo/react-hooks' | |
import { createMockClient } from 'mock-apollo-client' | |
import Contacts from 'react-native-contacts' | |
import { render, wait } from 'test-utils' | |
import DorisAnalytics from '~/services/DorisAnalytics' | |
import ContactsScreen from './index' | |
const setMock = jest.fn() | |
const trackMock = jest.fn() | |
const navigateMock = jest.fn() | |
const navigationMock = { | |
navigate: navigateMock | |
} | |
jest.mock('react-native-contacts') | |
describe('the ContactsScreen screen', () => { | |
jest.spyOn(DorisAnalytics, 'track').mockImplementation(trackMock) | |
jest.spyOn(DorisAnalytics, 'setPeople').mockImplementation(setMock) | |
beforeEach(() => { | |
jest.clearAllMocks() | |
}) | |
it('should return users from address book', async () => { | |
const mockClient = createMockClient() | |
const useFocusEffectCallback = jest.fn(callback => callback()) | |
useFocusEffect.mockImplementationOnce(useFocusEffectCallback) | |
Contacts.getAll = jest.fn(_ => | |
Promise.resolve([ | |
{ | |
displayName: 'Abel Tesfaye', | |
cellphone: '11971033230' | |
}, | |
{ | |
givenName: 'Chancelor ', | |
familyName: 'Bennett', | |
cellphone: '11971033231' | |
} | |
]) | |
) | |
const { getByTestId } = render( | |
<ApolloProvider client={mockClient}> | |
<ContactsScreen navigation={navigationMock} /> | |
</ApolloProvider> | |
) | |
await wait() | |
const loading = getByTestId('DorisLoadingWrapper') | |
expect(loading).toBeDefined() | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment