Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
Update to appropriate test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
hyochan committed Feb 9, 2019
1 parent 5363c54 commit c5d80d3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/components/screen/Intro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ interface IState {
class Page extends Component<IProps, IState> {
private timer: any;

constructor(props) {
constructor(props: IProps) {
super(props);
this.state = {
isLoggingIn: false,
Expand Down
35 changes: 21 additions & 14 deletions src/components/screen/__tests__/Intro.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import appStore from '../../../stores/appStore';

// Note: test renderer must be required after react-native.
import renderer, { ReactTestRenderer, ReactTestRendererJSON } from 'react-test-renderer';
import * as ShallowRenderer from 'react-test-renderer/shallow';
import User from '../../../models/User';
import { Provider } from 'mobx-react/native';

const props = {
store: appStore,
Expand All @@ -23,41 +26,45 @@ describe('Intro page DOM rendering test', () => {
tree = renderer.create(component).toJSON();
expect(tree).toMatchSnapshot();
});

it('shallow renders match snapshot', () => {
const shallow = ShallowRenderer.createRenderer();
const result = shallow.render(component);
expect(result).toMatchSnapshot();
});
});

describe('Interaction', () => {
let rendered: any;
let root: ReactTestRenderer['root'] | any;
const component = <Intro { ...props }/>;
const component = (
<Intro { ...props }/>
);

beforeEach(() => {
rendered = renderer.create(component);
root = rendered.root;
});

it('simulate click', () => {
// root.instance.state = {
// isLoggingIn: false,
// };

jest.useFakeTimers();
// const spy = jest.spyOn(instance.getInstance(), 'onLogin');
const spy = jest.spyOn(root.instance.wrappedInstance, 'onLogin');
const buttons = root.findAllByType(Button);
buttons[0].props.onPress();
root.instance.wrappedInstance.onLogin(); // == buttons[0].props.onPress();
expect(setTimeout).toHaveBeenCalledTimes(1);
// expect(rendered.getInstance().state.isLoggingIn).toEqual(true);
expect(root.instance.wrappedInstance.state.isLoggingIn).toEqual(true);

jest.runAllTimers();
// expect(rendered.getInstance().state.isLoggingIn).toEqual(false);
expect(root.instance.wrappedInstance.state.isLoggingIn).toEqual(false);
expect(props.store.user.displayName).toEqual('dooboolab');
expect(props.store.user.age).toEqual(30);
expect(props.store.user.job).toEqual('developer');
// expect(spy).toBeCalled();
buttons[1].props.onPress();
expect(spy).toHaveBeenCalled();
expect(props.navigation.navigate).toBeCalledWith('Temp');
});

// it('clearTimeout on componentWillUnmount', async () => {
// await rendered.getInstance().componentWillUnmount();
// expect(clearTimeout).toBeCalled();
// });
it('componentWillUnmount', () => {
rendered.unmount();
});
});
19 changes: 19 additions & 0 deletions src/components/screen/__tests__/__snapshots__/Intro.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,22 @@ exports[`Intro page DOM rendering test component and snapshot matches 1`] = `
</View>
</View>
`;

exports[`Intro page DOM rendering test shallow renders match snapshot 1`] = `
<Page
navigation={
Object {
"navigate": [MockFunction],
}
}
store={
ObservableListStore {
"_user": User {
"_age": undefined,
"_displayName": undefined,
"_job": undefined,
},
}
}
/>
`;

0 comments on commit c5d80d3

Please sign in to comment.