@khanacademy/wonder-blocks-testing 5.0.2 → 7.0.0

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/es/index.js +19 -191
  3. package/dist/index.js +87 -462
  4. package/package.json +2 -2
  5. package/src/__docs__/_overview_fixtures.stories.mdx +2 -6
  6. package/src/__docs__/exports.fixtures.stories.mdx +13 -27
  7. package/src/__docs__/types.get-props-options.stories.mdx +28 -1
  8. package/src/fixtures/__tests__/fixtures.test.js +75 -470
  9. package/src/fixtures/fixtures.basic.stories.js +34 -49
  10. package/src/fixtures/fixtures.defaultwrapper.stories.js +26 -40
  11. package/src/fixtures/fixtures.js +57 -100
  12. package/src/fixtures/types.js +7 -188
  13. package/src/index.js +1 -14
  14. package/src/__docs__/exports.fixture-adapters.stories.mdx +0 -49
  15. package/src/__docs__/exports.setup-fixtures.stories.mdx +0 -22
  16. package/src/__docs__/types.custom-mount-props.stories.mdx +0 -35
  17. package/src/__docs__/types.fixtures-adapter-factory.stories.mdx +0 -23
  18. package/src/__docs__/types.fixtures-adapter-fixture-options.stories.mdx +0 -35
  19. package/src/__docs__/types.fixtures-adapter-group-options.stories.mdx +0 -37
  20. package/src/__docs__/types.fixtures-adapter-group.stories.mdx +0 -43
  21. package/src/__docs__/types.fixtures-adapter-options.stories.mdx +0 -21
  22. package/src/__docs__/types.fixtures-adapter.stories.mdx +0 -35
  23. package/src/__docs__/types.fixtures-configuration.stories.mdx +0 -35
  24. package/src/__docs__/types.fixtures-options.stories.mdx +0 -51
  25. package/src/fixtures/__tests__/combine-options.test.js +0 -65
  26. package/src/fixtures/__tests__/combine-top-level.test.js +0 -100
  27. package/src/fixtures/__tests__/setup.test.js +0 -71
  28. package/src/fixtures/adapters/__tests__/__snapshots__/adapter-group.test.js.snap +0 -9
  29. package/src/fixtures/adapters/__tests__/__snapshots__/adapter.test.js.snap +0 -13
  30. package/src/fixtures/adapters/__tests__/adapter-group.test.js +0 -223
  31. package/src/fixtures/adapters/__tests__/adapter.test.js +0 -97
  32. package/src/fixtures/adapters/__tests__/storybook.test.js +0 -329
  33. package/src/fixtures/adapters/adapter-group.js +0 -88
  34. package/src/fixtures/adapters/adapter.js +0 -63
  35. package/src/fixtures/adapters/adapters.js +0 -2
  36. package/src/fixtures/adapters/storybook.js +0 -125
  37. package/src/fixtures/combine-options.js +0 -25
  38. package/src/fixtures/combine-top-level.js +0 -44
  39. package/src/fixtures/setup.js +0 -30
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @khanacademy/wonder-blocks-testing
2
2
 
3
+ ## 7.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - d078f526: Simplify fixtures API to target only CSF/storybook
8
+
9
+ ## 6.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - b79979b8: Add `logHandler` to GetPropsOptions to simplify generic event logging for common use cases
14
+
15
+ ## 6.0.0
16
+
17
+ ### Major Changes
18
+
19
+ - af459222: Improve typing for fixtures call
20
+
3
21
  ## 5.0.2
4
22
 
5
23
  ### Patch Changes
package/dist/es/index.js CHANGED
@@ -1,209 +1,37 @@
1
- import _extends from '@babel/runtime/helpers/extends';
2
1
  import * as React from 'react';
3
2
  import { action } from '@storybook/addon-actions';
4
- import { clone } from '@khanacademy/wonder-stuff-core';
5
3
  import { InterceptRequests } from '@khanacademy/wonder-blocks-data';
6
4
  import { StaticRouter, MemoryRouter, Switch, Route } from 'react-router-dom';
