Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update types + add TS tests #77

Merged
merged 20 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/typescript/index.js
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
env:
node: true
extends: 'eslint:recommended'
parser: '@typescript-eslint/parser'
parserOptions:
sourceType: module
rules:
indent: [ 2, 2, { SwitchCase: 1 } ]
no-trailing-spaces: 2
Expand Down
22 changes: 17 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,16 @@ $ yarn run test-browser-ie

### Types

We validate our TypeScript `index.d.ts` with:
We validate our TypeScript `index.d.ts` with two steps:

```sh
# Runs the TypeScript compiler over our types
$ yarn run test-tsc
kale-stew marked this conversation as resolved.
Show resolved Hide resolved

# Runs our types through a sample TypeScript file
$ yarn run test-ts
# Compiled output is put into a co-located index.js
# Treat the output like a snapshot, commit any changes
```

### Style
Expand Down Expand Up @@ -135,7 +141,13 @@ please flag to your reviewers and have a discussion about whether or not the siz

_Only for project administrators_

1. Run `npm version patch` (or `minor|major|VERSION`) to run tests and lint,
build published directories, then update `package.json` + add a git tag.
2. Run `npm publish` and publish to NPM if all is well.
3. Run `git push && git push --tags`
```sh
# (1) Run tests, lint, build published dir, update package.json
$ npm version [patch|minor|major|<version>]

# (2) If all is well, publish the new version to the npm registry
$ npm publish

# (3) Then, update github with the associated tag
$ git push && git push --tags
```
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
"preversion": "yarn test",
"benchmark": "node benchmark",
"eslint": "eslint \"*.js\" benchmark test",
"tslint": "eslint test/typescript/index.tsx",
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
"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": "tsc --target ES5 --noImplicitAny index.d.ts",
"test": "builder concurrent --buffer eslint test-ts test-node-cov test-browser",
"test-ie": "builder concurrent --buffer eslint test-ts test-node-cov test-browser-ie",
"test-node-cov": "nyc yarn test-node",
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
"test-ts-usage": "tsc --esModuleInterop --jsx react test/typescript/index.tsx",
"test-ts-defs": "tsc --target ES5 index.d.ts",
"test": "builder concurrent --buffer eslint test-ts-usage test-ts-defs test-node-cov test-browser",
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
"test-ie": "builder concurrent --buffer eslint test-ts-usage test-ts-defs test-node-cov test-browser-ie",
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
"compress": "terser --compress --mangle=\"toplevel:true\" -- index.js",
"size-min-gz": "yarn -s compress | gzip -9 | wc -c"
},
Expand All @@ -40,6 +42,10 @@
"@babel/preset-env": "^7.7.6",
"@testing-library/dom": "^7.5.1",
"@testing-library/preact": "^1.0.2",
"@types/node": "^14.0.1",
"@types/react": "^16.9.35",
"@types/react-redux": "^7.1.8",
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
"@typescript-eslint/parser": "^2.34.0",
"babel-loader": "^8.0.6",
"benchmark": "^2.1.4",
"builder": "^5.0.0",
Expand All @@ -64,7 +70,9 @@
"nyc": "^14.1.1",
"preact": "^10.4.1",
"react": "^16.3.1",
"react-redux": "^7.2.0",
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
"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
8 changes: 4 additions & 4 deletions test/node/advanced.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const PreactTestRenderer = require('@testing-library/preact');
const equal = require('../..');
const tests = require('./tests');

const TIMEOUT = '5000';
// `jsdom-global` does a lazy require under the hood to `jsdom` which is
// super expensive. We deliberately call + cleanup to take the require hit
// early and seed the require cache so subsequent calls are fast.
jsdom()();

class ReactChild extends React.Component {
shouldComponentUpdate(nextProps) {
Expand Down Expand Up @@ -61,9 +64,6 @@ describe('advanced', () => {
let sandbox;
let warnStub;

// eslint-disable-next-line no-invalid-this
beforeEach(function () { this.timeout(TIMEOUT); });

beforeEach(() => {
sandbox = sinon.createSandbox();
warnStub = sandbox.stub(console, 'warn');
Expand Down
52 changes: 52 additions & 0 deletions test/typescript/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use strict";
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var react_1 = __importDefault(require("react"));
var equal = require("../..");
var testArr = [
{ text: "green", id: "23" },
{ text: "sunshine", id: "1" },
{ text: "mountain", id: "11" },
{ text: "air", id: "8" },
{ text: "plants", id: "9" },
];
var TestChild = /** @class */ (function (_super) {
__extends(TestChild, _super);
function TestChild() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestChild.prototype.shouldComponentUpdate = function (nextProps) {
return !equal(this.props, nextProps);
};
TestChild.prototype.render = function () {
var todo = this.props.todo;
return react_1["default"].createElement("div", null, todo.text);
};
return TestChild;
}(react_1["default"].Component));
;
var TestContainer = /** @class */ (function (_super) {
__extends(TestContainer, _super);
function TestContainer() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestContainer.prototype.render = function () {
return testArr.map(function (item) { return react_1["default"].createElement(TestChild, { key: item.id, todo: item }); });
};
return TestContainer;
}(react_1["default"].Component));
38 changes: 38 additions & 0 deletions test/typescript/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
// import { useSelector } from 'react-redux';
kale-stew marked this conversation as resolved.
Show resolved Hide resolved

const equal = require('../..');

const testArr = [
{ text: 'green', id: '23' },
{ text: 'sunshine', id: '1' },
{ text: 'mountain', id: '11' },
{ text: 'air', id: '8' },
{ text: 'plants', id: '9' },
] as ITodo[];
kale-stew marked this conversation as resolved.
Show resolved Hide resolved

type ITodo = {
kale-stew marked this conversation as resolved.
Show resolved Hide resolved
text: string;
id: string;
};

type IProps = {
todo: ITodo;
};

class TestChild extends React.Component<IProps> {
shouldComponentUpdate(nextProps: IProps) {
return !equal(this.props, nextProps);
}

render() {
const todo = this.props.todo;
return <div>{todo.text}</div>;
}
}

class TestContainer extends React.Component {
render() {
return testArr.map((item) => <TestChild key={item.id} todo={item} />);
}
}
8 changes: 8 additions & 0 deletions test/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"composite": true,
"jsx": "react",
"target": "ES6",
"allowSyntheticDefaultImports": true
}
}
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"noImplicitAny": true
},
"env": {
"browser": true,
"node": true,
"es6": true
},
"references": [{ "path": "test/typescript" }],
"parserOptions": {
"sourceType": "module"
}
}
Loading