/
action_creator.spec.ts
161 lines (136 loc) · 5.07 KB
/
action_creator.spec.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
import { createAction, props, union } from '@ngrx/store';
import { expecter } from 'ts-snippet';
describe('Action Creators', () => {
let originalTimeout: number;
beforeEach(() => {
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 2000;
});
afterEach(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});
const expectSnippet = expecter(
code => `
// path goes from root
import {createAction, props, union} from './modules/store/src/action_creator';
${code}`,
{
moduleResolution: 'node',
target: 'es2015',
}
);
describe('createAction', () => {
it('should create an action', () => {
const foo = createAction('FOO', (foo: number) => ({ foo }));
const fooAction = foo(42);
expect(fooAction).toEqual({ type: 'FOO', foo: 42 });
});
it('should narrow the action', () => {
const foo = createAction('FOO', (foo: number) => ({ foo }));
const bar = createAction('BAR', (bar: number) => ({ bar }));
const both = union({ foo, bar });
const narrow = (action: typeof both) => {
if (action.type === foo.type) {
expect(action.foo).toEqual(42);
} else {
throw new Error('Should not get here.');
}
};
narrow(foo(42));
});
it('should be serializable', () => {
const foo = createAction('FOO', (foo: number) => ({ foo }));
const fooAction = foo(42);
const text = JSON.stringify(fooAction);
expect(JSON.parse(text)).toEqual({ type: 'FOO', foo: 42 });
});
it('should enforce ctor parameters', () => {
expectSnippet(`
const foo = createAction('FOO', (foo: number) => ({ foo }));
const fooAction = foo('42');
`).toFail(/not assignable to parameter of type 'number'/);
});
it('should enforce action property types', () => {
expectSnippet(`
const foo = createAction('FOO', (foo: number) => ({ foo }));
const fooAction = foo(42);
const value: string = fooAction.foo;
`).toFail(/'number' is not assignable to type 'string'/);
});
it('should enforce action property names', () => {
expectSnippet(`
const foo = createAction('FOO', (foo: number) => ({ foo }));
const fooAction = foo(42);
const value = fooAction.bar;
`).toFail(/'bar' does not exist on type/);
});
it('should not allow type property', () => {
expectSnippet(`
const foo = createAction('FOO', (type: string) => ({type}));
`).toFail(
/Type '{ type: string; }' is not assignable to type '"type property is not allowed in action creators"/
);
});
});
describe('empty', () => {
it('should allow empty action', () => {
const foo = createAction('FOO');
const fooAction = foo();
expect(fooAction).toEqual({ type: 'FOO' });
});
});
describe('props', () => {
it('should create an action', () => {
const foo = createAction('FOO', props<{ foo: number }>());
const fooAction = foo({ foo: 42 });
expect(fooAction).toEqual({ type: 'FOO', foo: 42 });
});
it('should narrow the action', () => {
const foo = createAction('FOO', props<{ foo: number }>());
const bar = createAction('BAR', props<{ bar: number }>());
const both = union({ foo, bar });
const narrow = (action: typeof both) => {
if (action.type === foo.type) {
expect(action.foo).toEqual(42);
} else {
throw new Error('Should not get here.');
}
};
narrow(foo({ foo: 42 }));
});
it('should be serializable', () => {
const foo = createAction('FOO', props<{ foo: number }>());
const fooAction = foo({ foo: 42 });
const text = JSON.stringify(fooAction);
expect(JSON.parse(text)).toEqual({ foo: 42, type: 'FOO' });
});
it('should enforce ctor parameters', () => {
expectSnippet(`
const foo = createAction('FOO', props<{ foo: number }>());
const fooAction = foo({ foo: '42' });
`).toFail(/'string' is not assignable to type 'number'/);
});
it('should enforce action property types', () => {
expectSnippet(`
const foo = createAction('FOO', props<{ foo: number }>());
const fooAction = foo({ foo: 42 });
const value: string = fooAction.foo;
`).toFail(/'number' is not assignable to type 'string'/);
});
it('should enforce action property names', () => {
expectSnippet(`
const foo = createAction('FOO', props<{ foo: number }>());
const fooAction = foo({ foo: 42 });
const value = fooAction.bar;
`).toFail(/'bar' does not exist on type/);
});
it('should not allow type property', () => {
const foo = createAction('FOO', props<{ type: number }>() as any);
expectSnippet(`
const foo = createAction('FOO', props<{ type: number }>());
`).toFail(
/Argument of type '"type property is not allowed in action creators"' is not assignable to parameter of type/
);
});
});
});