/
decorators.test.ts
70 lines (60 loc) · 2.13 KB
/
decorators.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
import { StoryContext } from '@storybook/addons';
import { defaultDecorateStory } from './decorators';
function makeContext(input: Record<string, any>): StoryContext {
return {
id: 'id',
kind: 'kind',
name: 'name',
viewMode: 'story',
parameters: {},
...input,
} as StoryContext;
}
describe('client-api.decorators', () => {
it('calls decorators in out to in order', () => {
const order = [];
const decorators = [
(s) => order.push(1) && s(),
(s) => order.push(2) && s(),
(s) => order.push(3) && s(),
];
const decorated = defaultDecorateStory(() => order.push(4), decorators);
expect(order).toEqual([]);
decorated();
expect(order).toEqual([3, 2, 1, 4]);
});
it('passes context through to sub decorators', () => {
const contexts = [];
const decorators = [
(s, c) => contexts.push(c) && s({ k: 1 }),
(s, c) => contexts.push(c) && s({ k: 2 }),
(s, c) => contexts.push(c) && s({ k: 3 }),
];
const decorated = defaultDecorateStory((c) => contexts.push(c), decorators);
expect(contexts).toEqual([]);
decorated(makeContext({ k: 0 }));
expect(contexts.map((c) => c.k)).toEqual([0, 3, 2, 1]);
});
it('merges contexts', () => {
const contexts = [];
const decorators = [(s, c) => contexts.push(c) && s({ c: 'd' })];
const decorated = defaultDecorateStory((c) => contexts.push(c), decorators);
expect(contexts).toEqual([]);
decorated(makeContext({ a: 'b' }));
expect(contexts).toEqual([
expect.objectContaining({ a: 'b' }),
expect.objectContaining({ a: 'b', c: 'd' }),
]);
});
it('DOES NOT merge parameter or pass through parameters key in context', () => {
const contexts = [];
const decorators = [(s, c) => contexts.push(c) && s({ parameters: { c: 'd' } })];
const decorated = defaultDecorateStory((c) => contexts.push(c), decorators);
expect(contexts).toEqual([]);
decorated(makeContext({ parameters: { a: 'b' } }));
expect(contexts).toEqual([
expect.objectContaining({ parameters: { a: 'b' } }),
expect.objectContaining({ parameters: { a: 'b' } }),
]);
});
});