Skip to content

Commit

Permalink
Update types to use generics & add a TS test for react-redux (#80)
Browse files Browse the repository at this point in the history
* Add a test for react-redux's useSelector hook

* Separate react-redux test from main usage test

* Fix types, use generics
  • Loading branch information
kale-stew authored May 27, 2020
1 parent e01e975 commit 75b679d
Show file tree
Hide file tree
Showing 5 changed files with 795 additions and 642 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- [#80](https://github.com/FormidableLabs/react-fast-compare/pull/80). Update types to use generic `any`s.
- [#77](https://github.com/FormidableLabs/react-fast-compare/pull/77). Add tests for our TypeScript type definitions.

## 3.1.0 (2020-05-08)

- [#76](https://github.com/FormidableLabs/react-fast-compare/pull/76). Add support for preact/compat.
Expand All @@ -17,6 +22,7 @@
## 3.0.0 (2020-01-05)

**Features:**

- [#36](https://github.com/FormidableLabs/react-fast-compare/pull/36). Update to `[email protected]` with modified support for ES.next data types: `Map`, `Set`, `ArrayBuffer`.
- [#57](https://github.com/FormidableLabs/react-fast-compare/pull/57). Minor refactoring to reduce min+gz size.
- [#59](https://github.com/FormidableLabs/react-fast-compare/pull/59). Rename exported to `isEqual` for TypeScript users.
Expand Down
3 changes: 1 addition & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
declare function isEqual(a: any, b: any): boolean;
declare namespace isEqual {}
declare function isEqual<A = any, B = any>(a: A, b: B): boolean;
export = isEqual;
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"preversion": "yarn test",
"benchmark": "node benchmark",
"eslint": "eslint \"*.js\" benchmark test",
"tslint": "eslint test/typescript/sample-usage.tsx",
"tslint": "eslint test/typescript/*.tsx",
"test-browser": "karma start test/browser/karma.conf.js",
"test-browser-ie": "karma start test/browser/karma.conf.ie.js",
"test-node": "mocha \"test/node/*.spec.js\"",
"test-node-cov": "nyc mocha \"test/node/*.spec.js\"",
"test-ts-usage": "tsc --esModuleInterop --jsx react --noEmit test/typescript/sample-usage.tsx",
"test-ts-usage": "tsc --esModuleInterop --jsx react --noEmit test/typescript/sample-react-redux-usage.tsx test/typescript/sample-usage.tsx",
"test-ts-defs": "tsc --target ES5 index.d.ts",
"test": "builder concurrent --buffer eslint tslint test-ts-usage test-ts-defs test-node-cov test-browser",
"test-ie": "builder concurrent --buffer eslint tslint test-ts-usage test-ts-defs test-node-cov test-browser-ie",
Expand Down Expand Up @@ -45,6 +45,8 @@
"@testing-library/preact": "^1.0.2",
"@types/node": "^14.0.1",
"@types/react": "^16.9.35",
"@types/react-dom": "^16.9.8",
"@types/react-redux": "^7.1.9",
"@typescript-eslint/parser": "^2.34.0",
"babel-loader": "^8.0.6",
"benchmark": "^2.1.4",
Expand All @@ -71,7 +73,10 @@
"nyc": "^14.1.1",
"preact": "^10.4.1",
"react": "^16.3.1",
"react-dom": "^16.13.1",
"react-redux": "^7.2.0",
"react-test-renderer": "^16.13.1",
"redux": "^4.0.5",
"shallow-equal-fuzzy": "0.0.2",
"sinon": "^7.5.0",
"terser": "^4.4.3",
Expand Down
46 changes: 46 additions & 0 deletions test/typescript/sample-react-redux-usage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file exists to test our types against sample user code
// This is compiled using `tsc` in our `test-ts-usage` script
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider, useSelector } from 'react-redux';
import { createStore } from 'redux';

import equal from '../../index.js';

type IState = {
items: string[];
};

type IAction = {
type: string;
payload: any;
};

const initialState: IState = {
items: [],
};

const reducer = (state: IState, action: IAction) => {
return state;
};

const lengthSelector = (state: IState): number => state.items.length;

const store = createStore(reducer, initialState);

function Test() {
const length = useSelector(lengthSelector, equal);
return (
<div>
Testing react-redux useSelector. There are
{length.toExponential()} items.
</div>
);
}

ReactDOM.render(
<Provider store={store}>
<Test />
</Provider>,
document.getElementById('root')
);
Loading

0 comments on commit 75b679d

Please sign in to comment.