Skip to content

Commit 292f3e6

Browse files
committed
wip: typescript tests
1 parent 93653f8 commit 292f3e6

File tree

10 files changed

+651
-230
lines changed

10 files changed

+651
-230
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/__tests__

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"test": "jest",
1919
"test:dev": "jest --watch --no-coverage",
2020
"test:coverage:watch": "jest --watch",
21-
"test:ts": "tsc --noEmit",
21+
"test:ts": "tsc -p types/__tests__/tsconfig.json",
2222
"postinstall": "node -e \"console.log('\\u001b[35m\\u001b[1mEnjoy react-spring? You can now donate to our open collective:\\u001b[22m\\u001b[39m\\n > \\u001b[34mhttps://opencollective.com/react-spring/donate\\u001b[0m')\""
2323
},
2424
"husky": {
@@ -97,6 +97,7 @@
9797
"rollup-plugin-node-resolve": "4.0.0",
9898
"rollup-plugin-size-snapshot": "0.8.0",
9999
"rollup-plugin-uglify": "6.0.2",
100+
"spec.ts": "^1.1.0",
100101
"typescript": "3.3.3"
101102
},
102103
"peerDependencies": {

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"include": ["src", "types"],
23
"compilerOptions": {
34
"target": "es2017",
45
"moduleResolution": "node",
@@ -15,6 +16,5 @@
1516
"noUnusedLocals": true,
1617
"noUnusedParameters": true,
1718
"strict": true
18-
},
19-
"include": ["src/**/*", "types/**/*.ts"]
19+
}
2020
}

types/__tests__/.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "es5",
4+
"singleQuote": true,
5+
"jsxBracketSameLine": true,
6+
"tabWidth": 2,
7+
"printWidth": 80
8+
}

types/__tests__/common.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import {
2+
PickAnimated,
3+
ForwardProps,
4+
AnimatedProps,
5+
AnimatedValue,
6+
SpringFrame,
7+
UnknownProps,
8+
} from '../lib/common';
9+
import { assert, _, test } from 'spec.ts';
10+
11+
const reservedProps = {
12+
config: null,
13+
from: {},
14+
to: {},
15+
ref: null,
16+
reset: null,
17+
reverse: null,
18+
immediate: null,
19+
delay: null,
20+
lazy: null,
21+
onStart: null,
22+
onRest: null,
23+
onFrame: null,
24+
attach: null,
25+
};
26+
27+
const forwardProps = {
28+
foo: null,
29+
bar: null,
30+
};
31+
32+
type RProps = typeof reservedProps;
33+
type FProps = typeof forwardProps;
34+
35+
test('ForwardProps', () => {
36+
// With reserved props, no forward props
37+
type P1 = ForwardProps<RProps>;
38+
assert(_ as P1, _ as {});
39+
40+
// With reserved and forward props
41+
type P2 = ForwardProps<RProps & FProps>;
42+
assert(_ as P2, _ as FProps);
43+
44+
// With forward props, no reserved props
45+
type P3 = ForwardProps<FProps>;
46+
assert(_ as P3, _ as FProps);
47+
48+
// No reserved or forward props
49+
type P4 = ForwardProps<{}>;
50+
assert(_ as P4, _ as {});
51+
});
52+
53+
test('PickAnimated', () => {
54+
// No props
55+
type A1 = PickAnimated<{}>;
56+
assert(_ as A1, _ as {});
57+
58+
// Forward props only
59+
type A3 = PickAnimated<FProps>;
60+
assert(_ as A3, _ as FProps);
61+
62+
// Forward props and "from" prop
63+
type A4 = PickAnimated<{ foo: null; from: { bar: null } }>;
64+
assert(_ as A4, _ as FProps);
65+
66+
// "to" and "from" props
67+
type A5 = PickAnimated<{ to: { foo: null }; from: { bar: null } }>;
68+
assert(_ as A5, _ as FProps);
69+
});
70+
71+
test('AnimatedProps', () => {
72+
// Primitive props
73+
type P2 = AnimatedProps<{ foo?: number }>;
74+
assert(
75+
_ as P2,
76+
_ as {
77+
foo?: number | AnimatedValue<number>;
78+
}
79+
);
80+
81+
// Object props
82+
type P3 = AnimatedProps<{ foo?: { bar?: number } }>;
83+
assert(
84+
_ as P3,
85+
_ as {
86+
foo?: AnimatedProps<{ bar?: number }>;
87+
}
88+
);
89+
90+
// Array props
91+
type P4 = AnimatedProps<{ foo: [number, number] }>;
92+
assert(
93+
_ as P4,
94+
_ as {
95+
foo: [number, number] | AnimatedValue<[number, number]>;
96+
}
97+
);
98+
99+
// Atomic object props
100+
type P5 = AnimatedProps<{
101+
set: Set<any>;
102+
map: Map<any, any>;
103+
date: Date;
104+
func: Function;
105+
prom: Promise<any>;
106+
}>;
107+
assert(
108+
_ as P5,
109+
_ as {
110+
set: Set<any> | AnimatedValue<Set<any>>;
111+
map: Map<any, any> | AnimatedValue<Map<any, any>>;
112+
date: Date | AnimatedValue<Date>;
113+
func: Function | AnimatedValue<Function>;
114+
prom: Promise<any> | AnimatedValue<Promise<any>>;
115+
}
116+
);
117+
});
118+
119+
test('SpringFrame', () => {
120+
type T1 = SpringFrame<{}>;
121+
assert(_ as T1, _ as { [x: string]: unknown });
122+
123+
type T2 = SpringFrame<{ to: { a: number }; from: { b: number } }>;
124+
assert(_ as T2, _ as UnknownProps & { a: number; b: number });
125+
});

