UNPKG

10 kBJavaScriptView Raw
1"use strict";
2
3function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
5var _react = _interopRequireDefault(require("react"));
6
7var _react2 = require("@testing-library/react");
8
9var _coreEvents = require("@storybook/core-events");
10
11var _theming = require("@storybook/theming");
12
13var _Panel = _interopRequireWildcard(require("./Panel"));
14
15var _shared = require("../shared");
16
17function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
18
19function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
22
23var createTestApi = function createTestApi() {
24 return {
25 on: jest.fn(function () {
26 return function () {};
27 }),
28 off: jest.fn(),
29 emit: jest.fn(),
30 getQueryParam: jest.fn(function () {
31 return undefined;
32 }),
33 setQueryParams: jest.fn()
34 };
35};
36
37describe('Panel', function () {
38 it('should subscribe to setKnobs event of channel', function () {
39 var testApi = createTestApi();
40 (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_theming.ThemeProvider, {
41 theme: (0, _theming.convert)(_theming.themes.light)
42 }, /*#__PURE__*/_react["default"].createElement(_Panel["default"], {
43 api: testApi,
44 active: true
45 })));
46 expect(testApi.on).toHaveBeenCalledWith(_shared.SET, expect.any(Function));
47 });
48 it('should subscribe to STORY_CHANGE event', function () {
49 var testApi = createTestApi();
50 (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_theming.ThemeProvider, {
51 theme: (0, _theming.convert)(_theming.themes.light)
52 }, /*#__PURE__*/_react["default"].createElement(_Panel["default"], {
53 api: testApi,
54 active: true
55 })));
56 expect(testApi.on.mock.calls).toContainEqual([_coreEvents.STORY_CHANGED, expect.any(Function)]);
57 expect(testApi.on).toHaveBeenCalledWith(_shared.SET, expect.any(Function));
58 });
59 describe('setKnobs handler', function () {
60 it('should read url params and set values for existing knobs', function () {
61 var handlers = {};
62 var testQueryParams = {
63 'knob-foo': 'test string',
64 bar: 'some other string'
65 };
66 var testApi = {
67 on: function on(e, handler) {
68 handlers[e] = handler;
69 return function () {};
70 },
71 off: jest.fn(),
72 emit: jest.fn(),
73 getQueryParam: function getQueryParam(key) {
74 return testQueryParams[key];
75 },
76 setQueryParams: jest.fn()
77 };
78 (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_theming.ThemeProvider, {
79 theme: (0, _theming.convert)(_theming.themes.light)
80 }, /*#__PURE__*/_react["default"].createElement(_Panel["default"], {
81 api: testApi,
82 active: true
83 })));
84 var setKnobsHandler = handlers[_shared.SET];
85 var knobs = {
86 foo: {
87 name: 'foo',
88 value: 'default string',
89 type: 'text'
90 },
91 baz: {
92 name: 'baz',
93 value: 'another knob value',
94 type: 'text'
95 }
96 };
97 setKnobsHandler({
98 knobs: knobs,
99 timestamp: +new Date()
100 });
101 var knobFromUrl = {
102 name: 'foo',
103 value: testQueryParams['knob-foo'],
104 type: 'text'
105 };
106 var e = _shared.CHANGE;
107 expect(testApi.emit).toHaveBeenCalledWith(e, knobFromUrl);
108 });
109 });
110 describe('handleChange()', function () {
111 it.skip('should set queryParams and emit knobChange event', function () {
112 var testApi = {
113 getQueryParam: jest.fn(),
114 setQueryParams: jest.fn(function () {
115 return undefined;
116 }),
117 on: jest.fn(function () {
118 return function () {};
119 }),
120 off: jest.fn(),
121 emit: jest.fn()
122 };
123 (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_Panel["default"], {
124 api: testApi,
125 active: true
126 }));
127 var testChangedKnob = {
128 name: 'foo',
129 value: 'changed text',
130 type: 'text'
131 }; // todo
132 // wrapper.instance().handleChange(testChangedKnob);
133
134 expect(testApi.emit).toHaveBeenCalledWith(_shared.CHANGE, testChangedKnob); // const paramsChange = { 'knob-foo': 'changed text' };
135 // expect(testApi.setQueryParams).toHaveBeenCalledWith(paramsChange);
136 });
137 });
138 describe('groups', function () {
139 var testApi = {
140 off: jest.fn(),
141 emit: jest.fn(),
142 getQueryParam: jest.fn(function () {
143 return undefined;
144 }),
145 setQueryParams: jest.fn(),
146 on: jest.fn(function () {
147 return function () {};
148 })
149 };
150 it.skip('should have no tabs when there are no groupIds', function () {
151 // Unfortunately, a shallow render will not invoke the render() function of the groups --
152 // it thinks they are unnamed function components (what they effectively are anyway).
153 //
154 // We have to do a full mount.
155 var root = (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_theming.ThemeProvider, {
156 theme: (0, _theming.convert)(_theming.themes.light)
157 }, /*#__PURE__*/_react["default"].createElement(_Panel["default"], {
158 api: testApi,
159 active: true
160 }))); // testApi.on.mock.calls[0][1]({
161 // knobs: {
162 // foo: {
163 // name: 'foo',
164 // defaultValue: 'test',
165 // used: true,
166 // // no groupId
167 // },
168 // bar: {
169 // name: 'bar',
170 // defaultValue: 'test2',
171 // used: true,
172 // // no groupId
173 // },
174 // },
175 // });
176 // root.rerender();
177 // const wrapper = root.find(Panel);
178 // const formWrapper = wrapper.find(PropForm);
179 // const knobs = formWrapper.map((formInstanceWrapper) => formInstanceWrapper.prop('knobs'));
180 // expect(knobs).toMatchSnapshot();
181 // root.unmount();
182 });
183 it.skip('should have one tab per groupId when all are defined', function () {
184 var root = (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_theming.ThemeProvider, {
185 theme: (0, _theming.convert)(_theming.themes.light)
186 }, /*#__PURE__*/_react["default"].createElement(_Panel["default"], {
187 api: testApi,
188 active: true
189 }))); // testApi.on.mock.calls[0][1]({
190 // knobs: {
191 // foo: {
192 // name: 'foo',
193 // defaultValue: 'test',
194 // used: true,
195 // groupId: 'foo',
196 // },
197 // bar: {
198 // name: 'bar',
199 // defaultValue: 'test2',
200 // used: true,
201 // groupId: 'bar',
202 // },
203 // },
204 // });
205 // const wrapper = root.update().find(Panel);
206 // const titles = wrapper
207 // .find(TabsState)
208 // .find('button')
209 // .map((child) => child.prop('children'));
210 // expect(titles).toEqual(['foo', 'bar']);
211 // const knobs = wrapper.find(PropForm);
212 // // but it should not have its own PropForm in this case
213 // expect(knobs.length).toEqual(titles.length);
214 // expect(knobs).toMatchSnapshot();
215 // root.unmount();
216 });
217 it.skip("the ".concat(_Panel.DEFAULT_GROUP_ID, " tab should have its own additional content when there are knobs both with and without a groupId"), function () {
218 var root = (0, _react2.render)( /*#__PURE__*/_react["default"].createElement(_theming.ThemeProvider, {
219 theme: (0, _theming.convert)(_theming.themes.light)
220 }, /*#__PURE__*/_react["default"].createElement(_Panel["default"], {
221 api: testApi,
222 active: true
223 }))); // testApi.on.mock.calls[0][1]({
224 // knobs: {
225 // bar: {
226 // name: 'bar',
227 // defaultValue: 'test2',
228 // used: true,
229 // // no groupId
230 // },
231 // foo: {
232 // name: 'foo',
233 // defaultValue: 'test',
234 // used: true,
235 // groupId: 'foo',
236 // },
237 // },
238 // });
239 // const wrapper = root.update().find(Panel);
240 // const titles = wrapper
241 // .find(TabsState)
242 // .find('button')
243 // .map((child) => child.prop('children'));
244 // expect(titles).toEqual(['foo', DEFAULT_GROUP_ID]);
245 // const knobs = wrapper.find(PropForm).map((propForm) => propForm.prop('knobs'));
246 // // there are props with no groupId so Other should also have its own PropForm
247 // expect(knobs.length).toEqual(titles.length);
248 // expect(knobs).toMatchSnapshot();
249 // root.unmount();
250 });
251 });
252});
\No newline at end of file