@khanacademy/wonder-blocks-testing 6.1.0 → 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 (38) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/es/index.js +16 -187
  3. package/dist/index.js +87 -461
  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/fixtures/__tests__/fixtures.test.js +75 -470
  8. package/src/fixtures/fixtures.basic.stories.js +30 -50
  9. package/src/fixtures/fixtures.defaultwrapper.stories.js +26 -40
  10. package/src/fixtures/fixtures.js +53 -102
  11. package/src/fixtures/types.js +3 -194
  12. package/src/index.js +1 -14
  13. package/src/__docs__/exports.fixture-adapters.stories.mdx +0 -49
  14. package/src/__docs__/exports.setup-fixtures.stories.mdx +0 -22
  15. package/src/__docs__/types.custom-mount-props.stories.mdx +0 -35
  16. package/src/__docs__/types.fixtures-adapter-factory.stories.mdx +0 -23
  17. package/src/__docs__/types.fixtures-adapter-fixture-options.stories.mdx +0 -35
  18. package/src/__docs__/types.fixtures-adapter-group-options.stories.mdx +0 -37
  19. package/src/__docs__/types.fixtures-adapter-group.stories.mdx +0 -43
  20. package/src/__docs__/types.fixtures-adapter-options.stories.mdx +0 -21
  21. package/src/__docs__/types.fixtures-adapter.stories.mdx +0 -35
  22. package/src/__docs__/types.fixtures-configuration.stories.mdx +0 -35
  23. package/src/__docs__/types.fixtures-options.stories.mdx +0 -51
  24. package/src/fixtures/__tests__/combine-options.test.js +0 -65
  25. package/src/fixtures/__tests__/combine-top-level.test.js +0 -100
  26. package/src/fixtures/__tests__/setup.test.js +0 -71
  27. package/src/fixtures/adapters/__tests__/__snapshots__/adapter-group.test.js.snap +0 -9
  28. package/src/fixtures/adapters/__tests__/__snapshots__/adapter.test.js.snap +0 -13
  29. package/src/fixtures/adapters/__tests__/adapter-group.test.js +0 -223
  30. package/src/fixtures/adapters/__tests__/adapter.test.js +0 -97
  31. package/src/fixtures/adapters/__tests__/storybook.test.js +0 -369
  32. package/src/fixtures/adapters/adapter-group.js +0 -88
  33. package/src/fixtures/adapters/adapter.js +0 -63
  34. package/src/fixtures/adapters/adapters.js +0 -2
  35. package/src/fixtures/adapters/storybook.js +0 -128
  36. package/src/fixtures/combine-options.js +0 -25
  37. package/src/fixtures/combine-top-level.js +0 -44
  38. package/src/fixtures/setup.js +0 -30
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## 6.1.0
4
10
 
5
11
  ### Minor Changes
package/dist/es/index.js CHANGED
@@ -1,208 +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;
83
10
  const getPropsOptions = {
84
11
  log: (message, ...args) => action(message).apply(void 0, args),
85
12
  logHandler: action
86
13
  };
87
- const exports = declaredFixtures.reduce((acc, {
88
- description,
89
- getProps,
90
- component: Component
91
- }, i) => {
92
- const storyName = `${i + 1} ${description}`;
93
- const exportName = storyName.replace(/\b\w/g, c => c.toUpperCase()).replace(/[^\w]+/g, "").replace(/[_]+/g, "");
94
- let Template = templateMap.get(Component);
95
14
 
96
- if (Template == null) {
97
- Template = MountingComponent ? args => React.createElement(MountingComponent, {
98
- component: Component,
99
- props: args,
100
- log: getPropsOptions.log
101
- }) : args => React.createElement(Component, args);
102
- templateMap.set(Component, Template);
103
- }
15
+ const makeStory = (description, props, wrapper = null) => {
16
+ const storyName = `${storyNumber++} ${description}`;
104
17
 
105
- acc[exportName] = Template.bind({});
106
- acc[exportName].args = getProps(getPropsOptions);
107
- acc[exportName].storyName = storyName;
108
- return acc;
109
- }, {
110
- default: _extends({
111
- title
112
- }, adapterOptions)
113
- });
114
- return Object.freeze(exports);
115
- });
18
+ const getProps = options => typeof props === "function" ? props(options) : props;
116
19
 
