1 | "use strict";
|
2 |
|
3 | var _KnobManager = _interopRequireDefault(require("./KnobManager"));
|
4 |
|
5 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
6 |
|
7 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
8 |
|
9 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
10 |
|
11 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
12 |
|
13 | jest.mock('global', function () {
|
14 | return {
|
15 | navigator: {
|
16 | userAgent: 'browser',
|
17 | platform: ''
|
18 | },
|
19 | window: {
|
20 | __STORYBOOK_CLIENT_API__: undefined,
|
21 | addEventListener: jest.fn(),
|
22 | location: {
|
23 | search: ''
|
24 | },
|
25 | history: {
|
26 | replaceState: jest.fn()
|
27 | }
|
28 | },
|
29 | document: {
|
30 | addEventListener: jest.fn(),
|
31 | getElementById: jest.fn().mockReturnValue({}),
|
32 | body: {
|
33 | classList: {
|
34 | add: jest.fn(),
|
35 | remove: jest.fn()
|
36 | }
|
37 | },
|
38 | documentElement: {},
|
39 | location: {
|
40 | search: '?id=kind--story'
|
41 | }
|
42 | }
|
43 | };
|
44 | });
|
45 | describe('KnobManager', function () {
|
46 | describe('knob()', function () {
|
47 | describe('when the knob is present in the knobStore', function () {
|
48 | var testManager = new _KnobManager["default"]();
|
49 | beforeEach(function () {
|
50 | testManager.knobStore = {
|
51 | set: jest.fn(),
|
52 | update: jest.fn(),
|
53 | get: function get() {
|
54 | return {
|
55 | defaultValue: 'default value',
|
56 | name: 'foo',
|
57 | type: 'string',
|
58 | value: 'current value'
|
59 | };
|
60 | }
|
61 | };
|
62 | });
|
63 | it('should return the existing knob value when types match', function () {
|
64 | var defaultKnob = {
|
65 | name: 'foo',
|
66 | type: 'string',
|
67 | value: 'default value'
|
68 | };
|
69 | var knob = testManager.knob('foo', defaultKnob);
|
70 | expect(knob).toEqual('current value');
|
71 | expect(testManager.knobStore.set).not.toHaveBeenCalled();
|
72 | });
|
73 | it('should update the existing knob options when types match', function () {
|
74 | var defaultKnob = {
|
75 | name: 'foo',
|
76 | type: 'string',
|
77 | value: 'default value',
|
78 | foo: 'foo'
|
79 | };
|
80 | testManager.knob('foo', defaultKnob);
|
81 | expect(testManager.knobStore.update).toHaveBeenCalledWith('foo', expect.objectContaining({
|
82 | foo: 'foo'
|
83 | }));
|
84 | });
|
85 | it('should return the new default knob value when type has changed', function () {
|
86 | var defaultKnob = {
|
87 | name: 'foo',
|
88 | value: true,
|
89 | type: 'boolean'
|
90 | };
|
91 | testManager.knob('foo', defaultKnob);
|
92 |
|
93 | var newKnob = _objectSpread(_objectSpread({}, defaultKnob), {}, {
|
94 | label: 'foo',
|
95 | defaultValue: defaultKnob.value
|
96 | });
|
97 |
|
98 | expect(testManager.knobStore.set).toHaveBeenCalledWith('foo', newKnob);
|
99 | });
|
100 | });
|
101 | describe('when the knob is not present in the knobStore', function () {
|
102 | var testManager = new _KnobManager["default"]();
|
103 | beforeEach(function () {
|
104 | testManager.knobStore = {
|
105 | set: jest.fn(),
|
106 | get: jest.fn()
|
107 | };
|
108 | testManager.knobStore.get.mockImplementationOnce(function () {
|
109 | return undefined;
|
110 | }).mockImplementationOnce(function () {
|
111 | return 'normal value';
|
112 | });
|
113 | });
|
114 | it('should return the new default knob value when default has changed', function () {
|
115 | var defaultKnob = {
|
116 | name: 'foo',
|
117 | value: 'normal value'
|
118 | };
|
119 | testManager.knob('foo', defaultKnob);
|
120 |
|
121 | var newKnob = _objectSpread(_objectSpread({}, defaultKnob), {}, {
|
122 | label: 'foo',
|
123 | defaultValue: defaultKnob.value
|
124 | });
|
125 |
|
126 | expect(testManager.knobStore.set).toHaveBeenCalledWith('foo', newKnob);
|
127 | });
|
128 | });
|
129 | });
|
130 | }); |
\ | No newline at end of file |