Skip to content

Commit bbf7e30

Browse files
committed
Unit test <Train/>
1 parent 08a664f commit bbf7e30

File tree

1 file changed

+123
-1
lines changed

1 file changed

+123
-1
lines changed

test/unit/oceans/ui.test.js

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import {shallow} from 'enzyme';
44
import sinon from 'sinon';
5-
import {Button, ConfirmationDialog, Words, wordSet} from '@ml/oceans/ui';
5+
import {Button, ConfirmationDialog, Words, wordSet, Train} from '@ml/oceans/ui';
66
import guide from '@ml/oceans/models/guide';
77
import soundLibrary from '@ml/oceans/models/soundLibrary';
8+
import train from '@ml/oceans/models/train';
89
import modeHelpers from '@ml/oceans/modeHelpers';
910
import {setState, getState, resetState} from '@ml/oceans/state';
1011
import {AppMode, Modes} from '@ml/oceans/constants';
@@ -197,3 +198,124 @@ describe('Words', () => {
197198
});
198199
});
199200
});
201+
202+
describe('Train', () => {
203+
let classifyFishStub;
204+
205+
beforeEach(() => {
206+
classifyFishStub = sinon.stub(train, 'onClassifyFish');
207+
});
208+
209+
afterEach(() => {
210+
train.onClassifyFish.restore();
211+
});
212+
213+
it('displays the current training count', () => {
214+
setState({yesCount: 10, noCount: 23});
215+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
216+
const trainCount = wrapper.find('#uitest-train-count');
217+
218+
expect(trainCount.text()).toEqual('33');
219+
});
220+
221+
it('displays 999 if current training count is greater than 999', () => {
222+
setState({yesCount: 1000, noCount: 1});
223+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
224+
const trainCount = wrapper.find('#uitest-train-count');
225+
226+
expect(trainCount.text()).toEqual('999');
227+
});
228+
229+
it('sets state to display confirmation dialog when erase icon is clicked', () => {
230+
const initialState = getState();
231+
expect(initialState.showConfirmationDialog).toBeFalsy();
232+
expect(initialState.confirmationDialogOnYes).toBeNull();
233+
234+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
235+
const eraseIcon = wrapper.find('FontAwesomeIcon').at(0);
236+
eraseIcon.simulate('click');
237+
238+
const newState = getState();
239+
expect(newState.showConfirmationDialog).toBeTruthy();
240+
expect(newState.confirmationDialogOnYes).not.toBeNull();
241+
});
242+
243+
describe('train "no" button', () => {
244+
const getNoButton = wrapper => wrapper.find('Button').at(0);
245+
246+
it('displays the current word when not in AppMode.CreaturesVTrash', () => {
247+
setState({appMode: 'not-creatures-v-trash', word: 'Spooky'});
248+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
249+
const noButton = getNoButton(wrapper).render();
250+
251+
expect(noButton.text().trimLeft()).toEqual('Not Spooky');
252+
});
253+
254+
it('displays "no" in AppMode.CreaturesVTrash', () => {
255+
setState({appMode: AppMode.CreaturesVTrash, word: 'Spooky'});
256+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
257+
const noButton = getNoButton(wrapper).render();
258+
259+
expect(noButton.text().trimLeft()).toEqual('No');
260+
});
261+
262+
it('classifies fish on click', () => {
263+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
264+
getNoButton(wrapper).simulate('click');
265+
266+
expect(classifyFishStub.withArgs(false).callCount).toEqual(1);
267+
});
268+
269+
it('opens bot head on click', () => {
270+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
271+
272+
expect(wrapper.state().headOpen).toBeFalsy();
273+
getNoButton(wrapper).simulate('click');
274+
expect(wrapper.state().headOpen).toBeTruthy();
275+
});
276+
});
277+
278+
describe('train "yes" button', () => {
279+
const getYesButton = wrapper => wrapper.find('Button').at(1);
280+
281+
it('displays the current word when not in AppMode.CreaturesVTrash', () => {
282+
setState({appMode: 'not-creatures-v-trash', word: 'Spooky'});
283+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
284+
const noButton = getYesButton(wrapper).render();
285+
286+
expect(noButton.text().trimLeft()).toEqual('Spooky');
287+
});
288+
289+
it('displays "yes" in AppMode.CreaturesVTrash', () => {
290+
setState({appMode: AppMode.CreaturesVTrash, word: 'Spooky'});
291+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
292+
const noButton = getYesButton(wrapper).render();
293+
294+
expect(noButton.text().trimLeft()).toEqual('Yes');
295+
});
296+
297+
it('classifies fish on click', () => {
298+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
299+
getYesButton(wrapper).simulate('click');
300+
301+
expect(classifyFishStub.withArgs(true).callCount).toEqual(1);
302+
});
303+
304+
it('opens bot head on click', () => {
305+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
306+
307+
expect(wrapper.state().headOpen).toBeFalsy();
308+
getYesButton(wrapper).simulate('click');
309+
expect(wrapper.state().headOpen).toBeTruthy();
310+
});
311+
});
312+
313+
it('transitions to Modes.Predicting when continue button is clicked', () => {
314+
const toModeStub = sinon.stub(modeHelpers, 'toMode');
315+
const wrapper = shallow(<Train {...DEFAULT_PROPS} />);
316+
const continueButton = wrapper.find('Button').at(2);
317+
continueButton.simulate('click');
318+
319+
expect(toModeStub.withArgs(Modes.Predicting).callCount).toEqual(1);
320+
});
321+
});

0 commit comments

Comments
 (0)