5
+ import _extends from '@babel/runtime/helpers/extends';
7
6
 
8
- class AdapterGroup {
9
- constructor(closeGroupFn, _options) {
10
- this.closeGroup = (adapterOptions = null) => {
11
- if (this._closeGroupFn == null) {
12
- throw new Error("Group already closed");
13
- }
14
-
15
- try {
16
- return this._closeGroupFn(this._options, adapterOptions, this._fixtures);
17
- } finally {
18
- this._closeGroupFn = null;
19
- }
20
- };
21
-
22
- this.declareFixture = options => {
23
- if (typeof options !== "object" || options === null) {
24
- throw new TypeError("options must be an object");
25
- }
26
-
27
- if (this._closeGroupFn == null) {
28
- throw new Error("Cannot declare fixtures after closing the group");
29
- }
30
-
31
- this._fixtures.push(options);
32
- };
33
-
34
- if (typeof closeGroupFn !== "function") {
35
- throw new TypeError("closeGroupFn must be a function");
36
- }
37
-
38
- if (typeof _options !== "object" || _options === null) {
39
- throw new TypeError("options must be an object");
40
- }
41
-
42
- this._closeGroupFn = closeGroupFn;
43
- this._options = _options;
44
- this._fixtures = [];
45
- }
46
-
47
- }
48
-
49
- class Adapter {
50
- constructor(name, closeGroupFn) {
51
- if (typeof name !== "string") {
52
- throw new TypeError("name must be a string");
53
- }
54
-
55
- if (name.trim() === "") {
56
- throw new Error("name must be a non-empty string");
57
- }
58
-
59
- if (typeof closeGroupFn !== "function") {
60
- throw new TypeError("closeGroupFn must be a function");
61
- }
62
-
63
- this._name = name;
64
- this._closeGroupFn = closeGroupFn;
65
- }
66
-
67
- get name() {
68
- return this._name;
69
- }
70
-
71
- declareGroup(options) {
72
- return new AdapterGroup(this._closeGroupFn, options);
73
- }
74
-
75
- }
76
-
77
- const getAdapter = (MountingComponent = null) => new Adapter("storybook", ({
78
- title,
79
- description: groupDescription,
80
- getDefaultTitle: _
81
- }, adapterOptions, declaredFixtures) => {
7
+ const fixtures = Component => {
82
8
  const templateMap = new WeakMap();
9
+ let storyNumber = 1;
10
+ const getPropsOptions = {
11
+ log: (message, ...args) => action(message).apply(void 0, args),
12
+ logHandler: action
13
+ };
83
14
 
84
- const log = (message, ...args) => action(message).apply(void 0, args);
15
+ const makeStory = (description, props, wrapper = null) => {
16
+ const storyName = `${storyNumber++} ${description}`;
17
+
18
+ const getProps = options => typeof props === "function" ? props(options) : props;
85
19
 
86
- const exports = declaredFixtures.reduce((acc, {
87
- description,
88
- getProps,
89
- component: Component
90
- }, i) => {
91
- const storyName = `${i + 1} ${description}`;
92
- const exportName = storyName.replace(/\b\w/g, c => c.toUpperCase()).replace(/[^\w]+/g, "").replace(/[_]+/g, "");
93
20
  let Template = templateMap.get(Component);
94
21
 
95
22
  if (Template == null) {
96
- Template = MountingComponent ? args => React.createElement(MountingComponent, {
97
- component: Component,
98
- props: args,
99
- log: log
100
- }) : args => React.createElement(Component, args);
101
- templateMap.set(Component, Template);
102
- }
103
-
104
- acc[exportName] = Template.bind({});
105
- acc[exportName].args = getProps({
106
- log
107
- });
108
- acc[exportName].storyName = storyName;
109
- return acc;
110
- }, {
111
- default: _extends({
112
- title
113
- }, adapterOptions)
114
- });
115
- return Object.freeze(exports);
116
- });
117
-
118
- var adapters$1 = /*#__PURE__*/Object.freeze({
119
- __proto__: null,
120
- storybook: getAdapter
121
- });
122
-
123
- let _configuration = null;
124
- const setup = configuration => {
125
- _configuration = configuration;
126
- };
127
- const getConfiguration = () => {
128
- if (_configuration == null) {
129
- throw new Error("Not configured");
130
- }
131
-
132
- return _configuration;
133
- };
134
-
135
- const combineTopLevel = (val1, val2) => {
136
- const obj2Clone = clone(val2);
137
-
138
- if (val1 !== null && val2 !== null && typeof val1 === "object" && typeof val2 === "object") {
139
- const val1IsArray = Array.isArray(val1);
140
- const val2IsArray = Array.isArray(val2);
141
-
142
- if (val1IsArray && val2IsArray) {
143
- return Array.from(new Set([].concat(val1, obj2Clone)));
144
- } else if (!val1IsArray && !val2IsArray) {
145
- return _extends({}, val1, obj2Clone);
146
- }
147
- }
23
+ Template = args => React.createElement(Component, args);
148
24
 
149
- return obj2Clone;
150
- };
151
-
152
- const combineOptions = (...toBeCombined) => {
153
- const combined = toBeCombined.filter(Boolean).reduce((acc, cur) => {
154
- for (const key of Object.keys(cur)) {
155
- acc[key] = combineTopLevel(acc[key], cur[key]);
25
+ templateMap.set(Component, Template);
156
26
  }
157
27
 
158
- return acc;
159
- }, {});
160
- return combined;
161
- };
162
-
163
- const normalizeOptions = componentOrOptions => {
164
- if (typeof componentOrOptions === "function" || typeof componentOrOptions.render === "function") {
165
- return {
166
- component: componentOrOptions
167
- };
168
- }
169
-
170
- return componentOrOptions;
171
- };
172
-
173
- const fixtures = (componentOrOptions, fn) => {
174
- var _additionalAdapterOpt;
175
-
176
- const {
177
- adapter,
178
- defaultAdapterOptions
179
- } = getConfiguration();
180
- const {
181
- title,
182
- component,
183
- description: groupDescription,
184
- defaultWrapper,
185
- additionalAdapterOptions
186
- } = normalizeOptions(componentOrOptions);
187
- const group = adapter.declareGroup({
188
- title,
189
- description: groupDescription,
190
- getDefaultTitle: () => component.displayName || component.name || "Component"
191
- });
192
-
193
- const addFixture = (description, props, wrapper = null) => {
194
- var _ref;
195
-
196
- group.declareFixture({
197
- description,
198
- getProps: options => typeof props === "function" ? props(options) : props,
199
- component: (_ref = wrapper != null ? wrapper : defaultWrapper) != null ? _ref : component
200
- });
28
+ const story = Template.bind({});
29
+ story.args = getProps(getPropsOptions);
30
+ story.storyName = storyName;
31
+ return story;
201
32
  };
202
33
 
203
- fn(addFixture);
204
- const groupAdapterOverrides = (_additionalAdapterOpt = additionalAdapterOptions == null ? void 0 : additionalAdapterOptions[adapter.name]) != null ? _additionalAdapterOpt : {};
205
- const combinedAdapterOptions = combineOptions(defaultAdapterOptions, groupAdapterOverrides);
206
- return group.closeGroup(combinedAdapterOptions);
34
+ return makeStory;
207
35
  };
208
36
 
209
37
  const getHref = input => {
@@ -572,4 +400,4 @@ const hookHarness = makeHookHarness(DefaultAdapters, DefaultConfigs);
572
400
 
573
401
  const testHarness = makeTestHarness(DefaultAdapters, DefaultConfigs);
574
402
 
575
- export { RespondWith, adapters$1 as fixtureAdapters, fixtures, adapters as harnessAdapters, hookHarness, makeHookHarness, makeTestHarness, mockFetch, mockGqlFetch, setup as setupFixtures, testHarness };
403
+ export { RespondWith, fixtures, adapters as harnessAdapters, hookHarness, makeHookHarness, makeTestHarness, mockFetch, mockGqlFetch, testHarness };