types/__tests__/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"include": ["*.ts"],
3+
"compilerOptions": {
4+
"target": "es2017",
5+
"moduleResolution": "node",
6+
"lib": ["dom", "es2017"],
7+
"noEmit": true,
8+
"jsx": "react",
9+
"allowSyntheticDefaultImports": true,
10+
"noUnusedLocals": true,
11+
"noUnusedParameters": true,
12+
"strict": true
13+
}
14+
}

types/__tests__/useSpring.tsx

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import {
2+
animated,
3+
useSpring,
4+
AnimatedValue,
5+
SpringHandle,
6+
SpringStopFn,
7+
SpringUpdateFn,
8+
} from '../web';
9+
import React, { useRef } from 'react';
10+
import { assert, test, _ } from 'spec.ts';
11+
import { UnknownProps } from '../lib/common';
12+
13+
test('infer return type via forward prop', () => {
14+
const props = useSpring({ foo: 0 });
15+
assert(props, _ as UnknownProps & {
16+
foo: AnimatedValue<number>;
17+
});
18+
19+
test('using with "animated()" component', () => {
20+
const Test = animated((_: { style: { foo: number } }) => null);
21+
return <Test style={props} />;
22+
});
23+
});
24+
25+
test('infer return type via "from" prop', () => {
26+
const props = useSpring({
27+
from: { foo: 0 },
28+
});
29+
assert(props, _ as UnknownProps & {
30+
foo: AnimatedValue<number>;
31+
});
32+
});
33+
34+
test('infer return type via "to" prop', () => {
35+
const props = useSpring({
36+
to: { foo: 0 },
37+
});
38+
assert(props, _ as UnknownProps & {
39+
foo: AnimatedValue<number>;
40+
});
41+
});
42+
43+
test('infer return type via "from" and "to" props', () => {
44+
const props = useSpring({
45+
from: { foo: 0 },
46+
to: { bar: '1' },
47+
});
48+
assert(props, _ as UnknownProps & {
49+
foo: AnimatedValue<number>;
50+
bar: AnimatedValue<string>;
51+
});
52+
});
53+
54+
test('infer return type via "from" and forward props', () => {
55+
const props = useSpring({
56+
from: { foo: 0 },
57+
bar: '1',
58+
});
59+
assert(props, _ as UnknownProps & {
60+
foo: AnimatedValue<number>;
61+
bar: AnimatedValue<string>;
62+
});
63+
});
64+
65+
test('infer animated array', () => {
66+
const props = useSpring({
67+
to: { foo: [0, 0] },
68+
});
69+
assert(props, _ as UnknownProps & {
70+
foo: AnimatedValue<number[]>;
71+
});
72+
73+
test('interpolated array', () => {
74+
props.foo.interpolate((a, b) => {
75+
assert(a, _ as number);
76+
assert(b, _ as number);
77+
});
78+
});
79+
});
80+
81+
test('imperative mode', () => {
82+
const [props, update, stop] = useSpring(() => ({
83+
foo: 0,
84+
}));
85+
assert(props, _ as UnknownProps & { foo: AnimatedValue<number> });
86+
assert(update, _ as SpringUpdateFn<{ foo: number }>);
87+
assert(stop, _ as SpringStopFn);
88+
89+
test('update()', () => {
90+
update({
91+
foo: 100,
92+
onRest(values) {
93+
assert(values, _ as UnknownProps & {
94+
foo: number;
95+
});
96+
},
97+
});
98+
});
99+
100+
test('stop()', () => {
101+
stop();
102+
stop(true);
103+
stop(true, 'foo');
104+
stop('foo');
105+
stop('foo', 'bar');
106+
});
107+
108+
test('with delay and reset', () => {
109+
const [props] = useSpring(() => ({
110+
foo: 0,
111+
delay: 1000,
112+
reset: true,
113+
}));
114+
assert(props, _ as UnknownProps & {
115+
foo: AnimatedValue<number>;
116+
});
117+
});
118+
119+
test('with callbacks', () => {
120+
const [props] = useSpring(() => ({
121+
foo: 0,
122+
onStart(anim) {
123+
assert(anim, _ as any);
124+
},
125+
onFrame(values) {
126+
assert(values.foo, _ as any);
127+
},
128+
onRest(values) {
129+
assert(values.foo, _ as any);
130+
},
131+
}));
132+
assert(props, _ as UnknownProps & {
133+
foo: AnimatedValue<number>;
134+
});
135+
});
136+
});
137+
138+
test('spring refs', () => {
139+
const ref = useRef<SpringHandle>(null);
140+
useSpring({ foo: 1, ref });
141+
ref.current!.start();
142+
ref.current!.stop(true, 'foo', 'bar');
143+
ref.current!.stop();
144+
});
145+
146+
test('basic config', () => {
147+
const props = useSpring({
148+
width: 100,
149+
from: { width: 0 },
150+
reset: _ as boolean,
151+
onStart(anim) {
152+
assert(anim, _ as any);
153+
},
154+
onFrame(values) {
155+
assert(values.width, _ as unknown);
156+
},
157+
onRest(values) {
158+
assert(values.width, _ as unknown);
159+
},
160+
});
161+
assert(props, _ as UnknownProps & {
162+
width: AnimatedValue<number>;
163+
});
164+
});
165+
166+
test('function as "to" prop', () => {
167+
const props = useSpring({
168+
to: async next => {
169+
assert(next, _ as SpringUpdateFn);
170+
await next({
171+
foo: 0,
172+
width: '100%', // New keys can be animated.
173+
delay: 1000,
174+
config: { duration: 1000 },
175+
onRest(values) {
176+
assert(values, _ as UnknownProps);
177+
},
178+
});
179+
},
180+
});
181+
assert(props, _ as UnknownProps);
182+
});
183+
184+
test('array as "to" prop', () => {
185+
// Avoid inferring "opacity" here, unless the "from" prop exists.
186+
const props = useSpring({
187+
to: [{ opacity: 1 }, { opacity: 0 }],
188+
// This key is ignored because "to" exists.
189+
opacity: 0,
190+
});
191+
assert(props, _ as UnknownProps);
192+
193+
test('with "from" prop', () => {
194+
const props = useSpring({
195+
to: [{ opacity: 1 }, { opacity: 0 }],
196+
from: { opacity: 0 },
197+
});
198+
assert(props, _ as UnknownProps & {
199+
opacity: AnimatedValue<number>;
200+
});
201+
});
202+
});

0 commit comments

Comments
 (0)