-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'react-testing-library/cleanup-after-each'; | ||
import React from 'react'; | ||
import { CSSTransition } from 'react-transition-group'; | ||
import { render, fireEvent } from 'react-testing-library'; | ||
import HiddenMessage from './hidden-message'; | ||
|
||
// NOTE: you do NOT have to do this in every test. | ||
// Learn more about Jest's __mocks__ directory: | ||
// https://jestjs.io/docs/en/manual-mocks | ||
jest.mock('react-transition-group', () => { | ||
return { | ||
CSSTransition: jest.fn(({ children, in: show }) => (show ? children : null)) | ||
}; | ||
}); | ||
test('you can mock things with jest.mock', () => { | ||
const { getByText, queryByText } = render( | ||
<HiddenMessage initialShow={true} /> | ||
); | ||
const toggleButton = getByText('Toggle'); | ||
const context = expect.any(Object); | ||
const children = expect.any(Object); | ||
const defaultProps = { children, timeout: 1000, className: 'fade' }; | ||
expect(CSSTransition).toHaveBeenCalledWith( | ||
{ in: true, ...defaultProps }, | ||
context | ||
); | ||
expect(getByText('Hello world')).not.toBeNull(); | ||
CSSTransition.mockClear(); | ||
fireEvent.click(toggleButton); | ||
expect(queryByText('Hello world')).toBeNull(); | ||
expect(CSSTransition).toHaveBeenCalledWith( | ||
{ in: false, ...defaultProps }, | ||
context | ||
); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from 'react'; | ||
import { CSSTransition } from 'react-transition-group'; | ||
|
||
function Fade(props: { children: any; in: boolean }) { | ||
return ( | ||
<CSSTransition in={props.in} timeout={1000} className="fade"> | ||
{props.children} | ||
</CSSTransition> | ||
); | ||
} | ||
|
||
export default class HiddenMessage extends React.Component< | ||
{ | ||
initialShow: boolean; | ||
}, | ||
{ show: boolean } | ||
> { | ||
static defaultProps = { initialShow: false }; | ||
state = { show: this.props.initialShow }; | ||
toggle = () => { | ||
this.setState(({ show }) => ({ show: !show })); | ||
}; | ||
render() { | ||
return ( | ||
<div> | ||
<button onClick={this.toggle}>Toggle</button> | ||
<Fade in={this.state.show}> | ||
<div>Hello world</div> | ||
</Fade> | ||
</div> | ||
); | ||
} | ||
} |
0c16077
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i never use shallow rendering
sometime, npm test -- --coverage will only generated coverage report of new/updated components
instead, npm test -- --coverage --watchAll=false
write tests. not too many. mostly integration.