Skip to content

Instantly share code, notes, and snippets.

@ernestofreyreg
Created March 22, 2020 23:49
Show Gist options
  • Save ernestofreyreg/14ef96ce8d52069c4accbefa89287d8a to your computer and use it in GitHub Desktop.
Save ernestofreyreg/14ef96ce8d52069c4accbefa89287d8a to your computer and use it in GitHub Desktop.
import * as React from 'react'
import { render } from '@testing-library/react'
const DemoContext = React.createContext<any>(null)
describe('Demo', () => {
const setup = DummyComponent => {
const update = jest.fn()
const utils = render(
<DemoContext.Provider value={{ update }}>
<DummyComponent />
</DemoContext.Provider>
)
return {
update,
...utils
}
}
it('do not call update', async () => {
const DummyUpdate: React.FC = () => {
return null
}
const { update } = setup(DummyUpdate)
expect(update).not.toHaveBeenCalled()
})
it('do call update', async () => {
const DummyUpdate: React.FC = () => {
const demoContext = React.useContext(DemoContext)
React.useEffect(() => {
demoContext.update()
})
return null
}
const { update } = setup(DummyUpdate)
expect(update).toHaveBeenCalled()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment