Skip to content

Commit

Permalink
kent example
Browse files Browse the repository at this point in the history
  • Loading branch information
lugithub committed May 21, 2019
1 parent 52cb196 commit 0c16077
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1",
"react-transition-group": "^4.0.1",
"rxjs": "^6.5.2",
"typescript": "3.4.5"
},
Expand Down Expand Up @@ -41,6 +42,7 @@
"@types/axios": "^0.14.0",
"@types/enzyme": "^3.9.2",
"@types/ramda": "^0.26.8",
"@types/react-transition-group": "^2.9.1",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.13.0",
"jest-dom": "^3.2.2",
Expand Down
35 changes: 35 additions & 0 deletions src/testing-lib/hidden-message.test.tsx
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
);
});
33 changes: 33 additions & 0 deletions src/testing-lib/hidden-message.tsx
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>
);
}
}

1 comment on commit 0c16077

@lugithub
Copy link
Owner Author

@lugithub lugithub commented on 0c16077 May 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.