/
Replaceable.test.ts
184 lines (166 loc) · 5.24 KB
/
Replaceable.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Replaceable from '../Replaceable';
describe('Replaceable', () => {
describe('constructor', () => {
test('init with object', () => {
const replaceable = new Replaceable({a: 1, b: 2});
expect(replaceable.object).toEqual({a: 1, b: 2});
expect(replaceable.type).toBe('object');
});
test('init with array', () => {
const replaceable = new Replaceable([1, 2, 3]);
expect(replaceable.object).toEqual([1, 2, 3]);
expect(replaceable.type).toBe('array');
});
test('init with Map', () => {
const replaceable = new Replaceable(
new Map([
['a', 1],
['b', 2],
]),
);
expect(replaceable.object).toEqual(
new Map([
['a', 1],
['b', 2],
]),
);
expect(replaceable.type).toBe('map');
});
test('init with other type should throw error', () => {
expect(() => {
//eslint-disable-next-line @typescript-eslint/no-unused-vars
const replaceable = new Replaceable(new Date());
}).toThrow('Type date is not support in Replaceable!');
});
});
describe('get', () => {
test('get object item', () => {
const replaceable = new Replaceable({a: 1, b: 2});
expect(replaceable.get('b')).toBe(2);
});
test('get array item', () => {
const replaceable = new Replaceable([1, 2, 3]);
expect(replaceable.get(1)).toBe(2);
});
test('get Map item', () => {
const replaceable = new Replaceable(
new Map([
['a', 1],
['b', 2],
]),
);
expect(replaceable.get('b')).toBe(2);
});
});
describe('set', () => {
test('set object item', () => {
const replaceable = new Replaceable({a: 1, b: 2});
replaceable.set('b', 3);
expect(replaceable.object).toEqual({a: 1, b: 3});
});
test('set array item', () => {
const replaceable = new Replaceable([1, 2, 3]);
replaceable.set(1, 3);
expect(replaceable.object).toEqual([1, 3, 3]);
});
test('set Map item', () => {
const replaceable = new Replaceable(
new Map([
['a', 1],
['b', 2],
]),
);
replaceable.set('b', 3);
expect(replaceable.object).toEqual(
new Map([
['a', 1],
['b', 3],
]),
);
});
});
describe('forEach', () => {
test('object forEach', () => {
const symbolKey = Symbol('jest');
const object = {a: 1, b: 2, [symbolKey]: 3};
const replaceable = new Replaceable(object);
const cb = jest.fn();
replaceable.forEach(cb);
expect(cb).toHaveBeenCalledTimes(3);
expect(cb.mock.calls[0]).toEqual([1, 'a', object]);
expect(cb.mock.calls[1]).toEqual([2, 'b', object]);
expect(cb.mock.calls[2]).toEqual([3, symbolKey, object]);
});
test('array forEach', () => {
const replaceable = new Replaceable([1, 2, 3]);
const cb = jest.fn();
replaceable.forEach(cb);
expect(cb).toHaveBeenCalledTimes(3);
expect(cb.mock.calls[0]).toEqual([1, 0, [1, 2, 3]]);
expect(cb.mock.calls[1]).toEqual([2, 1, [1, 2, 3]]);
expect(cb.mock.calls[2]).toEqual([3, 2, [1, 2, 3]]);
});
test('map forEach', () => {
const map = new Map([
['a', 1],
['b', 2],
]);
const replaceable = new Replaceable(map);
const cb = jest.fn();
replaceable.forEach(cb);
expect(cb).toHaveBeenCalledTimes(2);
expect(cb.mock.calls[0]).toEqual([1, 'a', map]);
expect(cb.mock.calls[1]).toEqual([2, 'b', map]);
});
test('forEach should ignore nonenumerable property', () => {
const symbolKey = Symbol('jest');
const symbolKey2 = Symbol('awesome');
const object = {a: 1, [symbolKey]: 3};
Object.defineProperty(object, 'b', {
configurable: true,
enumerable: false,
value: 2,
writable: true,
});
Object.defineProperty(object, symbolKey2, {
configurable: true,
enumerable: false,
value: 4,
writable: true,
});
const replaceable = new Replaceable(object);
const cb = jest.fn();
replaceable.forEach(cb);
expect(cb).toHaveBeenCalledTimes(2);
expect(cb.mock.calls[0]).toEqual([1, 'a', object]);
expect(cb.mock.calls[1]).toEqual([3, symbolKey, object]);
});
});
describe('isReplaceable', () => {
test('should return true if two object types equal and support', () => {
expect(Replaceable.isReplaceable({a: 1}, {b: 2})).toBe(true);
expect(Replaceable.isReplaceable([], [1, 2, 3])).toBe(true);
expect(
Replaceable.isReplaceable(
new Map(),
new Map([
['a', 1],
['b', 2],
]),
),
).toBe(true);
});
test('should return false if two object types not equal', () => {
expect(Replaceable.isReplaceable({a: 1}, [1, 2, 3])).toBe(false);
});
test('should return false if object types not support', () => {
expect(Replaceable.isReplaceable('foo', 'bar')).toBe(false);
});
});
});