@ray-js/lamp-bead-strip 1.1.1-beta-1 → 1.1.1-beta-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/components/index.rjs.test.js +372 -0
- package/lib/components/mode/mode.test.js +550 -0
- package/lib/components/utils.test.js +197 -0
- package/lib/index.test.d.ts +5 -0
- package/lib/index.test.js +166 -0
- package/package.json +19 -3
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview index.rjs 测试用例 - 测试 Render 类的功能
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/// <reference types="jest" />
|
|
6
|
+
|
|
7
|
+
// Mock dependencies
|
|
8
|
+
jest.mock('./utils', () => ({
|
|
9
|
+
topList: [0, 2.81, 2.29, 1.76, 1.2, 0.63, 0.06, -0.52, -1.12, -1.72, -2.1, -2.04, -1.58, -0.96, -0.37, 0.2, 0.78, 1.34, 1.9, 2.45]
|
|
10
|
+
}));
|
|
11
|
+
jest.mock('./mode/index', () => ({
|
|
12
|
+
1: jest.fn(() => ({
|
|
13
|
+
colorList: [['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], ['rgb(0, 255, 0)', 'rgb(0, 0, 255)']],
|
|
14
|
+
length: 2
|
|
15
|
+
}))
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
// Mock getCanvasById
|
|
19
|
+
global.getCanvasById = jest.fn(() => {
|
|
20
|
+
const mockCanvas = {
|
|
21
|
+
width: 570,
|
|
22
|
+
height: 48,
|
|
23
|
+
clientWidth: 570,
|
|
24
|
+
clientHeight: 48,
|
|
25
|
+
getContext: jest.fn(type => {
|
|
26
|
+
if (type === '2d') {
|
|
27
|
+
return {
|
|
28
|
+
canvas: mockCanvas,
|
|
29
|
+
scale: jest.fn(),
|
|
30
|
+
clearRect: jest.fn(),
|
|
31
|
+
beginPath: jest.fn(),
|
|
32
|
+
arc: jest.fn(),
|
|
33
|
+
fill: jest.fn(),
|
|
34
|
+
fillStyle: '',
|
|
35
|
+
globalCompositeOperation: 'source-over'
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
})
|
|
40
|
+
};
|
|
41
|
+
return Promise.resolve(mockCanvas);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Import after mocks
|
|
45
|
+
const RenderClass = require('./index.rjs').default;
|
|
46
|
+
describe('index.rjs Render Class', () => {
|
|
47
|
+
let renderInstance;
|
|
48
|
+
let mockComponent;
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
jest.clearAllMocks();
|
|
51
|
+
jest.useFakeTimers();
|
|
52
|
+
|
|
53
|
+
// Mock component instance
|
|
54
|
+
mockComponent = {
|
|
55
|
+
data: {
|
|
56
|
+
prop: {}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Create Render instance
|
|
61
|
+
renderInstance = new RenderClass(mockComponent);
|
|
62
|
+
});
|
|
63
|
+
afterEach(() => {
|
|
64
|
+
jest.useRealTimers();
|
|
65
|
+
if (renderInstance && renderInstance.timer) {
|
|
66
|
+
clearInterval(renderInstance.timer);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
describe('renderLight', () => {
|
|
70
|
+
test('应该在没有 canvasId 时返回并打印日志', async () => {
|
|
71
|
+
const consoleSpy = jest.spyOn(console, 'log').mockImplementation();
|
|
72
|
+
await renderInstance.renderLight({});
|
|
73
|
+
expect(consoleSpy).toHaveBeenCalledWith('canvasId 是必传项!');
|
|
74
|
+
consoleSpy.mockRestore();
|
|
75
|
+
});
|
|
76
|
+
test('应该初始化 canvas context', async () => {
|
|
77
|
+
await renderInstance.renderLight({
|
|
78
|
+
canvasId: 'test-123',
|
|
79
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'],
|
|
80
|
+
mode: 1,
|
|
81
|
+
ready: true,
|
|
82
|
+
speed: 50,
|
|
83
|
+
contentValue: {}
|
|
84
|
+
});
|
|
85
|
+
expect(global.getCanvasById).toHaveBeenCalledWith('ray-lamp-bead-strip-canvasId-test-123');
|
|
86
|
+
expect(renderInstance.ctx).toBeDefined();
|
|
87
|
+
expect(renderInstance.ctx.scale).toHaveBeenCalledWith(1, 1);
|
|
88
|
+
});
|
|
89
|
+
test('应该处理自定义动画列表', async () => {
|
|
90
|
+
await renderInstance.renderLight({
|
|
91
|
+
canvasId: 'test-123',
|
|
92
|
+
customAnimationList: [['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], ['rgb(0, 255, 0)', 'rgb(0, 0, 255)']],
|
|
93
|
+
customAnimationChangeTime: 200,
|
|
94
|
+
ready: true
|
|
95
|
+
});
|
|
96
|
+
expect(renderInstance.preCustom).toBeDefined();
|
|
97
|
+
expect(renderInstance.timer).toBeDefined();
|
|
98
|
+
});
|
|
99
|
+
test('应该调用 errorCheck 并处理错误情况', async () => {
|
|
100
|
+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
|
101
|
+
await renderInstance.renderLight({
|
|
102
|
+
canvasId: 'test-123',
|
|
103
|
+
colors: [],
|
|
104
|
+
mode: 1,
|
|
105
|
+
contentValue: {}
|
|
106
|
+
});
|
|
107
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith('未传入颜色列表!');
|
|
108
|
+
consoleErrorSpy.mockRestore();
|
|
109
|
+
});
|
|
110
|
+
test('应该验证颜色格式', async () => {
|
|
111
|
+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
|
112
|
+
await renderInstance.renderLight({
|
|
113
|
+
canvasId: 'test-123',
|
|
114
|
+
colors: ['invalid-color'],
|
|
115
|
+
mode: 1,
|
|
116
|
+
contentValue: {}
|
|
117
|
+
});
|
|
118
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith('颜色必须是rgb!');
|
|
119
|
+
consoleErrorSpy.mockRestore();
|
|
120
|
+
});
|
|
121
|
+
test('应该验证 speed 范围', async () => {
|
|
122
|
+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
|
123
|
+
await renderInstance.renderLight({
|
|
124
|
+
canvasId: 'test-123',
|
|
125
|
+
colors: ['rgb(255, 0, 0)'],
|
|
126
|
+
mode: 1,
|
|
127
|
+
speed: 150,
|
|
128
|
+
// 超出范围
|
|
129
|
+
contentValue: {}
|
|
130
|
+
});
|
|
131
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith('错误的speed值,其范围为1~100!');
|
|
132
|
+
consoleErrorSpy.mockRestore();
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
describe('startLight', () => {
|
|
136
|
+
test('应该在 init 为 true 时重置 offset', async () => {
|
|
137
|
+
await renderInstance.renderLight({
|
|
138
|
+
canvasId: 'test-123',
|
|
139
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'],
|
|
140
|
+
mode: 1,
|
|
141
|
+
ready: true,
|
|
142
|
+
speed: 50,
|
|
143
|
+
contentValue: {}
|
|
144
|
+
});
|
|
145
|
+
renderInstance.offset = 5;
|
|
146
|
+
renderInstance.startLight(true);
|
|
147
|
+
|
|
148
|
+
// startLight 中,如果 init 为 true,offset 会被重置为 0
|
|
149
|
+
// 然后 draw() 会被调用,将 offset 变为 1
|
|
150
|
+
// 所以最终 offset 应该是 1
|
|
151
|
+
expect(renderInstance.offset).toBe(1);
|
|
152
|
+
});
|
|
153
|
+
test('应该启动定时器', async () => {
|
|
154
|
+
await renderInstance.renderLight({
|
|
155
|
+
canvasId: 'test-123',
|
|
156
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'],
|
|
157
|
+
mode: 1,
|
|
158
|
+
ready: true,
|
|
159
|
+
speed: 50,
|
|
160
|
+
contentValue: {}
|
|
161
|
+
});
|
|
162
|
+
// Clear previous timer
|
|
163
|
+
await renderInstance.stopLight();
|
|
164
|
+
const setIntervalSpy = jest.spyOn(global, 'setInterval');
|
|
165
|
+
renderInstance.startLight(false);
|
|
166
|
+
expect(renderInstance.timer).toBeDefined();
|
|
167
|
+
expect(setIntervalSpy).toHaveBeenCalled();
|
|
168
|
+
setIntervalSpy.mockRestore();
|
|
169
|
+
});
|
|
170
|
+
test('应该在 ready 为 false 时不启动定时器', async () => {
|
|
171
|
+
await renderInstance.renderLight({
|
|
172
|
+
canvasId: 'test-123',
|
|
173
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'],
|
|
174
|
+
mode: 1,
|
|
175
|
+
ready: false,
|
|
176
|
+
speed: 50,
|
|
177
|
+
contentValue: {}
|
|
178
|
+
});
|
|
179
|
+
renderInstance.startLight(false);
|
|
180
|
+
expect(renderInstance.timer).toBeNull();
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
describe('stopLight', () => {
|
|
184
|
+
test('应该清除定时器', async () => {
|
|
185
|
+
await renderInstance.renderLight({
|
|
186
|
+
canvasId: 'test-123',
|
|
187
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'],
|
|
188
|
+
mode: 1,
|
|
189
|
+
ready: true,
|
|
190
|
+
speed: 50,
|
|
191
|
+
contentValue: {}
|
|
192
|
+
});
|
|
193
|
+
renderInstance.startLight(false);
|
|
194
|
+
expect(renderInstance.timer).toBeDefined();
|
|
195
|
+
await renderInstance.stopLight();
|
|
196
|
+
expect(renderInstance.timer).toBeNull();
|
|
197
|
+
});
|
|
198
|
+
test('应该在定时器不存在时不报错', async () => {
|
|
199
|
+
await expect(renderInstance.stopLight()).resolves.not.toThrow();
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
describe('draw', () => {
|
|
203
|
+
test('应该调用 drawPoint 并更新 offset', async () => {
|
|
204
|
+
await renderInstance.renderLight({
|
|
205
|
+
canvasId: 'test-123',
|
|
206
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'],
|
|
207
|
+
mode: 1,
|
|
208
|
+
ready: true,
|
|
209
|
+
speed: 50,
|
|
210
|
+
contentValue: {}
|
|
211
|
+
});
|
|
212
|
+
renderInstance.offset = 0;
|
|
213
|
+
renderInstance.draw();
|
|
214
|
+
expect(renderInstance.ctx.beginPath).toHaveBeenCalled();
|
|
215
|
+
expect(renderInstance.offset).toBe(1);
|
|
216
|
+
});
|
|
217
|
+
test('应该在 offset 达到 length 时重置为 0', async () => {
|
|
218
|
+
await renderInstance.renderLight({
|
|
219
|
+
canvasId: 'test-123',
|
|
220
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'],
|
|
221
|
+
mode: 1,
|
|
222
|
+
ready: true,
|
|
223
|
+
speed: 50,
|
|
224
|
+
contentValue: {}
|
|
225
|
+
});
|
|
226
|
+
renderInstance.offset = 1; // length is 2
|
|
227
|
+
renderInstance.draw();
|
|
228
|
+
expect(renderInstance.offset).toBe(0);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
describe('drawPoint', () => {
|
|
232
|
+
test('应该在 colorList 为空时返回', async () => {
|
|
233
|
+
await renderInstance.renderLight({
|
|
234
|
+
canvasId: 'test-123',
|
|
235
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'],
|
|
236
|
+
mode: 1,
|
|
237
|
+
ready: true,
|
|
238
|
+
speed: 50,
|
|
239
|
+
contentValue: {}
|
|
240
|
+
});
|
|
241
|
+
// Clear previous calls
|
|
242
|
+
renderInstance.ctx.clearRect.mockClear();
|
|
243
|
+
renderInstance.drawPoint(null);
|
|
244
|
+
expect(renderInstance.ctx.clearRect).not.toHaveBeenCalled();
|
|
245
|
+
});
|
|
246
|
+
test('应该绘制所有颜色点', async () => {
|
|
247
|
+
await renderInstance.renderLight({
|
|
248
|
+
canvasId: 'test-123',
|
|
249
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'],
|
|
250
|
+
mode: 1,
|
|
251
|
+
ready: true,
|
|
252
|
+
speed: 50,
|
|
253
|
+
contentValue: {}
|
|
254
|
+
});
|
|
255
|
+
// Clear previous calls from renderLight
|
|
256
|
+
renderInstance.ctx.beginPath.mockClear();
|
|
257
|
+
renderInstance.ctx.arc.mockClear();
|
|
258
|
+
renderInstance.ctx.fill.mockClear();
|
|
259
|
+
renderInstance.ctx.clearRect.mockClear();
|
|
260
|
+
renderInstance.drawPoint(['rgb(255, 0, 0)', 'rgb(0, 255, 0)']);
|
|
261
|
+
expect(renderInstance.ctx.clearRect).toHaveBeenCalled();
|
|
262
|
+
expect(renderInstance.ctx.beginPath).toHaveBeenCalledTimes(2);
|
|
263
|
+
expect(renderInstance.ctx.arc).toHaveBeenCalledTimes(2);
|
|
264
|
+
expect(renderInstance.ctx.fill).toHaveBeenCalledTimes(2);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
describe('checkCustomAnimation', () => {
|
|
268
|
+
test('应该处理自定义动画', async () => {
|
|
269
|
+
await renderInstance.renderLight({
|
|
270
|
+
canvasId: 'test-123',
|
|
271
|
+
customAnimationList: [['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], ['rgb(0, 255, 0)', 'rgb(0, 0, 255)']],
|
|
272
|
+
customAnimationChangeTime: 200,
|
|
273
|
+
ready: true
|
|
274
|
+
});
|
|
275
|
+
expect(renderInstance.preCustom).toBeDefined();
|
|
276
|
+
expect(renderInstance.timer).toBeDefined();
|
|
277
|
+
});
|
|
278
|
+
test('应该在内容相同时不重置 offset', async () => {
|
|
279
|
+
const props = {
|
|
280
|
+
canvasId: 'test-123',
|
|
281
|
+
customAnimationList: [['rgb(255, 0, 0)', 'rgb(0, 255, 0)']],
|
|
282
|
+
customAnimationChangeTime: 200,
|
|
283
|
+
ready: true
|
|
284
|
+
};
|
|
285
|
+
await renderInstance.renderLight(props);
|
|
286
|
+
const oldOffset = renderInstance.offset;
|
|
287
|
+
|
|
288
|
+
// 再次调用相同的内容
|
|
289
|
+
await renderInstance.renderLight(props);
|
|
290
|
+
|
|
291
|
+
// offset 不应该被重置(因为内容相同)
|
|
292
|
+
expect(renderInstance.offset).toBe(oldOffset);
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
describe('drawCustom', () => {
|
|
296
|
+
test('应该绘制自定义动画并更新 offset', async () => {
|
|
297
|
+
const props = {
|
|
298
|
+
canvasId: 'test-123',
|
|
299
|
+
customAnimationList: [['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], ['rgb(0, 255, 0)', 'rgb(0, 0, 255)']],
|
|
300
|
+
customAnimationChangeTime: 200,
|
|
301
|
+
ready: true
|
|
302
|
+
};
|
|
303
|
+
await renderInstance.renderLight(props);
|
|
304
|
+
renderInstance.offset = 0;
|
|
305
|
+
renderInstance.drawCustom(props.customAnimationList);
|
|
306
|
+
expect(renderInstance.ctx.beginPath).toHaveBeenCalled();
|
|
307
|
+
expect(renderInstance.offset).toBe(1);
|
|
308
|
+
});
|
|
309
|
+
test('应该在 offset 达到数组长度时重置为 0', async () => {
|
|
310
|
+
const props = {
|
|
311
|
+
canvasId: 'test-123',
|
|
312
|
+
customAnimationList: [['rgb(255, 0, 0)']],
|
|
313
|
+
customAnimationChangeTime: 200,
|
|
314
|
+
ready: true
|
|
315
|
+
};
|
|
316
|
+
await renderInstance.renderLight(props);
|
|
317
|
+
renderInstance.offset = 0;
|
|
318
|
+
renderInstance.drawCustom(props.customAnimationList);
|
|
319
|
+
expect(renderInstance.offset).toBe(0); // 重置为 0
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
describe('errorCheck', () => {
|
|
323
|
+
test('应该检查 mode 是否存在', async () => {
|
|
324
|
+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
|
325
|
+
await renderInstance.renderLight({
|
|
326
|
+
canvasId: 'test-123',
|
|
327
|
+
colors: ['rgb(255, 0, 0)'],
|
|
328
|
+
mode: 999,
|
|
329
|
+
// 不存在的 mode
|
|
330
|
+
contentValue: {}
|
|
331
|
+
});
|
|
332
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith('错误的动画mode类型!');
|
|
333
|
+
consoleErrorSpy.mockRestore();
|
|
334
|
+
});
|
|
335
|
+
test('应该检查 colors 数组是否为空', async () => {
|
|
336
|
+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
|
337
|
+
await renderInstance.renderLight({
|
|
338
|
+
canvasId: 'test-123',
|
|
339
|
+
colors: [],
|
|
340
|
+
mode: 1,
|
|
341
|
+
contentValue: {}
|
|
342
|
+
});
|
|
343
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith('未传入颜色列表!');
|
|
344
|
+
consoleErrorSpy.mockRestore();
|
|
345
|
+
});
|
|
346
|
+
test('应该检查颜色格式', async () => {
|
|
347
|
+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
|
348
|
+
await renderInstance.renderLight({
|
|
349
|
+
canvasId: 'test-123',
|
|
350
|
+
colors: ['#ff0000'],
|
|
351
|
+
// 不是 rgb 格式
|
|
352
|
+
mode: 1,
|
|
353
|
+
contentValue: {}
|
|
354
|
+
});
|
|
355
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith('颜色必须是rgb!');
|
|
356
|
+
consoleErrorSpy.mockRestore();
|
|
357
|
+
});
|
|
358
|
+
test('应该检查 speed 范围', async () => {
|
|
359
|
+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
|
360
|
+
await renderInstance.renderLight({
|
|
361
|
+
canvasId: 'test-123',
|
|
362
|
+
colors: ['rgb(255, 0, 0)'],
|
|
363
|
+
mode: 1,
|
|
364
|
+
speed: 0,
|
|
365
|
+
// 小于 1
|
|
366
|
+
contentValue: {}
|
|
367
|
+
});
|
|
368
|
+
expect(consoleErrorSpy).toHaveBeenCalledWith('错误的speed值,其范围为1~100!');
|
|
369
|
+
consoleErrorSpy.mockRestore();
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
});
|
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview mode 模块测试用例
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import modeOption from './index';
|
|
6
|
+
import mode1 from './mode1';
|
|
7
|
+
import mode2 from './mode2';
|
|
8
|
+
import mode3 from './mode3';
|
|
9
|
+
import mode4 from './mode4';
|
|
10
|
+
import mode5 from './mode5';
|
|
11
|
+
import mode6 from './mode6';
|
|
12
|
+
import mode7 from './mode7';
|
|
13
|
+
import mode8 from './mode8';
|
|
14
|
+
import mode9 from './mode9';
|
|
15
|
+
import mode10 from './mode10';
|
|
16
|
+
import mode11 from './mode11';
|
|
17
|
+
import mode12 from './mode12';
|
|
18
|
+
import mode13 from './mode13';
|
|
19
|
+
import mode14 from './mode14';
|
|
20
|
+
import mode15 from './mode15';
|
|
21
|
+
import mode16 from './mode16';
|
|
22
|
+
describe('mode 模块测试', () => {
|
|
23
|
+
const defaultColors = ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'];
|
|
24
|
+
const defaultCloseRgb = 'rgb(58,58,58)';
|
|
25
|
+
test('应该导出所有模式', () => {
|
|
26
|
+
expect(modeOption).toBeDefined();
|
|
27
|
+
expect(modeOption[1]).toBeDefined();
|
|
28
|
+
expect(modeOption[2]).toBeDefined();
|
|
29
|
+
expect(modeOption[16]).toBeDefined();
|
|
30
|
+
});
|
|
31
|
+
test('mode1 应该是一个函数', () => {
|
|
32
|
+
expect(typeof mode1).toBe('function');
|
|
33
|
+
});
|
|
34
|
+
describe('mode1 - 渐变模式', () => {
|
|
35
|
+
test('应该生成渐变颜色列表', () => {
|
|
36
|
+
const result = mode1(['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], {}, defaultCloseRgb);
|
|
37
|
+
expect(result).toHaveProperty('colorList');
|
|
38
|
+
expect(result).toHaveProperty('length');
|
|
39
|
+
expect(Array.isArray(result.colorList)).toBe(true);
|
|
40
|
+
expect(result.length).toBeGreaterThan(0);
|
|
41
|
+
});
|
|
42
|
+
test('segmented 为 false 时应该返回正确的结构', () => {
|
|
43
|
+
const result = mode1(['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], {
|
|
44
|
+
segmented: 0
|
|
45
|
+
}, defaultCloseRgb);
|
|
46
|
+
expect(result.colorList).toBeDefined();
|
|
47
|
+
expect(result.length).toBeGreaterThan(0);
|
|
48
|
+
if (result.colorList.length > 0) {
|
|
49
|
+
expect(Array.isArray(result.colorList[0])).toBe(true);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
test('segmented 为 true 时应该返回分段结构', () => {
|
|
53
|
+
const result = mode1(['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], {
|
|
54
|
+
segmented: 1
|
|
55
|
+
}, defaultCloseRgb);
|
|
56
|
+
expect(result.colorList).toBeDefined();
|
|
57
|
+
expect(result.length).toBeGreaterThan(0);
|
|
58
|
+
if (result.colorList.length > 0) {
|
|
59
|
+
expect(Array.isArray(result.colorList[0])).toBe(true);
|
|
60
|
+
expect(result.colorList[0].length).toBe(20);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
describe('mode2 - 跳变模式', () => {
|
|
65
|
+
test('segmented 为 false 时应该返回正确的结构', () => {
|
|
66
|
+
const result = mode2(defaultColors, {
|
|
67
|
+
segmented: 0
|
|
68
|
+
}, defaultCloseRgb);
|
|
69
|
+
expect(result).toHaveProperty('colorList');
|
|
70
|
+
expect(result).toHaveProperty('length');
|
|
71
|
+
expect(result.length).toBeGreaterThan(0);
|
|
72
|
+
});
|
|
73
|
+
test('segmented 为 true 时应该返回分段结构', () => {
|
|
74
|
+
const result = mode2(defaultColors, {
|
|
75
|
+
segmented: 1
|
|
76
|
+
}, defaultCloseRgb);
|
|
77
|
+
expect(result).toHaveProperty('colorList');
|
|
78
|
+
expect(result).toHaveProperty('length');
|
|
79
|
+
});
|
|
80
|
+
test('应该处理不同长度的颜色数组', () => {
|
|
81
|
+
const testCases = [{
|
|
82
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)']
|
|
83
|
+
}, {
|
|
84
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)']
|
|
85
|
+
}, {
|
|
86
|
+
colors: Array(4).fill('rgb(255, 0, 0)')
|
|
87
|
+
}, {
|
|
88
|
+
colors: Array(5).fill('rgb(255, 0, 0)')
|
|
89
|
+
}, {
|
|
90
|
+
colors: Array(6).fill('rgb(255, 0, 0)')
|
|
91
|
+
}, {
|
|
92
|
+
colors: Array(7).fill('rgb(255, 0, 0)')
|
|
93
|
+
}, {
|
|
94
|
+
colors: Array(8).fill('rgb(255, 0, 0)')
|
|
95
|
+
}];
|
|
96
|
+
testCases.forEach(_ref => {
|
|
97
|
+
let {
|
|
98
|
+
colors
|
|
99
|
+
} = _ref;
|
|
100
|
+
const result = mode2(colors, {
|
|
101
|
+
segmented: 1
|
|
102
|
+
}, defaultCloseRgb);
|
|
103
|
+
expect(result).toHaveProperty('colorList');
|
|
104
|
+
expect(result).toHaveProperty('length');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
describe('mode3 - 呼吸模式', () => {
|
|
109
|
+
test('segmented 为 false 时应该返回正确的结构', () => {
|
|
110
|
+
const result = mode3(defaultColors, {
|
|
111
|
+
segmented: 0
|
|
112
|
+
}, defaultCloseRgb);
|
|
113
|
+
expect(result).toHaveProperty('colorList');
|
|
114
|
+
expect(result).toHaveProperty('length');
|
|
115
|
+
expect(result.length).toBeGreaterThan(0);
|
|
116
|
+
});
|
|
117
|
+
test('segmented 为 true 时应该返回分段结构', () => {
|
|
118
|
+
const result = mode3(defaultColors, {
|
|
119
|
+
segmented: 1
|
|
120
|
+
}, defaultCloseRgb);
|
|
121
|
+
expect(result).toHaveProperty('colorList');
|
|
122
|
+
expect(result).toHaveProperty('length');
|
|
123
|
+
if (result.colorList.length > 0) {
|
|
124
|
+
expect(result.colorList[0].length).toBe(20);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
describe('mode4 - 闪烁模式', () => {
|
|
129
|
+
test('segmented 为 false 时应该返回正确的结构', () => {
|
|
130
|
+
const result = mode4(defaultColors, {
|
|
131
|
+
segmented: 0
|
|
132
|
+
}, defaultCloseRgb);
|
|
133
|
+
expect(result).toHaveProperty('colorList');
|
|
134
|
+
expect(result).toHaveProperty('length');
|
|
135
|
+
expect(result.length).toBeGreaterThan(0);
|
|
136
|
+
});
|
|
137
|
+
test('segmented 为 true 时应该返回分段结构', () => {
|
|
138
|
+
const result = mode4(defaultColors, {
|
|
139
|
+
segmented: 1
|
|
140
|
+
}, defaultCloseRgb);
|
|
141
|
+
expect(result).toHaveProperty('colorList');
|
|
142
|
+
expect(result).toHaveProperty('length');
|
|
143
|
+
});
|
|
144
|
+
test('应该处理不同长度的颜色数组', () => {
|
|
145
|
+
const result = mode4(['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)'], {
|
|
146
|
+
segmented: 1
|
|
147
|
+
}, defaultCloseRgb);
|
|
148
|
+
expect(result).toHaveProperty('colorList');
|
|
149
|
+
expect(result).toHaveProperty('length');
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
describe('mode5 - 流星模式', () => {
|
|
153
|
+
test('expand 为 undefined 时应该返回流星效果', () => {
|
|
154
|
+
const result = mode5(defaultColors, {}, defaultCloseRgb);
|
|
155
|
+
expect(result).toHaveProperty('colorList');
|
|
156
|
+
expect(result).toHaveProperty('length');
|
|
157
|
+
expect(result.length).toBeGreaterThan(0);
|
|
158
|
+
});
|
|
159
|
+
test('expand 为 1 时应该返回流星雨效果', () => {
|
|
160
|
+
const result = mode5(defaultColors, {
|
|
161
|
+
expand: 1
|
|
162
|
+
}, defaultCloseRgb);
|
|
163
|
+
expect(result).toHaveProperty('colorList');
|
|
164
|
+
expect(result).toHaveProperty('length');
|
|
165
|
+
});
|
|
166
|
+
test('expand 为 2 时应该返回幻彩流星效果', () => {
|
|
167
|
+
const result = mode5(['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)'], {
|
|
168
|
+
expand: 2
|
|
169
|
+
}, defaultCloseRgb);
|
|
170
|
+
expect(result).toHaveProperty('colorList');
|
|
171
|
+
expect(result).toHaveProperty('length');
|
|
172
|
+
});
|
|
173
|
+
test('应该处理 direction 参数', () => {
|
|
174
|
+
const result1 = mode5(defaultColors, {
|
|
175
|
+
direction: 0
|
|
176
|
+
}, defaultCloseRgb);
|
|
177
|
+
const result2 = mode5(defaultColors, {
|
|
178
|
+
direction: 1
|
|
179
|
+
}, defaultCloseRgb);
|
|
180
|
+
expect(result1).toHaveProperty('colorList');
|
|
181
|
+
expect(result2).toHaveProperty('colorList');
|
|
182
|
+
// direction 应该影响列表顺序
|
|
183
|
+
if (result1.colorList.length > 0 && result2.colorList.length > 0) {
|
|
184
|
+
expect(result1.colorList[0]).not.toEqual(result2.colorList[0]);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
describe('mode8 - 追光模式', () => {
|
|
189
|
+
test('应该返回正确的结构', () => {
|
|
190
|
+
const result = mode8(defaultColors, {}, defaultCloseRgb);
|
|
191
|
+
expect(result).toHaveProperty('colorList');
|
|
192
|
+
expect(result).toHaveProperty('length');
|
|
193
|
+
expect(result.length).toBeGreaterThan(0);
|
|
194
|
+
});
|
|
195
|
+
test('direction 参数应该影响列表顺序', () => {
|
|
196
|
+
const colors = ['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)'];
|
|
197
|
+
const result1 = mode8(colors, {
|
|
198
|
+
direction: 0
|
|
199
|
+
}, defaultCloseRgb);
|
|
200
|
+
const result2 = mode8(colors, {
|
|
201
|
+
direction: 1
|
|
202
|
+
}, defaultCloseRgb);
|
|
203
|
+
expect(result1).toHaveProperty('colorList');
|
|
204
|
+
expect(result2).toHaveProperty('colorList');
|
|
205
|
+
// direction 应该影响列表顺序,检查最后一个元素
|
|
206
|
+
if (result1.colorList.length > 1 && result2.colorList.length > 1) {
|
|
207
|
+
const last1 = result1.colorList[result1.colorList.length - 1];
|
|
208
|
+
const last2 = result2.colorList[result2.colorList.length - 1];
|
|
209
|
+
// 如果 direction 不同,最后一个元素应该不同(除非是循环的)
|
|
210
|
+
expect(Array.isArray(last1)).toBe(true);
|
|
211
|
+
expect(Array.isArray(last2)).toBe(true);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
describe('mode6 - 堆积模式', () => {
|
|
216
|
+
test('segmented 为 false 时应该返回正确的结构', () => {
|
|
217
|
+
const result = mode6(defaultColors, {
|
|
218
|
+
segmented: 0
|
|
219
|
+
}, defaultCloseRgb);
|
|
220
|
+
expect(result).toHaveProperty('colorList');
|
|
221
|
+
expect(result).toHaveProperty('length');
|
|
222
|
+
expect(result.length).toBeGreaterThan(0);
|
|
223
|
+
});
|
|
224
|
+
test('segmented 为 true 时应该返回分段结构', () => {
|
|
225
|
+
const result = mode6(defaultColors, {
|
|
226
|
+
segmented: 1
|
|
227
|
+
}, defaultCloseRgb);
|
|
228
|
+
expect(result).toHaveProperty('colorList');
|
|
229
|
+
expect(result).toHaveProperty('length');
|
|
230
|
+
});
|
|
231
|
+
test('应该处理 direction 参数', () => {
|
|
232
|
+
const result1 = mode6(defaultColors, {
|
|
233
|
+
direction: 0
|
|
234
|
+
}, defaultCloseRgb);
|
|
235
|
+
const result2 = mode6(defaultColors, {
|
|
236
|
+
direction: 1
|
|
237
|
+
}, defaultCloseRgb);
|
|
238
|
+
expect(result1).toHaveProperty('colorList');
|
|
239
|
+
expect(result2).toHaveProperty('colorList');
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
describe('mode7 - 飘落模式', () => {
|
|
243
|
+
test('segmented 为 false 时应该返回正确的结构', () => {
|
|
244
|
+
const result = mode7(defaultColors, {
|
|
245
|
+
segmented: 0
|
|
246
|
+
}, defaultCloseRgb);
|
|
247
|
+
expect(result).toHaveProperty('colorList');
|
|
248
|
+
expect(result).toHaveProperty('length');
|
|
249
|
+
expect(result.length).toBeGreaterThan(0);
|
|
250
|
+
});
|
|
251
|
+
test('segmented 为 true 时应该返回分段结构', () => {
|
|
252
|
+
const result = mode7(defaultColors, {
|
|
253
|
+
segmented: 1
|
|
254
|
+
}, defaultCloseRgb);
|
|
255
|
+
expect(result).toHaveProperty('colorList');
|
|
256
|
+
expect(result).toHaveProperty('length');
|
|
257
|
+
});
|
|
258
|
+
test('应该处理 direction 参数', () => {
|
|
259
|
+
const result1 = mode7(defaultColors, {
|
|
260
|
+
direction: 0
|
|
261
|
+
}, defaultCloseRgb);
|
|
262
|
+
const result2 = mode7(defaultColors, {
|
|
263
|
+
direction: 1
|
|
264
|
+
}, defaultCloseRgb);
|
|
265
|
+
expect(result1).toHaveProperty('colorList');
|
|
266
|
+
expect(result2).toHaveProperty('colorList');
|
|
267
|
+
});
|
|
268
|
+
test('应该处理长度为5的颜色数组', () => {
|
|
269
|
+
const colors = Array(5).fill('rgb(255, 0, 0)');
|
|
270
|
+
const result = mode7(colors, {
|
|
271
|
+
segmented: 1
|
|
272
|
+
}, defaultCloseRgb);
|
|
273
|
+
expect(result).toHaveProperty('colorList');
|
|
274
|
+
expect(result).toHaveProperty('length');
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
describe('mode9 - 飘动模式', () => {
|
|
278
|
+
test('应该返回正确的结构', () => {
|
|
279
|
+
const result = mode9(defaultColors, {}, defaultCloseRgb);
|
|
280
|
+
expect(result).toHaveProperty('colorList');
|
|
281
|
+
expect(result).toHaveProperty('length');
|
|
282
|
+
expect(result.length).toBeGreaterThan(0);
|
|
283
|
+
});
|
|
284
|
+
test('应该处理 direction 参数', () => {
|
|
285
|
+
const result1 = mode9(defaultColors, {
|
|
286
|
+
direction: 0
|
|
287
|
+
}, defaultCloseRgb);
|
|
288
|
+
const result2 = mode9(defaultColors, {
|
|
289
|
+
direction: 1
|
|
290
|
+
}, defaultCloseRgb);
|
|
291
|
+
expect(result1).toHaveProperty('colorList');
|
|
292
|
+
expect(result2).toHaveProperty('colorList');
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
describe('mode10 - 流水模式', () => {
|
|
296
|
+
test('应该返回正确的结构', () => {
|
|
297
|
+
const result = mode10(defaultColors, {}, defaultCloseRgb);
|
|
298
|
+
expect(result).toHaveProperty('colorList');
|
|
299
|
+
expect(result).toHaveProperty('length');
|
|
300
|
+
expect(result.length).toBeGreaterThan(0);
|
|
301
|
+
});
|
|
302
|
+
test('应该处理不同长度的颜色数组', () => {
|
|
303
|
+
const testCases = [{
|
|
304
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)']
|
|
305
|
+
}, {
|
|
306
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)']
|
|
307
|
+
}, {
|
|
308
|
+
colors: Array(4).fill('rgb(255, 0, 0)')
|
|
309
|
+
}, {
|
|
310
|
+
colors: Array(5).fill('rgb(255, 0, 0)')
|
|
311
|
+
}, {
|
|
312
|
+
colors: Array(6).fill('rgb(255, 0, 0)')
|
|
313
|
+
}, {
|
|
314
|
+
colors: Array(7).fill('rgb(255, 0, 0)')
|
|
315
|
+
}, {
|
|
316
|
+
colors: Array(8).fill('rgb(255, 0, 0)')
|
|
317
|
+
}];
|
|
318
|
+
testCases.forEach(_ref2 => {
|
|
319
|
+
let {
|
|
320
|
+
colors
|
|
321
|
+
} = _ref2;
|
|
322
|
+
const result = mode10(colors, {}, defaultCloseRgb);
|
|
323
|
+
expect(result).toHaveProperty('colorList');
|
|
324
|
+
expect(result).toHaveProperty('length');
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
test('应该处理 direction 参数', () => {
|
|
328
|
+
const result1 = mode10(defaultColors, {
|
|
329
|
+
direction: 0
|
|
330
|
+
}, defaultCloseRgb);
|
|
331
|
+
const result2 = mode10(defaultColors, {
|
|
332
|
+
direction: 1
|
|
333
|
+
}, defaultCloseRgb);
|
|
334
|
+
expect(result1).toHaveProperty('colorList');
|
|
335
|
+
expect(result2).toHaveProperty('colorList');
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
describe('mode11 - 彩虹模式', () => {
|
|
339
|
+
test('应该返回正确的结构', () => {
|
|
340
|
+
const result = mode11(defaultColors, {}, defaultCloseRgb);
|
|
341
|
+
expect(result).toHaveProperty('colorList');
|
|
342
|
+
expect(result).toHaveProperty('length');
|
|
343
|
+
expect(result.length).toBeGreaterThan(0);
|
|
344
|
+
});
|
|
345
|
+
test('应该处理不同长度的颜色数组', () => {
|
|
346
|
+
const testCases = [{
|
|
347
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)']
|
|
348
|
+
}, {
|
|
349
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)']
|
|
350
|
+
}, {
|
|
351
|
+
colors: Array(4).fill('rgb(255, 0, 0)')
|
|
352
|
+
}, {
|
|
353
|
+
colors: Array(5).fill('rgb(255, 0, 0)')
|
|
354
|
+
}, {
|
|
355
|
+
colors: Array(6).fill('rgb(255, 0, 0)')
|
|
356
|
+
}, {
|
|
357
|
+
colors: Array(7).fill('rgb(255, 0, 0)')
|
|
358
|
+
}, {
|
|
359
|
+
colors: Array(8).fill('rgb(255, 0, 0)')
|
|
360
|
+
}];
|
|
361
|
+
testCases.forEach(_ref3 => {
|
|
362
|
+
let {
|
|
363
|
+
colors
|
|
364
|
+
} = _ref3;
|
|
365
|
+
const result = mode11(colors, {}, defaultCloseRgb);
|
|
366
|
+
expect(result).toHaveProperty('colorList');
|
|
367
|
+
expect(result).toHaveProperty('length');
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
test('应该处理 direction 参数', () => {
|
|
371
|
+
const result1 = mode11(defaultColors, {
|
|
372
|
+
direction: 0
|
|
373
|
+
}, defaultCloseRgb);
|
|
374
|
+
const result2 = mode11(defaultColors, {
|
|
375
|
+
direction: 1
|
|
376
|
+
}, defaultCloseRgb);
|
|
377
|
+
expect(result1).toHaveProperty('colorList');
|
|
378
|
+
expect(result2).toHaveProperty('colorList');
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
describe('mode12 - 闪现模式', () => {
|
|
382
|
+
test('segmented 为 false 时应该返回正确的结构', () => {
|
|
383
|
+
const result = mode12(defaultColors, {
|
|
384
|
+
segmented: 0
|
|
385
|
+
}, defaultCloseRgb);
|
|
386
|
+
expect(result).toHaveProperty('colorList');
|
|
387
|
+
expect(result).toHaveProperty('length');
|
|
388
|
+
expect(result.length).toBeGreaterThan(0);
|
|
389
|
+
});
|
|
390
|
+
test('segmented 为 true 时应该返回分段结构', () => {
|
|
391
|
+
const result = mode12(defaultColors, {
|
|
392
|
+
segmented: 1
|
|
393
|
+
}, defaultCloseRgb);
|
|
394
|
+
expect(result).toHaveProperty('colorList');
|
|
395
|
+
expect(result).toHaveProperty('length');
|
|
396
|
+
});
|
|
397
|
+
test('应该处理不同长度的颜色数组', () => {
|
|
398
|
+
const testCases = [{
|
|
399
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)']
|
|
400
|
+
}, {
|
|
401
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)']
|
|
402
|
+
}, {
|
|
403
|
+
colors: Array(4).fill('rgb(255, 0, 0)')
|
|
404
|
+
}, {
|
|
405
|
+
colors: Array(5).fill('rgb(255, 0, 0)')
|
|
406
|
+
}, {
|
|
407
|
+
colors: Array(6).fill('rgb(255, 0, 0)')
|
|
408
|
+
}, {
|
|
409
|
+
colors: Array(7).fill('rgb(255, 0, 0)')
|
|
410
|
+
}, {
|
|
411
|
+
colors: Array(8).fill('rgb(255, 0, 0)')
|
|
412
|
+
}];
|
|
413
|
+
testCases.forEach(_ref4 => {
|
|
414
|
+
let {
|
|
415
|
+
colors
|
|
416
|
+
} = _ref4;
|
|
417
|
+
const result1 = mode12(colors, {
|
|
418
|
+
segmented: 0
|
|
419
|
+
}, defaultCloseRgb);
|
|
420
|
+
const result2 = mode12(colors, {
|
|
421
|
+
segmented: 1
|
|
422
|
+
}, defaultCloseRgb);
|
|
423
|
+
expect(result1).toHaveProperty('colorList');
|
|
424
|
+
expect(result2).toHaveProperty('colorList');
|
|
425
|
+
});
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
describe('mode13 - 反弹模式', () => {
|
|
429
|
+
test('expand 为 undefined 时应该返回正确的结构', () => {
|
|
430
|
+
const result = mode13(defaultColors, {}, defaultCloseRgb);
|
|
431
|
+
expect(result).toHaveProperty('colorList');
|
|
432
|
+
expect(result).toHaveProperty('length');
|
|
433
|
+
expect(result.length).toBeGreaterThan(0);
|
|
434
|
+
});
|
|
435
|
+
test('expand 为 1 时应该返回渐变效果', () => {
|
|
436
|
+
const result = mode13(defaultColors, {
|
|
437
|
+
expand: 1
|
|
438
|
+
}, defaultCloseRgb);
|
|
439
|
+
expect(result).toHaveProperty('colorList');
|
|
440
|
+
expect(result).toHaveProperty('length');
|
|
441
|
+
});
|
|
442
|
+
test('应该处理奇数长度的颜色数组', () => {
|
|
443
|
+
const result = mode13(['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)'], {}, defaultCloseRgb);
|
|
444
|
+
expect(result).toHaveProperty('colorList');
|
|
445
|
+
expect(result).toHaveProperty('length');
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
describe('mode14 - 穿梭模式', () => {
|
|
449
|
+
test('应该返回正确的结构', () => {
|
|
450
|
+
const result = mode14(defaultColors, {}, defaultCloseRgb);
|
|
451
|
+
expect(result).toHaveProperty('colorList');
|
|
452
|
+
expect(result).toHaveProperty('length');
|
|
453
|
+
expect(result.length).toBeGreaterThan(0);
|
|
454
|
+
});
|
|
455
|
+
test('应该处理多个颜色', () => {
|
|
456
|
+
const result = mode14(['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)'], {}, defaultCloseRgb);
|
|
457
|
+
expect(result).toHaveProperty('colorList');
|
|
458
|
+
expect(result).toHaveProperty('length');
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
describe('mode15 - 乱闪模式', () => {
|
|
462
|
+
test('应该返回正确的结构', () => {
|
|
463
|
+
const result = mode15(defaultColors, {}, defaultCloseRgb);
|
|
464
|
+
expect(result).toHaveProperty('colorList');
|
|
465
|
+
expect(result).toHaveProperty('length');
|
|
466
|
+
expect(result.length).toBeGreaterThan(0);
|
|
467
|
+
});
|
|
468
|
+
test('应该处理多个颜色', () => {
|
|
469
|
+
const result = mode15(['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)'], {}, defaultCloseRgb);
|
|
470
|
+
expect(result).toHaveProperty('colorList');
|
|
471
|
+
expect(result).toHaveProperty('length');
|
|
472
|
+
});
|
|
473
|
+
});
|
|
474
|
+
describe('mode16 - 开合模式', () => {
|
|
475
|
+
test('expand 为 undefined 时应该返回正确的结构', () => {
|
|
476
|
+
const result = mode16(defaultColors, {}, defaultCloseRgb);
|
|
477
|
+
expect(result).toHaveProperty('colorList');
|
|
478
|
+
expect(result).toHaveProperty('length');
|
|
479
|
+
expect(result.length).toBeGreaterThan(0);
|
|
480
|
+
});
|
|
481
|
+
test('expand 为 1 时应该返回扩展效果', () => {
|
|
482
|
+
const result = mode16(defaultColors, {
|
|
483
|
+
expand: 1
|
|
484
|
+
}, defaultCloseRgb);
|
|
485
|
+
expect(result).toHaveProperty('colorList');
|
|
486
|
+
expect(result).toHaveProperty('length');
|
|
487
|
+
});
|
|
488
|
+
test('应该处理多个颜色', () => {
|
|
489
|
+
const result = mode16(['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)'], {}, defaultCloseRgb);
|
|
490
|
+
expect(result).toHaveProperty('colorList');
|
|
491
|
+
expect(result).toHaveProperty('length');
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
describe('modeOption 导出测试', () => {
|
|
495
|
+
test('应该包含所有16种模式', () => {
|
|
496
|
+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].forEach(mode => {
|
|
497
|
+
expect(modeOption[mode]).toBeDefined();
|
|
498
|
+
expect(typeof modeOption[mode]).toBe('function');
|
|
499
|
+
});
|
|
500
|
+
});
|
|
501
|
+
test('每个模式函数应该接受三个参数并返回正确结构', () => {
|
|
502
|
+
const colors = ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'];
|
|
503
|
+
const contentValue = {};
|
|
504
|
+
// 测试所有模式
|
|
505
|
+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16].forEach(mode => {
|
|
506
|
+
const result = modeOption[mode](colors, contentValue, 'rgb(58,58,58)');
|
|
507
|
+
expect(result).toHaveProperty('colorList');
|
|
508
|
+
expect(result).toHaveProperty('length');
|
|
509
|
+
expect(Array.isArray(result.colorList)).toBe(true);
|
|
510
|
+
expect(typeof result.length).toBe('number');
|
|
511
|
+
expect(result.length).toBeGreaterThan(0);
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
test('应该处理不同参数组合', () => {
|
|
515
|
+
const colors = ['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)'];
|
|
516
|
+
|
|
517
|
+
// 测试不同 contentValue 参数
|
|
518
|
+
const testCases = [{}, {
|
|
519
|
+
segmented: 0
|
|
520
|
+
}, {
|
|
521
|
+
segmented: 1
|
|
522
|
+
}, {
|
|
523
|
+
direction: 0
|
|
524
|
+
}, {
|
|
525
|
+
direction: 1
|
|
526
|
+
}, {
|
|
527
|
+
expand: 0
|
|
528
|
+
}, {
|
|
529
|
+
expand: 1
|
|
530
|
+
}, {
|
|
531
|
+
expand: 2
|
|
532
|
+
}, {
|
|
533
|
+
segmented: 1,
|
|
534
|
+
direction: 1
|
|
535
|
+
}, {
|
|
536
|
+
segmented: 1,
|
|
537
|
+
expand: 1
|
|
538
|
+
}];
|
|
539
|
+
|
|
540
|
+
// 测试几个主要模式
|
|
541
|
+
[1, 2, 3, 4, 5, 8, 10].forEach(mode => {
|
|
542
|
+
testCases.forEach(contentValue => {
|
|
543
|
+
const result = modeOption[mode](colors, contentValue, defaultCloseRgb);
|
|
544
|
+
expect(result).toHaveProperty('colorList');
|
|
545
|
+
expect(result).toHaveProperty('length');
|
|
546
|
+
});
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
});
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview utils 工具函数测试用例
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { generateGradientColors, topList, getListByIndex, splitNumber, arrayFill, splitLight, splitArray, averageRGBColors, findOverlapIndices } from './utils';
|
|
6
|
+
describe('utils 工具函数测试', () => {
|
|
7
|
+
describe('generateGradientColors', () => {
|
|
8
|
+
test('应该生成两个颜色之间的渐变色', () => {
|
|
9
|
+
const result = generateGradientColors(['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], 5);
|
|
10
|
+
expect(result).toHaveLength(5);
|
|
11
|
+
expect(result[0]).toBe('rgb(255, 0, 0)');
|
|
12
|
+
expect(result[4]).toBe('rgb(0, 255, 0)');
|
|
13
|
+
});
|
|
14
|
+
test('应该生成多个颜色之间的渐变色', () => {
|
|
15
|
+
const result = generateGradientColors(['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)'], 10);
|
|
16
|
+
expect(result).toHaveLength(10);
|
|
17
|
+
expect(result[0]).toBe('rgb(255, 0, 0)');
|
|
18
|
+
expect(result[9]).toBe('rgb(0, 0, 255)');
|
|
19
|
+
});
|
|
20
|
+
test('应该处理 rgba 格式的颜色', () => {
|
|
21
|
+
const result = generateGradientColors(['rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)'], 3);
|
|
22
|
+
expect(result).toHaveLength(3);
|
|
23
|
+
expect(result[0]).toContain('rgb(255, 0, 0)');
|
|
24
|
+
});
|
|
25
|
+
test('颜色数组长度小于2时应该抛出错误', () => {
|
|
26
|
+
expect(() => {
|
|
27
|
+
generateGradientColors(['rgb(255, 0, 0)'], 5);
|
|
28
|
+
}).toThrow('颜色数组长度必须为2至8之间');
|
|
29
|
+
});
|
|
30
|
+
test('颜色数组长度大于8时应该抛出错误', () => {
|
|
31
|
+
const colors = Array(9).fill('rgb(255, 0, 0)');
|
|
32
|
+
expect(() => {
|
|
33
|
+
generateGradientColors(colors, 5);
|
|
34
|
+
}).toThrow('颜色数组长度必须为2至8之间');
|
|
35
|
+
});
|
|
36
|
+
test('步骤数小于等于0时应该抛出错误', () => {
|
|
37
|
+
expect(() => {
|
|
38
|
+
generateGradientColors(['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], 0);
|
|
39
|
+
}).toThrow('步骤数必须大于0');
|
|
40
|
+
expect(() => {
|
|
41
|
+
generateGradientColors(['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], -1);
|
|
42
|
+
}).toThrow('步骤数必须大于0');
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
describe('getListByIndex', () => {
|
|
46
|
+
test('应该返回指定索引的值', () => {
|
|
47
|
+
const list = [1, 2, 3, 4, 5];
|
|
48
|
+
expect(getListByIndex(list, 0)).toBe(1);
|
|
49
|
+
expect(getListByIndex(list, 2)).toBe(3);
|
|
50
|
+
expect(getListByIndex(list, 4)).toBe(5);
|
|
51
|
+
});
|
|
52
|
+
test('索引超出范围时应该循环返回', () => {
|
|
53
|
+
const list = [1, 2, 3];
|
|
54
|
+
expect(getListByIndex(list, 3)).toBe(1); // 3 % 3 = 0, list[0] = 1
|
|
55
|
+
expect(getListByIndex(list, 5)).toBe(3); // 5 % 3 = 2, list[2] = 3
|
|
56
|
+
expect(getListByIndex(list, 10)).toBe(2); // 10 % 3 = 1, list[1] = 2
|
|
57
|
+
});
|
|
58
|
+
test('应该使用 topList 常量', () => {
|
|
59
|
+
expect(getListByIndex(topList, 0)).toBe(0);
|
|
60
|
+
expect(getListByIndex(topList, 1)).toBe(2.81);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
describe('splitNumber', () => {
|
|
64
|
+
test('应该将数字分割成指定数量的部分', () => {
|
|
65
|
+
expect(splitNumber(10, 2)).toEqual([5, 5]);
|
|
66
|
+
expect(splitNumber(10, 3)).toEqual([4, 3, 3]);
|
|
67
|
+
expect(splitNumber(20, 4)).toEqual([5, 5, 5, 5]);
|
|
68
|
+
});
|
|
69
|
+
test('有余数时应该均匀分配', () => {
|
|
70
|
+
expect(splitNumber(10, 3)).toEqual([4, 3, 3]);
|
|
71
|
+
expect(splitNumber(11, 3)).toEqual([4, 4, 3]);
|
|
72
|
+
expect(splitNumber(13, 4)).toEqual([4, 3, 3, 3]);
|
|
73
|
+
});
|
|
74
|
+
test('parts 小于2时应该抛出错误', () => {
|
|
75
|
+
expect(() => {
|
|
76
|
+
splitNumber(10, 1);
|
|
77
|
+
}).toThrow('Parts must be between 2 and 8');
|
|
78
|
+
});
|
|
79
|
+
test('parts 大于8时应该抛出错误', () => {
|
|
80
|
+
expect(() => {
|
|
81
|
+
splitNumber(10, 9);
|
|
82
|
+
}).toThrow('Parts must be between 2 and 8');
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
describe('arrayFill', () => {
|
|
86
|
+
test('应该创建指定长度的数组并填充值', () => {
|
|
87
|
+
expect(arrayFill(3, 'test')).toEqual(['test', 'test', 'test']);
|
|
88
|
+
expect(arrayFill(5, 0)).toEqual([0, 0, 0, 0, 0]);
|
|
89
|
+
});
|
|
90
|
+
test('number 小于等于0时应该返回空数组', () => {
|
|
91
|
+
expect(arrayFill(0, 'test')).toEqual([]);
|
|
92
|
+
expect(arrayFill(-1, 'test')).toEqual([]);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
describe('splitArray', () => {
|
|
96
|
+
test('应该将数组按指定间隔分割', () => {
|
|
97
|
+
expect(splitArray([1, 2, 3, 4, 5, 6, 7, 8], 4)).toEqual([[1, 2, 3, 4], [5, 6, 7, 8]]);
|
|
98
|
+
});
|
|
99
|
+
test('数组长度不能被间隔整除时应该保留剩余元素', () => {
|
|
100
|
+
expect(splitArray([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
|
|
101
|
+
});
|
|
102
|
+
test('默认间隔为4', () => {
|
|
103
|
+
expect(splitArray([1, 2, 3, 4, 5, 6, 7, 8])).toEqual([[1, 2, 3, 4], [5, 6, 7, 8]]);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
describe('splitLight', () => {
|
|
107
|
+
test('颜色数组长度为2时应该返回正确的分割', () => {
|
|
108
|
+
const result = splitLight(['rgb(255, 0, 0)', 'rgb(0, 255, 0)']);
|
|
109
|
+
expect(result.arr).toEqual([10, 10]);
|
|
110
|
+
// splitArray 将2个颜色按间隔2分割,得到1组(每组2个元素)
|
|
111
|
+
expect(result.color).toHaveLength(1);
|
|
112
|
+
expect(result.color[0]).toHaveLength(2);
|
|
113
|
+
});
|
|
114
|
+
test('颜色数组长度为3时应该返回正确的分割', () => {
|
|
115
|
+
const result = splitLight(['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)']);
|
|
116
|
+
expect(result.arr).toEqual([6, 7, 7]);
|
|
117
|
+
// splitArray 将3个颜色按间隔3分割,得到1组(每组3个元素)
|
|
118
|
+
expect(result.color).toHaveLength(1);
|
|
119
|
+
expect(result.color[0]).toHaveLength(3);
|
|
120
|
+
});
|
|
121
|
+
test('颜色数组长度为5时应该扩展为4倍', () => {
|
|
122
|
+
const result = splitLight(['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)', 'rgb(255, 255, 0)', 'rgb(255, 0, 255)']);
|
|
123
|
+
expect(result.arr).toEqual([5, 5, 5, 5]);
|
|
124
|
+
// arrayFill(4, colors).flat(1) 会创建 4 * 5 = 20 个元素,然后按间隔4分割
|
|
125
|
+
expect(result.color).toHaveLength(5);
|
|
126
|
+
expect(result.color[0]).toHaveLength(4);
|
|
127
|
+
});
|
|
128
|
+
test('颜色数组长度为6时应该扩展为2倍', () => {
|
|
129
|
+
const colors = Array(6).fill('rgb(255, 0, 0)');
|
|
130
|
+
const result = splitLight(colors);
|
|
131
|
+
expect(result.arr).toEqual([5, 5, 5, 5]);
|
|
132
|
+
// arrayFill(2, colors).flat(1) 会创建 2 * 6 = 12 个元素,然后按间隔4分割
|
|
133
|
+
expect(result.color).toHaveLength(3);
|
|
134
|
+
expect(result.color[0]).toHaveLength(4);
|
|
135
|
+
});
|
|
136
|
+
test('颜色数组长度为7时应该扩展为4倍', () => {
|
|
137
|
+
const colors = Array(7).fill('rgb(255, 0, 0)');
|
|
138
|
+
const result = splitLight(colors);
|
|
139
|
+
expect(result.arr).toEqual([5, 5, 5, 5]);
|
|
140
|
+
// arrayFill(4, colors).flat(1) 会创建 4 * 7 = 28 个元素,然后按间隔4分割
|
|
141
|
+
expect(result.color).toHaveLength(7);
|
|
142
|
+
expect(result.color[0]).toHaveLength(4);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
describe('averageRGBColors', () => {
|
|
146
|
+
test('应该计算多个RGB颜色的平均值', () => {
|
|
147
|
+
const result = averageRGBColors(['rgb(100, 0, 0)', 'rgb(200, 0, 0)']);
|
|
148
|
+
expect(result).toBe('rgb(150, 0, 0)');
|
|
149
|
+
});
|
|
150
|
+
test('应该处理三个颜色分量', () => {
|
|
151
|
+
const result = averageRGBColors(['rgb(100, 50, 0)', 'rgb(200, 100, 50)']);
|
|
152
|
+
expect(result).toBe('rgb(150, 75, 25)');
|
|
153
|
+
});
|
|
154
|
+
test('应该处理多个颜色', () => {
|
|
155
|
+
const result = averageRGBColors(['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)']);
|
|
156
|
+
expect(result).toBe('rgb(85, 85, 85)');
|
|
157
|
+
});
|
|
158
|
+
test('应该处理 rgba 格式', () => {
|
|
159
|
+
const result = averageRGBColors(['rgba(100, 0, 0, 1)', 'rgba(200, 0, 0, 1)']);
|
|
160
|
+
expect(result).toBe('rgb(150, 0, 0)');
|
|
161
|
+
});
|
|
162
|
+
test('空数组应该抛出错误', () => {
|
|
163
|
+
expect(() => {
|
|
164
|
+
averageRGBColors([]);
|
|
165
|
+
}).toThrow('The input array is empty');
|
|
166
|
+
});
|
|
167
|
+
test('结果应该限制在0-255范围内', () => {
|
|
168
|
+
const result = averageRGBColors(['rgb(300, 0, 0)', 'rgb(400, 0, 0)']);
|
|
169
|
+
expect(result).toBe('rgb(255, 0, 0)');
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
describe('findOverlapIndices', () => {
|
|
173
|
+
test('应该找到两个范围的重叠索引', () => {
|
|
174
|
+
expect(findOverlapIndices([0, 5], [3, 8])).toEqual([3, 4, 5]);
|
|
175
|
+
expect(findOverlapIndices([2, 6], [4, 10])).toEqual([4, 5, 6]);
|
|
176
|
+
});
|
|
177
|
+
test('没有重叠时应该返回空数组', () => {
|
|
178
|
+
expect(findOverlapIndices([0, 2], [3, 5])).toEqual([]);
|
|
179
|
+
expect(findOverlapIndices([5, 10], [0, 4])).toEqual([]);
|
|
180
|
+
});
|
|
181
|
+
test('完全重叠时应该返回完整范围', () => {
|
|
182
|
+
expect(findOverlapIndices([2, 5], [2, 5])).toEqual([2, 3, 4, 5]);
|
|
183
|
+
});
|
|
184
|
+
test('一个范围包含另一个范围时应该返回较小的范围', () => {
|
|
185
|
+
expect(findOverlapIndices([0, 10], [3, 7])).toEqual([3, 4, 5, 6, 7]);
|
|
186
|
+
});
|
|
187
|
+
test('负数索引应该被处理为0', () => {
|
|
188
|
+
expect(findOverlapIndices([-1, 3], [1, 5])).toEqual([1, 2, 3]);
|
|
189
|
+
expect(findOverlapIndices([0, 3], [-2, 2])).toEqual([0, 1, 2]);
|
|
190
|
+
});
|
|
191
|
+
test('负数范围会被处理为0,可能产生交集', () => {
|
|
192
|
+
// [-5, -2] 被处理为 [0, 0],[0, 3] 被处理为 [0, 3]
|
|
193
|
+
// 重叠部分是 [0, 0],所以返回 [0]
|
|
194
|
+
expect(findOverlapIndices([-5, -2], [0, 3])).toEqual([0]);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview 组件测试用例 - 从入口文件 index.tsx 开始测试
|
|
5
|
+
* @jest-environment jsdom
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/// <reference types="jest" />
|
|
9
|
+
|
|
10
|
+
// Mock must be before imports
|
|
11
|
+
import '@testing-library/jest-dom';
|
|
12
|
+
import React from 'react';
|
|
13
|
+
import { render } from '@testing-library/react';
|
|
14
|
+
import LampBeadStrip from './index';
|
|
15
|
+
import { defaultProps } from './props';
|
|
16
|
+
jest.mock('./utils', () => ({
|
|
17
|
+
getSystemInfoRes: jest.fn(() => ({
|
|
18
|
+
windowWidth: 375
|
|
19
|
+
}))
|
|
20
|
+
}));
|
|
21
|
+
jest.mock('./components', () => {
|
|
22
|
+
return jest.fn(props => {
|
|
23
|
+
var _props$prop;
|
|
24
|
+
return /*#__PURE__*/React.createElement("div", {
|
|
25
|
+
"data-testid": "lamp-bead-strip-component",
|
|
26
|
+
"data-props": JSON.stringify(props)
|
|
27
|
+
}, /*#__PURE__*/React.createElement("canvas", {
|
|
28
|
+
"data-testid": "lamp-canvas",
|
|
29
|
+
"data-canvas-id": (_props$prop = props.prop) === null || _props$prop === void 0 ? void 0 : _props$prop.canvasId
|
|
30
|
+
}));
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
describe('LampBeadStrip Component', () => {
|
|
34
|
+
const defaultTestProps = {
|
|
35
|
+
canvasId: 'test-canvas-123',
|
|
36
|
+
colors: ['rgb(255, 0, 0)', 'rgb(0, 255, 0)'],
|
|
37
|
+
mode: 1,
|
|
38
|
+
ready: true,
|
|
39
|
+
speed: 50
|
|
40
|
+
};
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
jest.clearAllMocks();
|
|
43
|
+
});
|
|
44
|
+
test('应该正确渲染组件', () => {
|
|
45
|
+
const {
|
|
46
|
+
getByTestId
|
|
47
|
+
} = render(/*#__PURE__*/React.createElement(LampBeadStrip, defaultTestProps));
|
|
48
|
+
const component = getByTestId('lamp-bead-strip-component');
|
|
49
|
+
expect(component).toBeInTheDocument();
|
|
50
|
+
});
|
|
51
|
+
test('应该传递正确的 props 给子组件', () => {
|
|
52
|
+
const {
|
|
53
|
+
getByTestId
|
|
54
|
+
} = render(/*#__PURE__*/React.createElement(LampBeadStrip, defaultTestProps));
|
|
55
|
+
const component = getByTestId('lamp-bead-strip-component');
|
|
56
|
+
const props = JSON.parse(component.getAttribute('data-props') || '{}');
|
|
57
|
+
expect(props.prop.canvasId).toBe(defaultTestProps.canvasId);
|
|
58
|
+
expect(props.prop.colors).toEqual(defaultTestProps.colors);
|
|
59
|
+
expect(props.prop.mode).toBe(defaultTestProps.mode);
|
|
60
|
+
expect(props.prop.ready).toBe(defaultTestProps.ready);
|
|
61
|
+
expect(props.prop.speed).toBe(defaultTestProps.speed);
|
|
62
|
+
});
|
|
63
|
+
test('应该计算并传递 scale 属性', () => {
|
|
64
|
+
const {
|
|
65
|
+
getByTestId
|
|
66
|
+
} = render(/*#__PURE__*/React.createElement(LampBeadStrip, defaultTestProps));
|
|
67
|
+
const component = getByTestId('lamp-bead-strip-component');
|
|
68
|
+
const props = JSON.parse(component.getAttribute('data-props') || '{}');
|
|
69
|
+
|
|
70
|
+
// windowWidth = 375, standardScale = 1 (default)
|
|
71
|
+
// scale = (375 / 375) * 1 = 1
|
|
72
|
+
expect(props.prop.scale).toBe(1);
|
|
73
|
+
});
|
|
74
|
+
test('应该使用 standardScale 计算缩放', () => {
|
|
75
|
+
const {
|
|
76
|
+
getByTestId
|
|
77
|
+
} = render(/*#__PURE__*/React.createElement(LampBeadStrip, _extends({}, defaultTestProps, {
|
|
78
|
+
standardScale: 2
|
|
79
|
+
})));
|
|
80
|
+
const component = getByTestId('lamp-bead-strip-component');
|
|
81
|
+
const props = JSON.parse(component.getAttribute('data-props') || '{}');
|
|
82
|
+
|
|
83
|
+
// windowWidth = 375, standardScale = 2
|
|
84
|
+
// scale = (375 / 375) * 2 = 2
|
|
85
|
+
expect(props.prop.scale).toBe(2);
|
|
86
|
+
});
|
|
87
|
+
test('应该优先使用传入的 scale 属性', () => {
|
|
88
|
+
const {
|
|
89
|
+
getByTestId
|
|
90
|
+
} = render(/*#__PURE__*/React.createElement(LampBeadStrip, _extends({}, defaultTestProps, {
|
|
91
|
+
scale: 3,
|
|
92
|
+
standardScale: 2
|
|
93
|
+
})));
|
|
94
|
+
const component = getByTestId('lamp-bead-strip-component');
|
|
95
|
+
const props = JSON.parse(component.getAttribute('data-props') || '{}');
|
|
96
|
+
|
|
97
|
+
// 如果传入了 scale,应该使用传入的值
|
|
98
|
+
expect(props.prop.scale).toBe(3);
|
|
99
|
+
});
|
|
100
|
+
test('应该传递所有其他 props', () => {
|
|
101
|
+
const customProps = _objectSpread(_objectSpread({}, defaultTestProps), {}, {
|
|
102
|
+
className: 'custom-class',
|
|
103
|
+
containerStyle: 'width: 100px;',
|
|
104
|
+
canvasStyle: 'height: 50px;',
|
|
105
|
+
closeColor: 'rgb(58,58,58)',
|
|
106
|
+
contentValue: {
|
|
107
|
+
expand: 1,
|
|
108
|
+
direction: 0,
|
|
109
|
+
segmented: 1
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
const {
|
|
113
|
+
getByTestId
|
|
114
|
+
} = render(/*#__PURE__*/React.createElement(LampBeadStrip, customProps));
|
|
115
|
+
const component = getByTestId('lamp-bead-strip-component');
|
|
116
|
+
const props = JSON.parse(component.getAttribute('data-props') || '{}');
|
|
117
|
+
expect(props.prop.className).toBe(customProps.className);
|
|
118
|
+
expect(props.prop.containerStyle).toBe(customProps.containerStyle);
|
|
119
|
+
expect(props.prop.canvasStyle).toBe(customProps.canvasStyle);
|
|
120
|
+
expect(props.prop.closeColor).toBe(customProps.closeColor);
|
|
121
|
+
expect(props.prop.contentValue).toEqual(customProps.contentValue);
|
|
122
|
+
});
|
|
123
|
+
test('应该使用 defaultProps', () => {
|
|
124
|
+
const {
|
|
125
|
+
getByTestId
|
|
126
|
+
} = render(/*#__PURE__*/React.createElement(LampBeadStrip, {
|
|
127
|
+
canvasId: 'test-canvas',
|
|
128
|
+
colors: ['rgb(255, 0, 0)']
|
|
129
|
+
}));
|
|
130
|
+
const component = getByTestId('lamp-bead-strip-component');
|
|
131
|
+
const props = JSON.parse(component.getAttribute('data-props') || '{}');
|
|
132
|
+
|
|
133
|
+
// 应该使用 defaultProps 中的默认值
|
|
134
|
+
expect(props.prop.mode).toBe(defaultProps.mode);
|
|
135
|
+
expect(props.prop.ready).toBe(defaultProps.ready);
|
|
136
|
+
expect(props.prop.speed).toBe(defaultProps.speed);
|
|
137
|
+
expect(props.prop.closeColor).toBe(defaultProps.closeColor);
|
|
138
|
+
});
|
|
139
|
+
test('应该处理不同的 windowWidth', () => {
|
|
140
|
+
// 由于 getSystemInfoRes 在模块顶层被调用,我们需要重新 mock
|
|
141
|
+
// 这个测试验证了默认的 windowWidth = 375 的情况
|
|
142
|
+
// 如果要测试不同的 windowWidth,需要在模块加载前设置 mock
|
|
143
|
+
const {
|
|
144
|
+
getByTestId
|
|
145
|
+
} = render(/*#__PURE__*/React.createElement(LampBeadStrip, defaultTestProps));
|
|
146
|
+
const component = getByTestId('lamp-bead-strip-component');
|
|
147
|
+
const props = JSON.parse(component.getAttribute('data-props') || '{}');
|
|
148
|
+
|
|
149
|
+
// windowWidth = 375 (from mock), standardScale = 1
|
|
150
|
+
// scale = (375 / 375) * 1 = 1
|
|
151
|
+
expect(props.prop.scale).toBe(1);
|
|
152
|
+
});
|
|
153
|
+
test('应该支持自定义动画列表', () => {
|
|
154
|
+
const customAnimationList = [['rgb(255, 0, 0)', 'rgb(0, 255, 0)'], ['rgb(0, 255, 0)', 'rgb(0, 0, 255)']];
|
|
155
|
+
const {
|
|
156
|
+
getByTestId
|
|
157
|
+
} = render(/*#__PURE__*/React.createElement(LampBeadStrip, _extends({}, defaultTestProps, {
|
|
158
|
+
customAnimationList: customAnimationList,
|
|
159
|
+
customAnimationChangeTime: 200
|
|
160
|
+
})));
|
|
161
|
+
const component = getByTestId('lamp-bead-strip-component');
|
|
162
|
+
const props = JSON.parse(component.getAttribute('data-props') || '{}');
|
|
163
|
+
expect(props.prop.customAnimationList).toEqual(customAnimationList);
|
|
164
|
+
expect(props.prop.customAnimationChangeTime).toBe(200);
|
|
165
|
+
});
|
|
166
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/lamp-bead-strip",
|
|
3
|
-
"version": "1.1.1-beta-
|
|
3
|
+
"version": "1.1.1-beta-2",
|
|
4
4
|
"description": "动画灯珠条带",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"files": [
|
|
@@ -17,6 +17,9 @@
|
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
19
|
"lint": "eslint src --ext .js,.jsx,.ts,.tsx --fix",
|
|
20
|
+
"test": "jest",
|
|
21
|
+
"test:watch": "jest --watch",
|
|
22
|
+
"test:coverage": "jest --coverage",
|
|
20
23
|
"build": "patch-package && ray build --type=component",
|
|
21
24
|
"watch": "ray start --type=component --output ./example/src/lib",
|
|
22
25
|
"build:tuya": "ray build ./example",
|
|
@@ -35,17 +38,29 @@
|
|
|
35
38
|
},
|
|
36
39
|
"dependencies": {},
|
|
37
40
|
"devDependencies": {
|
|
41
|
+
"@babel/preset-env": "^7.28.5",
|
|
42
|
+
"@babel/preset-typescript": "^7.28.5",
|
|
38
43
|
"@commitlint/cli": "^7.2.1",
|
|
39
44
|
"@commitlint/config-conventional": "^9.0.1",
|
|
40
45
|
"@ray-js/cli": "^1.4.9",
|
|
41
46
|
"@ray-js/panel-sdk": "^1.12.1",
|
|
42
47
|
"@ray-js/ray": "^1.4.9",
|
|
48
|
+
"@testing-library/dom": "^10.4.1",
|
|
49
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
50
|
+
"@testing-library/react": "^16.3.0",
|
|
51
|
+
"@types/jest": "^30.0.0",
|
|
52
|
+
"babel-jest": "^30.2.0",
|
|
43
53
|
"core-js": "^3.19.1",
|
|
44
54
|
"eslint-config-tuya-panel": "^0.4.2",
|
|
45
55
|
"husky": "^1.2.0",
|
|
56
|
+
"jest": "^30.2.0",
|
|
57
|
+
"jest-environment-jsdom": "^30.2.0",
|
|
46
58
|
"lint-staged": "^10.2.11",
|
|
47
59
|
"patch-package": "^8.0.0",
|
|
48
|
-
"
|
|
60
|
+
"react": "^19.2.0",
|
|
61
|
+
"react-dom": "^19.2.0",
|
|
62
|
+
"standard-version": "9.3.2",
|
|
63
|
+
"ts-jest": "^29.4.5"
|
|
49
64
|
},
|
|
50
65
|
"resolutions": {
|
|
51
66
|
"@ray-js/builder-mp": "1.4.15"
|
|
@@ -65,5 +80,6 @@
|
|
|
65
80
|
"prettier --write",
|
|
66
81
|
"git add"
|
|
67
82
|
]
|
|
68
|
-
}
|
|
83
|
+
},
|
|
84
|
+
"author": "潇齐(马佳辉)"
|
|
69
85
|
}
|