Skip to content

Commit

Permalink
Add more tests and updates to component
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhefner committed Jun 17, 2018
1 parent 58b3295 commit 8260d57
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 14 deletions.
24 changes: 14 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ class TimerWrapper extends Component {
onStop,
} = nextProps;

if (time !== this.props.time) {
if (active === this.props.active && time !== this.state.time) {
const timeDiff = this.state.time - time;

this.setState({
time,
startTime: this.state.startTime + timeDiff,
time: this.state.time + timeDiff,
});
return;
}

if (active !== this.props.active) {
Expand All @@ -65,15 +69,15 @@ class TimerWrapper extends Component {
this.setState({
startTime: Date.now() - nextTime,
time: nextTime,
}, () => {
onStart({
duration,
progress: this.getProgress(nextTime),
time: nextTime,
});

this.animationFrame = requestAnimationFrame(this.tick);
});

onStart({
duration,
progress: this.getProgress(nextTime),
time: nextTime,
});

this.animationFrame = requestAnimationFrame(this.tick);
break;

case false:
Expand Down
123 changes: 119 additions & 4 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ describe('<Timer />', () => {
component.unmount();
});

test('onStop - called', () => {
expect.assertions(2);

const onStop = jest.fn();

component = mount(<Timer active onStop={onStop} />, {
attachTo: document.getElementById('root'),
});

expect(onStop).not.toBeCalled();
component.setProps({ active: false });
expect(onStop).toBeCalled();

component.unmount();
});

test('time offset supported - onStart', () => {
const onStart = jest.fn();

Expand Down Expand Up @@ -248,18 +264,117 @@ describe('<Timer />', () => {
component.unmount();
});

test('onStop - called', () => {
test('time prop changed', () => {
expect.assertions(2);
const onTimeUpdate = jest.fn();

component = mount(<Timer active onTimeUpdate={onTimeUpdate} />, {
attachTo: document.getElementById('root'),
});

clock.tick(16);
expect(onTimeUpdate).toHaveBeenLastCalledWith({
duration: 10000,
progress: 0.0016,
time: 16,
});
component.setProps({ time: 5000 });
clock.tick(16);
expect(onTimeUpdate).toHaveBeenLastCalledWith({
duration: 10000,
progress: 0.5016,
time: 5016,
});

component.unmount();
});

test('active prop changed - after timer complete', () => {
expect.assertions(4);

const onStart = jest.fn();
const onStop = jest.fn();
const onTimeUpdate = jest.fn();

component = mount(<Timer active onStop={onStop} />, {
component = mount((
<Timer
active
onStart={onStart}
onStop={onStop}
onTimeUpdate={onTimeUpdate}
/>
), {
attachTo: document.getElementById('root'),
});

expect(onStop).not.toBeCalled();
expect(onStart).toHaveBeenLastCalledWith({
duration: 10000,
progress: 0,
time: 0,
});
clock.tick(10000);
expect(onTimeUpdate).toHaveBeenLastCalledWith({
duration: 10000,
progress: 1,
time: 10000,
});
component.setProps({ active: false });
expect(onStop).toBeCalled();
expect(onStop).toHaveBeenLastCalledWith({
duration: 10000,
progress: 1,
time: 10000,
});
component.setProps({ active: true });
expect(onStart).toHaveBeenLastCalledWith({
duration: 10000,
progress: 0,
time: 0,
});

component.unmount();
});

test('active prop changed - before timer complete', () => {
expect.assertions(4);

const onStart = jest.fn();
const onStop = jest.fn();
const onTimeUpdate = jest.fn();

component = mount((
<Timer
active
onStart={onStart}
onStop={onStop}
onTimeUpdate={onTimeUpdate}
/>
), {
attachTo: document.getElementById('root'),
});

expect(onStart).toHaveBeenLastCalledWith({
duration: 10000,
progress: 0,
time: 0,
});
clock.tick(2000);
expect(onTimeUpdate).toHaveBeenLastCalledWith({
duration: 10000,
progress: 0.2,
time: 2000,
});
component.setProps({ active: false });
expect(onStop).toHaveBeenLastCalledWith({
duration: 10000,
progress: 0.2,
time: 2000,
});
component.setProps({ active: true });
expect(onStart).toHaveBeenLastCalledWith({
duration: 10000,
progress: 0.2,
time: 2000,
});

component.unmount();
});
Expand Down

0 comments on commit 8260d57

Please sign in to comment.