Skip to content

Commit

Permalink
v2.2.1: Replace deep equal with shallow comparator (#77)
Browse files Browse the repository at this point in the history
* Replace deep equal with shallow comparator

* Update build badge link

* Update for NaN comparison
  • Loading branch information
ndresx committed Feb 8, 2020
1 parent 99520f3 commit 8bd2612
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# React <Countdown /> [![npm][npm]][npm-url] [![Build Status](https://travis-ci.org/ndresx/react-countdown.svg?branch=master)](https://travis-ci.org/ndresx/react-countdown) [![Coverage Status](https://coveralls.io/repos/github/ndresx/react-countdown/badge.svg?branch=master)](https://coveralls.io/github/ndresx/react-countdown?branch=master)
# React <Countdown /> [![npm][npm]][npm-url] [![Build Status](https://travis-ci.com/ndresx/react-countdown.svg?branch=master)](https://travis-ci.com/ndresx/react-countdown) [![Coverage Status](https://coveralls.io/repos/github/ndresx/react-countdown/badge.svg?branch=master)](https://coveralls.io/github/ndresx/react-countdown?branch=master)
A customizable countdown component for React.

* [Getting Started](#getting-started)
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-countdown",
"version": "2.2.0",
"version": "2.2.1",
"description": "A customizable countdown component for React.",
"main": "./dist/index.js",
"module": "./dist/index.es.js",
Expand Down Expand Up @@ -39,7 +39,6 @@
"test:watch": "jest --watch"
},
"dependencies": {
"lodash.isequal": "^4.5.0",
"prop-types": "^15.7.2"
},
"devDependencies": {
Expand All @@ -51,7 +50,6 @@
"@types/enzyme-adapter-react-16": "^1.0.5",
"@types/enzyme-to-json": "^1.5.3",
"@types/jest": "^24.0.13",
"@types/lodash.isequal": "^4.5.5",
"@types/react": "^16.8.19",
"coveralls": "^3.0.4",
"enzyme": "^3.10.0",
Expand Down
34 changes: 33 additions & 1 deletion src/Countdown.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { mount } from 'enzyme';

import Countdown from './Countdown';
import Countdown, { CountdownProps } from './Countdown';
import { calcTimeDelta, formatTimeDelta } from './utils';

import { CountdownProps as LegacyCountdownProps } from './LegacyCountdown';
Expand Down Expand Up @@ -179,6 +179,38 @@ describe('<Countdown />', () => {
expect(api.isCompleted()).toBe(true);
});

it('should only re-set time delta state when props have changed', () => {
const root = document.createElement('div');
wrapper = mount(<Countdown date={1000} />, { attachTo: root });
const obj = wrapper.instance();
obj.setTimeDeltaState = jest.fn();

function mergeProps(partialProps: Partial<CountdownProps>): CountdownProps {
return { ...wrapper.props(), ...partialProps };
}

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

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

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);

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

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

it('should not (try to) set state after component unmount', () => {
expect(wrapper.state().timeDelta.completed).toBe(false);

Expand Down
19 changes: 16 additions & 3 deletions src/Countdown.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';

const isEqual = require('lodash.isequal');

import LegacyCountdown, { CountdownProps as LegacyCountdownProps } from './LegacyCountdown';

import {
Expand Down Expand Up @@ -125,7 +123,7 @@ export default class Countdown extends React.Component<CountdownProps, Countdown
return;
}

if (!isEqual(this.props, prevProps)) {
if (!this.shallowCompareProps(this.props, prevProps)) {
this.setTimeDeltaState(this.calcTimeDelta());
}
}
Expand Down Expand Up @@ -207,6 +205,21 @@ export default class Countdown extends React.Component<CountdownProps, Countdown
return this.state.timeDelta.completed;
};

shallowCompareProps(propsA: CountdownProps, propsB: CountdownProps): boolean {
const keysA = Object.keys(propsA);
return (
keysA.length === Object.keys(propsB).length &&
!keysA.some(keyA => {
const valueA = propsA[keyA];
const valueB = propsB[keyA];
return (
!propsB.hasOwnProperty(keyA) ||
!(valueA === valueB || (valueA !== valueA && valueB !== valueB)) // NaN !== NaN
);
})
);
}

setTimeDeltaState(timeDelta: CountdownTimeDelta): void {
let callback;

Expand Down
12 changes: 0 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -971,18 +971,6 @@
dependencies:
"@types/jest-diff" "*"

"@types/lodash.isequal@^4.5.5":
version "4.5.5"
resolved "https://registry.yarnpkg.com/@types/lodash.isequal/-/lodash.isequal-4.5.5.tgz#4fed1b1b00bef79e305de0352d797e9bb816c8ff"
integrity sha512-4IKbinG7MGP131wRfceK6W4E/Qt3qssEFLF30LnJbjYiSfHGGRU/Io8YxXrZX109ir+iDETC8hw8QsDijukUVg==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.134"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.134.tgz#9032b440122db3a2a56200e91191996161dde5b9"
integrity sha512-2/O0khFUCFeDlbi7sZ7ZFRCcT812fAeOLm7Ev4KbwASkZ575TDrDcY7YyaoHdTOzKcNbfiwLYZqPmoC4wadrsw==

"@types/node@*", "@types/node@^12.0.7":
version "12.0.7"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.0.7.tgz#4f2563bad652b2acb1722d7e7aae2b0ff62d192c"
Expand Down

0 comments on commit 8bd2612

Please sign in to comment.