@khanacademy/wonder-blocks-testing 5.0.0 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/src/harness/__tests__/types.flowtest.js +115 -0
- package/src/harness/types.js +3 -3
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
TestHarnessAdapter,
|
|
6
|
+
TestHarnessAdapters,
|
|
7
|
+
TestHarnessConfig,
|
|
8
|
+
TestHarnessConfigs,
|
|
9
|
+
} from "../types.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* TestHarnessAdapter<TConfig>
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
//> should assert type of config.
|
|
16
|
+
((
|
|
17
|
+
children: React.Node,
|
|
18
|
+
// TConfig is string, but we typed this arg as a number
|
|
19
|
+
// $FlowExpectedError[incompatible-cast]
|
|
20
|
+
config: number,
|
|
21
|
+
): React.Element<any> => <div />: TestHarnessAdapter<string>);
|
|
22
|
+
//<
|
|
23
|
+
|
|
24
|
+
//> should work for correct definition
|
|
25
|
+
((children: React.Node, config: string): React.Element<any> => (
|
|
26
|
+
<div />
|
|
27
|
+
): TestHarnessAdapter<string>);
|
|
28
|
+
//<
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* TestHarnessAdapters
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
//> should work for empty case
|
|
35
|
+
({}: TestHarnessAdapters);
|
|
36
|
+
//<
|
|
37
|
+
|
|
38
|
+
//> should assert if adapter is not Adapter<TConfig>
|
|
39
|
+
({
|
|
40
|
+
// String is not a adapter function
|
|
41
|
+
// $FlowExpectedError[incompatible-cast]
|
|
42
|
+
adapterString: "string",
|
|
43
|
+
}: TestHarnessAdapters);
|
|
44
|
+
//<
|
|
45
|
+
|
|
46
|
+
//> should work for a function matching Adapter<TConfig>
|
|
47
|
+
({
|
|
48
|
+
adapterA: (children, config) => <div>test</div>,
|
|
49
|
+
}: TestHarnessAdapters);
|
|
50
|
+
//<
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* TestHarnessConfig<TAdapter>
|
|
54
|
+
*/
|
|
55
|
+
//> should give the config type of an adapter
|
|
56
|
+
("string": TestHarnessConfig<TestHarnessAdapter<string>>);
|
|
57
|
+
//<
|
|
58
|
+
|
|
59
|
+
//> should error if the config type is wrong
|
|
60
|
+
// 45 is not a string
|
|
61
|
+
// $FlowExpectedError[incompatible-cast]
|
|
62
|
+
(45: TestHarnessConfig<TestHarnessAdapter<string>>);
|
|
63
|
+
//<
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* TestHarnessConfigs<TAdapters>
|
|
67
|
+
*
|
|
68
|
+
* NOTE: This only works if the properties of the passed THarnasses type
|
|
69
|
+
* are explicitly typed as `TestHarnessAdapter<TConfig>` so if passing in a
|
|
70
|
+
* non-Adapters type (which we should be, to get strong TConfig types instead
|
|
71
|
+
* of `any`), then that object should make sure that each adapter is strongly
|
|
72
|
+
* marked as `TestHarnessAdapter<TConfig>` - flow does not appear to pattern
|
|
73
|
+
* match against the type definition when invoking the ExtractConfig type and I
|
|
74
|
+
* haven't worked out how to get it to multi-dispatch so that it matches
|
|
75
|
+
* functions too. Even worse, if the type doesn't match, it just allows `any`
|
|
76
|
+
* in the configs object, rather than indicating any kind of problem.
|
|
77
|
+
*/
|
|
78
|
+
const notadapters = "this is wrong";
|
|
79
|
+
const adapterA: TestHarnessAdapter<string> = (
|
|
80
|
+
children: React.Node,
|
|
81
|
+
config: ?string,
|
|
82
|
+
): React.Element<any> => <div />;
|
|
83
|
+
const adapterB: TestHarnessAdapter<number> = (
|
|
84
|
+
children: React.Node,
|
|
85
|
+
config: ?number,
|
|
86
|
+
): React.Element<any> => <div />;
|
|
87
|
+
const adapters = {
|
|
88
|
+
adapterA,
|
|
89
|
+
adapterB,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
//> should assert if parameterized type is not valid Adapters
|
|
93
|
+
// string is not a valid Adapter
|
|
94
|
+
// $FlowExpectedError[incompatible-use]
|
|
95
|
+
// $FlowExpectedError[incompatible-type-arg]
|
|
96
|
+
({}: TestHarnessConfigs<typeof notadapters>);
|
|
97
|
+
//<
|
|
98
|
+
|
|
99
|
+
//> should expect one config per adapter
|
|
100
|
+
// both adapter configs missing
|
|
101
|
+
// $FlowExpectedError[incompatible-exact]
|
|
102
|
+
({}: TestHarnessConfigs<typeof adapters>);
|
|
103
|
+
// adapterB config missing
|
|
104
|
+
// $FlowExpectedError[prop-missing]
|
|
105
|
+
({adapterA: "test"}: TestHarnessConfigs<typeof adapters>);
|
|
106
|
+
//<
|
|
107
|
+
|
|
108
|
+
//> should assert if config does not match adapter config
|
|
109
|
+
({
|
|
110
|
+
adapterA: "a string, this is correct",
|
|
111
|
+
// the config type here is a number, not a string
|
|
112
|
+
// $FlowExpectedError[incompatible-cast]
|
|
113
|
+
adapterB: "a string, but it should be a number",
|
|
114
|
+
}: TestHarnessConfigs<typeof adapters>);
|
|
115
|
+
//<
|
package/src/harness/types.js
CHANGED
|
@@ -4,7 +4,7 @@ import * as React from "react";
|
|
|
4
4
|
/**
|
|
5
5
|
* A adapter to be composed with our test harnass infrastructure.
|
|
6
6
|
*/
|
|
7
|
-
export type TestHarnessAdapter
|
|
7
|
+
export type TestHarnessAdapter<TConfig> = (
|
|
8
8
|
children: React.Node,
|
|
9
9
|
config: TConfig,
|
|
10
10
|
) => React.Element<any>;
|
|
@@ -36,7 +36,7 @@ type ExtractMaybeConfig = <TConfig>(TestHarnessAdapter<TConfig>) => ?TConfig;
|
|
|
36
36
|
*
|
|
37
37
|
* This is the `TestHarnessAdapter` equivalent of `React.ElementConfig`.
|
|
38
38
|
*/
|
|
39
|
-
export type TestHarnessConfig
|
|
39
|
+
export type TestHarnessConfig<TAdapter> = $Call<ExtractConfig, TAdapter>;
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
42
|
* The `TestHarnessConfigs` type as defined by parsing a given set of adapters.
|
|
@@ -51,7 +51,7 @@ export type TestHarnessConfig<-TAdapter> = $Call<ExtractConfig, TAdapter>;
|
|
|
51
51
|
* functions too. Even worse, if the type doesn't match, it just allows `any`
|
|
52
52
|
* in the `Configs` object, rather than indicating any kind of problem.
|
|
53
53
|
*/
|
|
54
|
-
export type TestHarnessConfigs
|
|
54
|
+
export type TestHarnessConfigs<TAdapters: TestHarnessAdapters> = $ObjMap<
|
|
55
55
|
TAdapters,
|
|
56
56
|
ExtractMaybeConfig,
|
|
57
57
|
>;
|