-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
ArgsStore.test.ts
211 lines (166 loc) · 6.59 KB
/
ArgsStore.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { ArgsStore } from './ArgsStore';
jest.mock('@storybook/client-logger');
const stringType = { type: { name: 'string' } };
const booleanType = { type: { name: 'boolean' } };
describe('ArgsStore', () => {
describe('setInitial / get', () => {
it('returns in a straightforward way', () => {
const store = new ArgsStore();
store.setInitial({ id: 'id', initialArgs: { foo: 'bar' } } as any);
expect(store.get('id')).toEqual({ foo: 'bar' });
});
it('throws if you try to get non-existent', () => {
const store = new ArgsStore();
expect(() => store.get('id')).toThrow(/No args known/);
});
describe('on second call for same story', () => {
describe('if initialArgs are unchanged', () => {
it('does nothing if the args are untouched', () => {
const store = new ArgsStore();
const previousStory = {
id: 'id',
initialArgs: { a: '1', b: '1' },
argTypes: { a: stringType, b: stringType },
} as any;
store.setInitial(previousStory);
const story = {
id: 'id',
initialArgs: { a: '1', b: '1' },
argTypes: { a: stringType, b: stringType },
} as any;
store.setInitial(story);
expect(store.get(story.id)).toEqual({ a: '1', b: '1' });
});
it('retains any arg changes', () => {
const store = new ArgsStore();
const previousStory = {
id: 'id',
initialArgs: { a: '1', b: false, c: 'unchanged' },
argTypes: { a: stringType, b: booleanType, c: stringType },
} as any;
store.setInitial(previousStory);
// NOTE: I'm not sure technically you should be allowed to set d here, but
// let's make sure we behave sensibly if you do
store.update('id', { a: 'update', b: true, d: 'update' });
const story = {
id: 'id',
initialArgs: { a: '1', b: false, c: 'unchanged' },
argTypes: { a: stringType, b: booleanType, c: stringType },
} as any;
store.setInitial(story);
// In any case c is not retained.
expect(store.get(story.id)).toEqual({ a: 'update', b: true, c: 'unchanged' });
});
});
describe('when initialArgs change', () => {
it('replaces old args with new if the args are untouched', () => {
const store = new ArgsStore();
const previousStory = {
id: 'id',
initialArgs: { a: '1', b: '1' },
argTypes: { a: stringType, b: stringType },
} as any;
store.setInitial(previousStory);
const story = {
id: 'id',
initialArgs: { a: '1', c: '1' },
argTypes: { a: stringType, c: stringType },
} as any;
store.setInitial(story);
expect(store.get(story.id)).toEqual({ a: '1', c: '1' });
});
it('applies the same delta if the args are changed', () => {
const store = new ArgsStore();
const previousStory = {
id: 'id',
initialArgs: { a: '1', b: '1' },
argTypes: { a: stringType, b: stringType },
} as any;
store.setInitial(previousStory);
// NOTE: I'm not sure technically you should be allowed to set c here
store.update('id', { a: 'update', c: 'update' });
const story = {
id: 'id',
initialArgs: { a: '2', d: '2' },
argTypes: { a: stringType, d: stringType },
} as any;
store.setInitial(story);
// In any case c is not retained.
expect(store.get(story.id)).toEqual({ a: 'update', d: '2' });
});
});
});
});
describe('update', () => {
it('overrides on a per-key basis', () => {
const store = new ArgsStore();
store.setInitial({ id: 'id', initialArgs: {} } as any);
store.update('id', { foo: 'bar' });
expect(store.get('id')).toEqual({ foo: 'bar' });
store.update('id', { baz: 'bing' });
expect(store.get('id')).toEqual({ foo: 'bar', baz: 'bing' });
});
it('does not merge objects', () => {
const store = new ArgsStore();
store.setInitial({ id: 'id', initialArgs: {} } as any);
store.update('id', { obj: { foo: 'bar' } });
expect(store.get('id')).toEqual({ obj: { foo: 'bar' } });
store.update('id', { obj: { baz: 'bing' } });
expect(store.get('id')).toEqual({ obj: { baz: 'bing' } });
});
it('does not set keys to undefined, it simply unsets them', () => {
const store = new ArgsStore();
store.setInitial({ id: 'id', initialArgs: { foo: 'bar' } } as any);
store.update('id', { foo: undefined });
expect('foo' in store.get('id')).toBe(false);
});
});
describe('updateFromPersisted', () => {
it('ensures the types of args are correct', () => {
const store = new ArgsStore();
store.setInitial({ id: 'id', initialArgs: {} } as any);
const story = {
id: 'id',
argTypes: { a: stringType },
} as any;
store.updateFromPersisted(story, { a: 'str' });
expect(store.get('id')).toEqual({ a: 'str' });
store.updateFromPersisted(story, { a: 42 });
expect(store.get('id')).toEqual({ a: '42' });
});
it('merges objects and sparse arrays', () => {
const store = new ArgsStore();
store.setInitial({ id: 'id', initialArgs: { a: { foo: 'bar' }, b: ['1', '2', '3'] } } as any);
const story = {
id: 'id',
argTypes: {
a: { type: { name: 'object', value: { name: 'string' } } },
b: { type: { name: 'array', value: { name: 'string' } } },
},
} as any;
store.updateFromPersisted(story, { a: { baz: 'bing' } });
expect(store.get('id')).toEqual({
a: { foo: 'bar', baz: 'bing' },
b: ['1', '2', '3'],
});
// eslint-disable-next-line no-sparse-arrays
store.updateFromPersisted(story, { b: [, , '4'] });
expect(store.get('id')).toEqual({
a: { foo: 'bar', baz: 'bing' },
b: ['1', '2', '4'],
});
});
it('checks args are allowed options', () => {
const store = new ArgsStore();
store.setInitial({ id: 'id', initialArgs: {} } as any);
const story = {
id: 'id',
argTypes: { a: { type: { name: 'string' }, options: ['a', 'b'] } },
} as any;
store.updateFromPersisted(story, { a: 'random' });
expect(store.get('id')).toEqual({});
store.updateFromPersisted(story, { a: 'a' });
expect(store.get('id')).toEqual({ a: 'a' });
});
});
});