Skip to content

Commit 8260d57

Browse files
committed
Add more tests and updates to component
1 parent 58b3295 commit 8260d57

File tree

2 files changed

+133
-14
lines changed

2 files changed

+133
-14
lines changed

src/index.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ class TimerWrapper extends Component {
4949
onStop,
5050
} = nextProps;
5151

52-
if (time !== this.props.time) {
52+
if (active === this.props.active && time !== this.state.time) {
53+
const timeDiff = this.state.time - time;
54+
5355
this.setState({
54-
time,
56+
startTime: this.state.startTime + timeDiff,
57+
time: this.state.time + timeDiff,
5558
});
59+
return;
5660
}
5761

5862
if (active !== this.props.active) {
@@ -65,15 +69,15 @@ class TimerWrapper extends Component {
6569
this.setState({
6670
startTime: Date.now() - nextTime,
6771
time: nextTime,
72+
}, () => {
73+
onStart({
74+
duration,
75+
progress: this.getProgress(nextTime),
76+
time: nextTime,
77+
});
78+
79+
this.animationFrame = requestAnimationFrame(this.tick);
6880
});
69-
70-
onStart({
71-
duration,
72-
progress: this.getProgress(nextTime),
73-
time: nextTime,
74-
});
75-
76-
this.animationFrame = requestAnimationFrame(this.tick);
7781
break;
7882

7983
case false:

src/index.test.js

+119-4
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,22 @@ describe('<Timer />', () => {
197197
component.unmount();
198198
});
199199

200+
test('onStop - called', () => {
201+
expect.assertions(2);
202+
203+
const onStop = jest.fn();
204+
205+
component = mount(<Timer active onStop={onStop} />, {
206+
attachTo: document.getElementById('root'),
207+
});
208+
209+
expect(onStop).not.toBeCalled();
210+
component.setProps({ active: false });
211+
expect(onStop).toBeCalled();
212+
213+
component.unmount();
214+
});
215+
200216
test('time offset supported - onStart', () => {
201217
const onStart = jest.fn();
202218

@@ -248,18 +264,117 @@ describe('<Timer />', () => {
248264
component.unmount();
249265
});
250266

251-
test('onStop - called', () => {
267+
test('time prop changed', () => {
252268
expect.assertions(2);
269+
const onTimeUpdate = jest.fn();
253270

271+
component = mount(<Timer active onTimeUpdate={onTimeUpdate} />, {
272+
attachTo: document.getElementById('root'),
273+
});
274+
275+
clock.tick(16);
276+
expect(onTimeUpdate).toHaveBeenLastCalledWith({
277+
duration: 10000,
278+
progress: 0.0016,
279+
time: 16,
280+
});
281+
component.setProps({ time: 5000 });
282+
clock.tick(16);
283+
expect(onTimeUpdate).toHaveBeenLastCalledWith({
284+
duration: 10000,
285+
progress: 0.5016,
286+
time: 5016,
287+
});
288+
289+
component.unmount();
290+
});
291+
292+
test('active prop changed - after timer complete', () => {
293+
expect.assertions(4);
294+
295+
const onStart = jest.fn();
254296
const onStop = jest.fn();
297+
const onTimeUpdate = jest.fn();
255298

256-
component = mount(<Timer active onStop={onStop} />, {
299+
component = mount((
300+
<Timer
301+
active
302+
onStart={onStart}
303+
onStop={onStop}
304+
onTimeUpdate={onTimeUpdate}
305+
/>
306+
), {
257307
attachTo: document.getElementById('root'),
258308
});
259309

260-
expect(onStop).not.toBeCalled();
310+
expect(onStart).toHaveBeenLastCalledWith({
311+
duration: 10000,
312+
progress: 0,
313+
time: 0,
314+
});
315+
clock.tick(10000);
316+
expect(onTimeUpdate).toHaveBeenLastCalledWith({
317+
duration: 10000,
318+
progress: 1,
319+
time: 10000,
320+
});
261321
component.setProps({ active: false });
262-
expect(onStop).toBeCalled();
322+
expect(onStop).toHaveBeenLastCalledWith({
323+
duration: 10000,
324+
progress: 1,
325+
time: 10000,
326+
});
327+
component.setProps({ active: true });
328+
expect(onStart).toHaveBeenLastCalledWith({
329+
duration: 10000,
330+
progress: 0,
331+
time: 0,
332+
});
333+
334+
component.unmount();
335+
});
336+
337+
test('active prop changed - before timer complete', () => {
338+
expect.assertions(4);
339+
340+
const onStart = jest.fn();
341+
const onStop = jest.fn();
342+
const onTimeUpdate = jest.fn();
343+
344+
component = mount((
345+
<Timer
346+
active
347+
onStart={onStart}
348+
onStop={onStop}
349+
onTimeUpdate={onTimeUpdate}
350+
/>
351+
), {
352+
attachTo: document.getElementById('root'),
353+
});
354+
355+
expect(onStart).toHaveBeenLastCalledWith({
356+
duration: 10000,
357+
progress: 0,
358+
time: 0,
359+
});
360+
clock.tick(2000);
361+
expect(onTimeUpdate).toHaveBeenLastCalledWith({
362+
duration: 10000,
363+
progress: 0.2,
364+
time: 2000,
365+
});
366+
component.setProps({ active: false });
367+
expect(onStop).toHaveBeenLastCalledWith({
368+
duration: 10000,
369+
progress: 0.2,
370+
time: 2000,
371+
});
372+
component.setProps({ active: true });
373+
expect(onStart).toHaveBeenLastCalledWith({
374+
duration: 10000,
375+
progress: 0.2,
376+
time: 2000,
377+
});
263378

264379
component.unmount();
265380
});

0 commit comments

Comments
 (0)