Skip to content

Commit 5dce482

Browse files
committed
Add test for TypeScript typings
1 parent 0906e09 commit 5dce482

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
2222
// Simpler TypeScript 2.8+ definition of Omit (disabled for now to support lower TypeScript versions)
2323
// export type Omit<T, K extends keyof T> = Pick<T, Diff<keyof T, K>>;
2424

25+
// Due to https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20796, we cannot use traceLifecycle as a decorator
26+
// in TypeScript, so just do `const TracedComponent = traceLifecycle(ComponentToTrace)` instead.
2527
export function traceLifecycle<P extends TraceProps>(
2628
component: React.ComponentClass<P>
2729
): React.ComponentClass<Omit<P, keyof TraceProps>>;

package-lock.json

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"typings": "./index.d.ts",
77
"scripts": {
88
"lint": "eslint --ext js,jsx --max-warnings 0 src test examples",
9-
"test": "npm run lint && jest",
9+
"test": "npm run lint && npm run test-typings && jest",
10+
"test-typings": "tsc -p test/typescript",
1011
"start": "webpack-dev-server --hot --mode development",
1112
"build-demo": "webpack --mode development",
1213
"clean-lib": "rm -rf dist",
@@ -44,6 +45,8 @@
4445
}
4546
},
4647
"devDependencies": {
48+
"@types/react": "^16.3.14",
49+
"@types/react-dom": "^16.0.5",
4750
"babel-cli": "^6.26.0",
4851
"babel-eslint": "^8.2.3",
4952
"babel-jest": "^22.4.3",
@@ -83,7 +86,8 @@
8386
"react-redux": "^5.0.7",
8487
"redux": "^4.0.0",
8588
"redux-thunk": "^2.2.0",
86-
"style-loader": "^0.20.3"
89+
"style-loader": "^0.20.3",
90+
"typescript": "^2.8.3"
8791
},
8892
"peerDependencies": {
8993
"react": "^16.3.0",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as React from 'react';
2+
import { render } from 'react-dom';
3+
import { resetInstanceIdCounters, clearLog, Log, traceLifecycle, VisualizerProvider, TraceProps }
4+
from 'react-lifecycle-visualizer';
5+
6+
// Basic test to check if the typings are consistent. Whether these types correspond to the actual JavaScript
7+
// implementation is currently not tested, as we'd need to do a TypeScript webpack build for that.
8+
9+
resetInstanceIdCounters();
10+
clearLog();
11+
12+
interface ComponentToTraceProps extends TraceProps {};
13+
interface ComponentToTraceState {}
14+
15+
class ComponentToTrace extends React.Component<ComponentToTraceProps, ComponentToTraceState> {
16+
constructor(props: ComponentToTraceProps, context?: any) {
17+
props.trace('before super(props)');
18+
super(props, context);
19+
this.props.trace('after super(props)');
20+
}
21+
22+
static getDerivedStateFromProps(nextProps : ComponentToTraceProps, nextState: ComponentToTraceState) {
23+
nextProps.trace('deriving');
24+
return null;
25+
}
26+
27+
render() {
28+
return <this.props.LifecyclePanel/>;
29+
}
30+
}
31+
32+
// Due to https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20796, we cannot use `traceLifecycle` as a decorator
33+
// in TypeScript, so we just apply it as a function.
34+
const TracedComponent = traceLifecycle(ComponentToTrace);
35+
36+
const ProvidedComponent = () => (
37+
<VisualizerProvider>
38+
<div>
39+
<TracedComponent/>
40+
<Log/>
41+
</div>
42+
</VisualizerProvider>
43+
);
44+
render(<ProvidedComponent/>, document.getElementById('root'));

test/typescript/tsconfig.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"files": [
3+
"../../index.d.ts",
4+
"react-lifecycle-visualizer-typings-test.tsx"
5+
],
6+
"compilerOptions": {
7+
"module": "commonjs",
8+
"lib": [
9+
"es6",
10+
"dom"
11+
],
12+
"noImplicitAny": true,
13+
"noImplicitThis": true,
14+
"strictNullChecks": true,
15+
"strictFunctionTypes": true,
16+
"experimentalDecorators": true,
17+
"baseUrl": "../..",
18+
"jsx": "react",
19+
"typeRoots": [
20+
"../.."
21+
],
22+
"types": [],
23+
"noEmit": true,
24+
"paths": {
25+
"react-lifecycle-visualizer": ["index.d.ts"]
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)