-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
addArgs.test.ts
67 lines (59 loc) · 2.36 KB
/
addArgs.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
import { StoryContext } from '@storybook/addons';
import { inferActionsFromArgTypesRegex, addActionsFromArgTypes } from './addArgs';
describe('actions parameter enhancers', () => {
describe('actions.argTypesRegex parameter', () => {
const baseParameters = {
argTypes: { onClick: {}, onFocus: {}, somethingElse: {} },
actions: { argTypesRegex: '^on.*' },
};
it('should add actions that match a pattern', () => {
const parameters = baseParameters;
const { args } = inferActionsFromArgTypesRegex({ parameters } as StoryContext);
expect(Object.keys(args)).toEqual(['onClick', 'onFocus']);
});
it('should prioritize pre-existing args', () => {
const parameters = {
...baseParameters,
args: { onClick: 'pre-existing arg' },
};
const { args } = inferActionsFromArgTypesRegex({ parameters } as StoryContext);
expect(Object.keys(args)).toEqual(['onClick', 'onFocus']);
expect(args.onClick).toEqual('pre-existing arg');
});
it('should do nothing if actions are disabled', () => {
const parameters = {
...baseParameters,
actions: { ...baseParameters.actions, disable: true },
};
const result = inferActionsFromArgTypesRegex({ parameters } as StoryContext);
expect(result).toBeFalsy();
});
});
describe('argTypes.action parameter', () => {
const baseParameters = {
argTypes: {
onClick: { action: 'clicked!' },
onBlur: { action: 'blurred!' },
},
};
it('should add actions based on action.args', () => {
const parameters = baseParameters;
const { args } = addActionsFromArgTypes({ parameters } as StoryContext);
expect(Object.keys(args)).toEqual(['onClick', 'onBlur']);
});
it('should prioritize pre-existing args', () => {
const parameters = {
...baseParameters,
args: { onClick: 'pre-existing arg' },
};
const { args } = addActionsFromArgTypes({ parameters } as StoryContext);
expect(Object.keys(args)).toEqual(['onClick', 'onBlur']);
expect(args.onClick).toEqual('pre-existing arg');
});
it('should do nothing if actions are disabled', () => {
const parameters = { ...baseParameters, actions: { disable: true } };
const result = addActionsFromArgTypes({ parameters } as StoryContext);
expect(result).toBeFalsy();
});
});
});