@khanacademy/wonder-blocks-testing 7.0.1 → 7.0.2
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/dist/es/index.js +4 -3
- package/dist/index.js +6 -5
- package/package.json +1 -1
- package/src/fixtures/__tests__/fixtures.test.js +18 -0
- package/src/fixtures/fixtures.js +5 -3
package/CHANGELOG.md
CHANGED
package/dist/es/index.js
CHANGED
|
@@ -17,12 +17,13 @@ const fixtures = Component => {
|
|
|
17
17
|
|
|
18
18
|
const getProps = options => typeof props === "function" ? props(options) : props;
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
const RealComponent = wrapper || Component;
|
|
21
|
+
let Template = templateMap.get(RealComponent);
|
|
21
22
|
|
|
22
23
|
if (Template == null) {
|
|
23
|
-
Template = args => React.createElement(
|
|
24
|
+
Template = args => React.createElement(RealComponent, args);
|
|
24
25
|
|
|
25
|
-
templateMap.set(
|
|
26
|
+
templateMap.set(RealComponent, Template);
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
const story = Template.bind({});
|
package/dist/index.js
CHANGED
|
@@ -719,20 +719,21 @@ const fixtures = Component => {
|
|
|
719
719
|
const makeStory = (description, props, wrapper = null) => {
|
|
720
720
|
const storyName = `${storyNumber++} ${description}`;
|
|
721
721
|
|
|
722
|
-
const getProps = options => typeof props === "function" ? props(options) : props;
|
|
722
|
+
const getProps = options => typeof props === "function" ? props(options) : props;
|
|
723
|
+
|
|
724
|
+
const RealComponent = wrapper || Component; // We create a “template” of how args map to rendering
|
|
723
725
|
// for each type of component as the component here could
|
|
724
726
|
// be the component under test, or wrapped in a wrapper
|
|
725
727
|
// component. We don't use decorators for the wrapper
|
|
726
728
|
// because we may not be in a storybook context and it
|
|
727
729
|
// keeps the framework API simpler this way.
|
|
728
730
|
|
|
729
|
-
|
|
730
|
-
let Template = templateMap.get(Component);
|
|
731
|
+
let Template = templateMap.get(RealComponent);
|
|
731
732
|
|
|
732
733
|
if (Template == null) {
|
|
733
|
-
Template = args => /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__["createElement"](
|
|
734
|
+
Template = args => /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__["createElement"](RealComponent, args);
|
|
734
735
|
|
|
735
|
-
templateMap.set(
|
|
736
|
+
templateMap.set(RealComponent, Template);
|
|
736
737
|
} // Each story that shares that component then reuses that
|
|
737
738
|
// template.
|
|
738
739
|
|
package/package.json
CHANGED
|
@@ -128,5 +128,23 @@ describe("fixtures", () => {
|
|
|
128
128
|
screen.getByText('I rendered {"aProp":"aValue"}'),
|
|
129
129
|
).toBeInTheDocument();
|
|
130
130
|
});
|
|
131
|
+
|
|
132
|
+
it("should render the wrapper", () => {
|
|
133
|
+
// Arrange
|
|
134
|
+
const fixture = fixtures(
|
|
135
|
+
(props) => `I rendered ${JSON.stringify(props)}`,
|
|
136
|
+
);
|
|
137
|
+
const Fixture: any = fixture(
|
|
138
|
+
"A simple story",
|
|
139
|
+
{},
|
|
140
|
+
() => "I am a wrapper",
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
// Act
|
|
144
|
+
render(<Fixture aProp="aValue" />);
|
|
145
|
+
|
|
146
|
+
// Assert
|
|
147
|
+
expect(screen.getByText("I am a wrapper")).toBeInTheDocument();
|
|
148
|
+
});
|
|
131
149
|
});
|
|
132
150
|
});
|
package/src/fixtures/fixtures.js
CHANGED
|
@@ -44,16 +44,18 @@ export const fixtures = <
|
|
|
44
44
|
const getProps = (options) =>
|
|
45
45
|
typeof props === "function" ? props(options) : props;
|
|
46
46
|
|
|
47
|
+
const RealComponent = wrapper || Component;
|
|
48
|
+
|
|
47
49
|
// We create a “template” of how args map to rendering
|
|
48
50
|
// for each type of component as the component here could
|
|
49
51
|
// be the component under test, or wrapped in a wrapper
|
|
50
52
|
// component. We don't use decorators for the wrapper
|
|
51
53
|
// because we may not be in a storybook context and it
|
|
52
54
|
// keeps the framework API simpler this way.
|
|
53
|
-
let Template = templateMap.get((
|
|
55
|
+
let Template = templateMap.get((RealComponent: any));
|
|
54
56
|
if (Template == null) {
|
|
55
|
-
Template = (args) => <
|
|
56
|
-
templateMap.set((
|
|
57
|
+
Template = (args) => <RealComponent {...args} />;
|
|
58
|
+
templateMap.set((RealComponent: any), Template);
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
// Each story that shares that component then reuses that
|