-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.ts
112 lines (106 loc) · 3.65 KB
/
index.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
import { ActionStandard, prefixDefault, setPrefix } from './index'
// From https://github.com/reduxjs/redux/blob/master/src/utils/isPlainObject.js
const isPlainObject = (obj: any) => {
if (typeof obj !== 'object' || obj === null) return false
let proto = obj
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto)
}
return Object.getPrototypeOf(obj) === proto
}
// tslint:disable max-classes-per-file
describe(ActionStandard.name, () => {
describe('prefix', () => {
test('exists', () => {
class Test extends ActionStandard {}
const test = new Test()
expect(test.type.startsWith(prefixDefault)).toBe(true)
expect(isPlainObject(test)).toBe(true)
})
test('can be changed via subsclassing', () => {
const prefixNew = 'test'
class Test extends ActionStandard {
protected static readonly prefix = prefixNew
}
const test = new Test()
expect(test.type.startsWith(prefixNew)).toBe(true)
expect(isPlainObject(test)).toBe(true)
})
test('can be changed via setPrefix', () => {
const prefixNew = 'test'
setPrefix(prefixNew)
class Test extends ActionStandard {}
const test = new Test()
expect(test.type.startsWith(prefixNew)).toBe(true)
expect(isPlainObject(test)).toBe(true)
})
test('can be inherited', () => {
const prefixNew = 'test'
class TestParent extends ActionStandard {
protected static readonly prefix = prefixNew
}
class TestChild extends TestParent {}
const test = new TestChild()
expect(test.type.startsWith(prefixNew)).toBe(true)
expect(isPlainObject(test)).toBe(true)
})
})
describe('type', () => {
test("ends with class' name", () => {
class Test extends ActionStandard {}
const test = new Test()
expect(Test.type.endsWith('Test')).toBe(true)
expect(test.type.endsWith('Test')).toBe(true)
expect(isPlainObject(test)).toBe(true)
})
test("is prefix with class' name", () => {
class Test extends ActionStandard {}
const test = new Test()
expect(Test.type).toBe(`${prefixDefault}Test`)
expect(test.type).toBe(`${prefixDefault}Test`)
expect(isPlainObject(test)).toBe(true)
})
})
describe('params', () => {
test('no payload and no meta', () => {
class Test extends ActionStandard {}
const test = new Test()
expect(test.payload).toBe(undefined)
expect(test.meta).toBe(undefined)
expect(test.error).toBe(false)
})
test('payload and no meta', () => {
const payload = 42
class Test extends ActionStandard<number> {}
const test = new Test(payload)
expect(test.payload).toBe(payload)
expect(test.meta).toBe(undefined)
expect(test.error).toBe(false)
})
test('payload and meta', () => {
const payload = 42
const meta = 'test'
class Test extends ActionStandard<number, string> {}
const test = new Test(payload, meta)
expect(test.payload).toBe(payload)
expect(test.meta).toBe(meta)
expect(test.error).toBe(false)
})
test('no payload and meta', () => {
const meta = 'test'
class Test extends ActionStandard<undefined, string> {}
const test = new Test(undefined, meta)
expect(test.payload).toBe(undefined)
expect(test.meta).toBe(meta)
expect(test.error).toBe(false)
})
test('error payload', () => {
const payload = new Error()
class Test extends ActionStandard<Error> {}
const test = new Test(payload)
expect(test.payload).toBe(payload)
expect(test.meta).toBe(undefined)
expect(test.error).toBe(true)
})
})
})