-
Notifications
You must be signed in to change notification settings - Fork 2
/
Action.spec.js
90 lines (68 loc) · 2.83 KB
/
Action.spec.js
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
import { createAction } from '../redux-fluent';
describe('createAction', () => {
const type = '@@test/todos:add';
const payload = { test: 'test' };
const meta = { test: 'test' };
it('Factory should return a function actionCreator', () => {
expect(createAction('test')).toEqual(jasmine.any(Function));
});
it('Factory should accept a payloadCreator', () => {
const payloadCreator = jasmine.createSpy('payloadCreator')
.and.callFake(todoId => ({ id: todoId }));
const deleteTodo = createAction('@@todos/:id | delete', payloadCreator);
const todoId = 12;
expect(deleteTodo(todoId).payload).toEqual({ id: todoId });
expect(payloadCreator).toHaveBeenCalledWith(todoId);
});
it('Factory should accept a metaCreator', () => {
const metaCreator = jasmine.createSpy('metaCreator')
.and.callFake(metaThing => ({ id: metaThing }));
const deleteTodo = createAction('@@todos/:id | delete', null, metaCreator);
const metaThing = 'Hello World';
expect(deleteTodo(null, metaThing).meta).toEqual({ id: metaThing });
expect(metaCreator).toHaveBeenCalledWith(metaThing);
});
describe('actionCreator', () => {
it('should have a property type', () => {
expect(createAction(type)).toHaveProperty('type', type);
});
it('should have a property toString', () => {
expect(createAction('test')).toHaveProperty('toString', jasmine.any(Function));
});
it('toString() should return type', () => {
expect(createAction(type).toString()).toEqual(type);
});
it('should return an action', () => {
const actionCreator = createAction(type);
const action = actionCreator();
expect(action).toEqual({
type,
error: false,
payload: null,
meta: null,
});
expect(action.payload).not.toEqual(jasmine.any(Error));
expect(action.error).toEqual(false);
});
it('should return an action error if Payload = Error', () => {
const actionCreator = createAction(type);
const action = actionCreator(new Error('hello world'));
expect(action).toEqual(jasmine.any(Object));
expect(action.payload).toEqual(jasmine.any(Error));
expect(action.error).toEqual(true);
});
});
describe('action', () => {
const actionCreator = createAction(type);
const macro = (prop, val, Type) => () => {
expect(actionCreator(payload, meta))
.toHaveProperty(prop, jasmine.any(Type));
expect(actionCreator(payload, meta))
.toHaveProperty(prop, val);
};
it('should have property [payload = payload]', macro('payload', payload, Object));
it('should have property [meta = meta]', macro('meta', meta, Object));
it('should have property [error = error]', macro('error', false, Boolean));
it('should have property [type = type]', macro('type', type, String));
});
});