Created
March 22, 2020 23:49
-
-
Save ernestofreyreg/14ef96ce8d52069c4accbefa89287d8a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 * 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