Skip to content

Commit

Permalink
Merge 3ee0f4b into 9371576
Browse files Browse the repository at this point in the history
  • Loading branch information
ndresx committed May 8, 2021
2 parents 9371576 + 3ee0f4b commit 7ba2590
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
22 changes: 11 additions & 11 deletions src/Countdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('<Countdown />', () => {
expect(api.isCompleted()).toBe(true);
});

it('should only re-set time delta state when props have changed', () => {
it('should only reset time delta state when date prop is changing', () => {
const root = document.createElement('div');
wrapper = mount(<Countdown date={1000} />, { attachTo: root });
const obj = wrapper.instance();
Expand All @@ -192,22 +192,22 @@ describe('<Countdown />', () => {
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(1);

wrapper.setProps(mergeProps({ intervalDelay: 999 }));
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(2);
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(1);

wrapper.setProps(mergeProps({ date: 500 }));
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(2);

wrapper.setProps(mergeProps({ precision: NaN }));
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(3);

wrapper.setProps(mergeProps({ precision: NaN }));
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(3);
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(1);

wrapper.setProps(mergeProps({ precision: 3 }));
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(4);
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(1);

wrapper.setProps(mergeProps({ date: 750 }));
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(5);
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(2);

wrapper.setProps(mergeProps({ children: <div /> }));
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(2);

wrapper.setProps(mergeProps({ date: 1000 }));
expect(obj.setTimeDeltaState).toHaveBeenCalledTimes(3);
});

it('should not (try to) set state after component unmount', () => {
Expand Down
25 changes: 4 additions & 21 deletions src/Countdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,10 @@ export default class Countdown extends React.Component<CountdownProps, Countdown
return;
}

if (!this.shallowCompare(this.props, prevProps)) {
if (this.props.date !== prevProps.date) {
this.initialTimestamp = this.calcOffsetStartTimestamp();
this.offsetStartTimestamp = this.initialTimestamp;
this.offsetTime = 0;
}
if (this.props.date !== prevProps.date) {
this.initialTimestamp = this.calcOffsetStartTimestamp();
this.offsetStartTimestamp = this.initialTimestamp;
this.offsetTime = 0;

this.setTimeDeltaState(this.calcTimeDelta());
}
Expand Down Expand Up @@ -248,21 +246,6 @@ export default class Countdown extends React.Component<CountdownProps, Countdown
return this.state.status === status;
}

shallowCompare(objA: object, objB: object): boolean {
const keysA = Object.keys(objA);
return (
keysA.length === Object.keys(objB).length &&
!keysA.some(keyA => {
const valueA = objA[keyA];
const valueB = objB[keyA];
return (
!objB.hasOwnProperty(keyA) ||
!(valueA === valueB || (valueA !== valueA && valueB !== valueB)) // NaN !== NaN
);
})
);
}

handleOnComplete = (timeDelta: CountdownTimeDelta): void => {
if (this.props.onComplete) this.props.onComplete(timeDelta);
};
Expand Down

0 comments on commit 7ba2590

Please sign in to comment.