Skip to content

Commit 8a7b9c8

Browse files
authored
Merge pull request #74 from oslabs-beta/josh/travel-test
added unit test for travelcontainer
2 parents 68817cd + 20e52ab commit 8a7b9c8

File tree

2 files changed

+83
-97
lines changed

2 files changed

+83
-97
lines changed

src/app/__tests__/TravelContainer.test.js

Lines changed: 76 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,112 +3,103 @@ import { shallow, configure } from 'enzyme';
33
import React from 'react';
44
import Adapter from 'enzyme-adapter-react-16';
55
import TravelContainer from '../containers/TravelContainer';
6-
// import MainContainer from '../containers/MainContainer';
7-
// import Dropdown from '../components/Dropdown';
8-
6+
import MainSlider from '../components/MainSlider';
7+
import Dropdown from '../components/Dropdown';
8+
import { useStoreContext } from '../store';
9+
import { moveBackward, moveForward } from '../actions/actions';
910

1011
configure({ adapter: new Adapter() });
1112

12-
const props = {
13-
moveBackward: jest.fn(),
14-
moveForward: jest.fn(),
15-
snapshotsLength: 5,
16-
handleChangeSnapshot: jest.fn(),
17-
handleJumpSnapshot: jest.fn(),
18-
snapshotIndex: 6,
19-
play: jest.fn(),
20-
playing: false,
21-
pause: jest.fn(),
13+
const state = {
14+
tabs: {
15+
87: {
16+
snapshots: [1, 2, 3, 4],
17+
sliderIndex: 2,
18+
playing: true,
19+
},
20+
},
21+
currentTab: 87,
2222
};
2323

24-
// These are fake props to be used during dropdown tests
24+
const dispatch = jest.fn();
25+
jest.mock('../store');
26+
useStoreContext.mockImplementation(() => [state, dispatch]);
2527

26-
// const dropdownProps = {
27-
// selectedOption: {
28-
// value: 1,
29-
// label: 'label',
30-
// },
31-
// options: [0.5, 1, 2],
32-
// onChange: jest.fn(),
33-
// };
28+
let wrapper;
3429

35-
// const options = [
36-
// { value: 2000, label: '0.5x' },
37-
// { value: 1000, label: '1.0x' },
38-
// { value: 500, label: '2.0x' },
39-
// ];
30+
beforeEach(() => {
31+
wrapper = shallow(<TravelContainer snapshotsLength={2} />);
32+
useStoreContext.mockClear();
33+
dispatch.mockClear();
34+
});
4035

41-
describe('testing the backward and forward buttons', () => {
42-
test('if the backward button rewinds the playback', () => {
43-
const wrapper = shallow(<TravelContainer {...props} />);
36+
describe('<TravelContainer /> rendering', () => {
37+
test('should render three buttons', () => {
38+
expect(wrapper.find('button')).toHaveLength(3);
39+
});
40+
test('should render one MainSlider', () => {
41+
expect(wrapper.find(MainSlider)).toHaveLength(1);
42+
});
43+
test('should render one Dropdown', () => {
44+
expect(wrapper.find(Dropdown)).toHaveLength(1);
45+
});
46+
});
4447

48+
describe('testing the backward-button', () => {
49+
test('should dispatch action upon click', () => {
4550
wrapper.find('.backward-button').simulate('click');
46-
47-
expect(props.moveBackward).toHaveBeenCalled();
51+
expect(dispatch.mock.calls.length).toBe(1);
4852
});
4953

50-
test('if the forward button forwards the playback', () => {
51-
const wrapper = shallow(<TravelContainer {...props} />);
52-
53-
wrapper.find('.forward-button').simulate('click');
54-
55-
expect(props.moveForward).toHaveBeenCalled();
54+
test('should send moveBackward action to dispatch', () => {
55+
wrapper.find('.backward-button').simulate('click');
56+
expect(dispatch.mock.calls[0][0]).toEqual(moveBackward());
5657
});
5758
});
5859

59-
describe('testing the play button', () => {
60-
test('if the play button starts the playback', () => {
61-
const wrapper = shallow(<TravelContainer {...props} />);
62-
63-
wrapper.find('.play-button').simulate('click');
64-
65-
expect(props.play).toHaveBeenCalled();
60+
describe('testing the forward-button', () => {
61+
test('should dispatch action upon click', () => {
62+
wrapper.find('.forward-button').simulate('click');
63+
expect(dispatch.mock.calls.length).toBe(1);
6664
});
6765

68-
test("if playback is not running, the button should state 'Play'", () => {
69-
const wrapper = shallow(<TravelContainer {...props} />);
70-
71-
wrapper.find('.play-button');
72-
73-
expect(wrapper.find('.play-button').text()).toBe('Play');
66+
test('should send moveforward action to dispatch', () => {
67+
wrapper.find('.forward-button').simulate('click');
68+
expect(dispatch.mock.calls[0][0]).toEqual(moveForward());
7469
});
70+
});
7571

76-
test("if playback is running, the button should state 'Pause'", () => {
77-
props.playing = true;
78-
79-
const wrapper = shallow(<TravelContainer {...props} />);
80-
81-
wrapper.find('.play-button');
82-
72+
describe('testing the play-button', () => {
73+
test("should display 'pause' if playing is true", () => {
74+
state.tabs[87].playing = true;
75+
wrapper = shallow(<TravelContainer snapshotsLength={2} />);
8376
expect(wrapper.find('.play-button').text()).toBe('Pause');
8477
});
85-
});
86-
87-
describe('testing the playback speed', () => {
88-
test('if the playback dropdown states 0.5x the speed should be 0.5x', () => {
89-
const wrapper = shallow(<TravelContainer {...props} />);
9078

91-
wrapper.find('Dropdown').simulate('change', { value: ['val'] });
92-
// wrapper.find('select').simulate('change', { value : 'hello'});
93-
// console.log('val',wrapper.find('Dropdown').simulate('select', { value: ['val'] }));
94-
// expect(wrapper.find('Dropdown').text()).toBe('0.5x')
95-
expect(wrapper.find('select [selected]').val()).toEqual('key');
79+
test('should display play if playing is false', () => {
80+
state.tabs[87].playing = false;
81+
wrapper = shallow(<TravelContainer snapshotsLength={2} />);
82+
expect(wrapper.find('.play-button').text()).toBe('Play');
9683
});
97-
// test('if the playback dropdown states 1x the speed should be 1x', () => {
98-
99-
// const wrapper = shallow(<TravelContainer { ...dropdownProps } />);
100-
101-
// expect(wrapper.find('Dropdown').label).toBe('1.0x')
102-
103-
// });
104-
105-
// test('if the playback dropdown states 2x the speed should be 2x', () => {
106-
107-
// const wrapper = shallow(<TravelContainer { ...dropdownProps } />);
108-
109-
// wrapper.find('Dropdown').simulate('click');
110-
111-
// expect(wrapper.find('Dropdown').label).toBe('2.0x')
112-
113-
// });
11484
});
85+
86+
// describe('testing the playback speed', () => {
87+
// test('if the playback dropdown states 0.5x the speed should be 0.5x', () => {
88+
// const wrapper = shallow(<TravelContainer {...props} />);
89+
// wrapper.find('Dropdown').simulate('change', { value: ['val'] });
90+
// wrapper.find('select').simulate('change', { value: 'hello' });
91+
// console.log('val', wrapper.find('Dropdown').simulate('select', { value: ['val'] }));
92+
// expect(wrapper.find('Dropdown').text()).toBe('0.5x');
93+
// expect(wrapper.find('select [selected]').val()).toEqual('key');
94+
// });
95+
// test('if the playback dropdown states 1x the speed should be 1x', () => {
96+
// const wrapper = shallow(<TravelContainer {...dropdownProps} />);
97+
98+
// expect(wrapper.find('Dropdown').label).toBe('1.0x');
99+
// });
100+
// test('if the playback dropdown states 2x the speed should be 2x', () => {
101+
// const wrapper = shallow(<TravelContainer {...dropdownProps} />);
102+
// wrapper.find('Dropdown').simulate('click');
103+
// expect(wrapper.find('Dropdown').label).toBe('2.0x');
104+
// });
105+
// });

src/app/containers/TravelContainer.jsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,25 @@ function TravelContainer({ snapshotsLength }) {
3838

3939
return (
4040
<div className="travel-container">
41-
<button className="play-button" type="button" onClick={() => play(selectedSpeed.value, playing, dispatch, snapshotsLength, sliderIndex)}>
41+
<button
42+
className="play-button"
43+
type="button"
44+
onClick={() => play(selectedSpeed.value, playing, dispatch, snapshotsLength, sliderIndex)}
45+
>
4246
{playing ? 'Pause' : 'Play'}
4347
</button>
44-
<MainSlider
45-
snapshotsLength={snapshotsLength}
46-
sliderIndex={sliderIndex}
47-
dispatch={dispatch}
48-
/>
48+
<MainSlider snapshotsLength={snapshotsLength} sliderIndex={sliderIndex} dispatch={dispatch} />
4949
<button className="backward-button" onClick={() => dispatch(moveBackward())} type="button">
5050
{'<'}
5151
</button>
5252
<button className="forward-button" onClick={() => dispatch(moveForward())} type="button">
5353
{'>'}
5454
</button>
55-
<Dropdown
56-
speeds={speeds}
57-
selectedSpeed={selectedSpeed}
58-
setSpeed={setSpeed}
59-
/>
55+
<Dropdown speeds={speeds} selectedSpeed={selectedSpeed} setSpeed={setSpeed} />
6056
</div>
6157
);
6258
}
6359

64-
6560
TravelContainer.propTypes = {
6661
snapshotsLength: PropTypes.number.isRequired,
6762
};

0 commit comments

Comments
 (0)