117
- var adapters$1 = /*#__PURE__*/Object.freeze({
118
- __proto__: null,
119
- storybook: getAdapter
120
- });
121
-
122
- let _configuration = null;
123
- const setup = configuration => {
124
- _configuration = configuration;
125
- };
126
- const getConfiguration = () => {
127
- if (_configuration == null) {
128
- throw new Error("Not configured");
129
- }
130
-
131
- return _configuration;
132
- };
133
-
134
- const combineTopLevel = (val1, val2) => {
135
- const obj2Clone = clone(val2);
136
-
137
- if (val1 !== null && val2 !== null && typeof val1 === "object" && typeof val2 === "object") {
138
- const val1IsArray = Array.isArray(val1);
139
- const val2IsArray = Array.isArray(val2);
140
-
141
- if (val1IsArray && val2IsArray) {
142
- return Array.from(new Set([].concat(val1, obj2Clone)));
143
- } else if (!val1IsArray && !val2IsArray) {
144
- return _extends({}, val1, obj2Clone);
145
- }
146
- }
20
+ let Template = templateMap.get(Component);
147
21
 
148
- return obj2Clone;
149
- };
22
+ if (Template == null) {
23
+ Template = args => React.createElement(Component, args);
150
24
 
151
- const combineOptions = (...toBeCombined) => {
152
- const combined = toBeCombined.filter(Boolean).reduce((acc, cur) => {
153
- for (const key of Object.keys(cur)) {
154
- acc[key] = combineTopLevel(acc[key], cur[key]);
25
+ templateMap.set(Component, Template);
155
26
  }
156
27
 
157
- return acc;
158
- }, {});
159
- return combined;
160
- };
161
-
162
- const normalizeOptions = componentOrOptions => {
163
- if (typeof componentOrOptions === "function" || typeof componentOrOptions.render === "function") {
164
- return {
165
- component: componentOrOptions
166
- };
167
- }
168
-
169
- return componentOrOptions;
170
- };
171
-
172
- const fixtures = (componentOrOptions, fn) => {
173
- var _additionalAdapterOpt;
174
-
175
- const {
176
- adapter,
177
- defaultAdapterOptions
178
- } = getConfiguration();
179
- const {
180
- title,
181
- component,
182
- description: groupDescription,
183
- defaultWrapper,
184
- additionalAdapterOptions
185
- } = normalizeOptions(componentOrOptions);
186
- const group = adapter.declareGroup({
187
- title,
188
- description: groupDescription,
189
- getDefaultTitle: () => component.displayName || component.name || "Component"
190
- });
191
-
192
- const addFixture = (description, props, wrapper = null) => {
193
- var _ref;
194
-
195
- group.declareFixture({
196
- description,
197
- getProps: options => typeof props === "function" ? props(options) : props,
198
- component: (_ref = wrapper != null ? wrapper : defaultWrapper) != null ? _ref : component
199
- });
28
+ const story = Template.bind({});
29
+ story.args = getProps(getPropsOptions);
30
+ story.storyName = storyName;
31
+ return story;
200
32
  };
201
33
 
202
- fn(addFixture);
203
- const groupAdapterOverrides = (_additionalAdapterOpt = additionalAdapterOptions == null ? void 0 : additionalAdapterOptions[adapter.name]) != null ? _additionalAdapterOpt : {};
204
- const combinedAdapterOptions = combineOptions(defaultAdapterOptions, groupAdapterOverrides);
205
- return group.closeGroup(combinedAdapterOptions);
34
+ return makeStory;
206
35
  };
207
36
 
208
37
  const getHref = input => {
@@ -571,4 +400,4 @@ const hookHarness = makeHookHarness(DefaultAdapters, DefaultConfigs);
571
400
 
572
401
  const testHarness = makeTestHarness(DefaultAdapters, DefaultConfigs);
573
402
 
574
- 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 };