Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelcavalcante/9a4953dede0f9e3814464446b25eb65a to your computer and use it in GitHub Desktop.
Save rafaelcavalcante/9a4953dede0f9e3814464446b25eb65a to your computer and use it in GitHub Desktop.
Mocked react-native-contacts
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)
}
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