Skip to content

Commit 0c16077

Browse files
committed
kent example
1 parent 52cb196 commit 0c16077

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"react": "^16.8.6",
1414
"react-dom": "^16.8.6",
1515
"react-scripts": "3.0.1",
16+
"react-transition-group": "^4.0.1",
1617
"rxjs": "^6.5.2",
1718
"typescript": "3.4.5"
1819
},
@@ -41,6 +42,7 @@
4142
"@types/axios": "^0.14.0",
4243
"@types/enzyme": "^3.9.2",
4344
"@types/ramda": "^0.26.8",
45+
"@types/react-transition-group": "^2.9.1",
4446
"enzyme": "^3.9.0",
4547
"enzyme-adapter-react-16": "^1.13.0",
4648
"jest-dom": "^3.2.2",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'react-testing-library/cleanup-after-each';
2+
import React from 'react';
3+
import { CSSTransition } from 'react-transition-group';
4+
import { render, fireEvent } from 'react-testing-library';
5+
import HiddenMessage from './hidden-message';
6+
7+
// NOTE: you do NOT have to do this in every test.
8+
// Learn more about Jest's __mocks__ directory:
9+
// https://jestjs.io/docs/en/manual-mocks
10+
jest.mock('react-transition-group', () => {
11+
return {
12+
CSSTransition: jest.fn(({ children, in: show }) => (show ? children : null))
13+
};
14+
});
15+
test('you can mock things with jest.mock', () => {
16+
const { getByText, queryByText } = render(
17+
<HiddenMessage initialShow={true} />
18+
);
19+
const toggleButton = getByText('Toggle');
20+
const context = expect.any(Object);
21+
const children = expect.any(Object);
22+
const defaultProps = { children, timeout: 1000, className: 'fade' };
23+
expect(CSSTransition).toHaveBeenCalledWith(
24+
{ in: true, ...defaultProps },
25+
context
26+
);
27+
expect(getByText('Hello world')).not.toBeNull();
28+
CSSTransition.mockClear();
29+
fireEvent.click(toggleButton);
30+
expect(queryByText('Hello world')).toBeNull();
31+
expect(CSSTransition).toHaveBeenCalledWith(
32+
{ in: false, ...defaultProps },
33+
context
34+
);
35+
});

src/testing-lib/hidden-message.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react';
2+
import { CSSTransition } from 'react-transition-group';
3+
4+
function Fade(props: { children: any; in: boolean }) {
5+
return (
6+
<CSSTransition in={props.in} timeout={1000} className="fade">
7+
{props.children}
8+
</CSSTransition>
9+
);
10+
}
11+
12+
export default class HiddenMessage extends React.Component<
13+
{
14+
initialShow: boolean;
15+
},
16+
{ show: boolean }
17+
> {
18+
static defaultProps = { initialShow: false };
19+
state = { show: this.props.initialShow };
20+
toggle = () => {
21+
this.setState(({ show }) => ({ show: !show }));
22+
};
23+
render() {
24+
return (
25+
<div>
26+
<button onClick={this.toggle}>Toggle</button>
27+
<Fade in={this.state.show}>
28+
<div>Hello world</div>
29+
</Fade>
30+
</div>
31+
);
32+
}
33+
}

0 commit comments

Comments
 (0)