feat: deviceSlice添加

This commit is contained in:
zdl
2025-11-25 16:57:48 +08:00
parent 1bc3241596
commit bb878c5346
3 changed files with 456 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
/**
* deviceSlice 单元测试
*
* 测试用例:
* 1. 初始状态检查
* 2. updateScreenSize action 测试
* 3. selector 函数测试
*/
import deviceReducer, { updateScreenSize, selectIsMobile } from './deviceSlice';
describe('deviceSlice', () => {
describe('reducer', () => {
it('should return the initial state', () => {
const initialState = deviceReducer(undefined, { type: '@@INIT' });
expect(initialState).toHaveProperty('isMobile');
expect(typeof initialState.isMobile).toBe('boolean');
});
it('should handle updateScreenSize', () => {
// 模拟初始状态
const initialState = { isMobile: false };
// 执行 action注意实际 isMobile 值由 detectIsMobile() 决定)
const newState = deviceReducer(initialState, updateScreenSize());
// 验证状态结构
expect(newState).toHaveProperty('isMobile');
expect(typeof newState.isMobile).toBe('boolean');
});
});
describe('selectors', () => {
it('selectIsMobile should return correct value', () => {
const mockState = {
device: {
isMobile: true,
},
};
const result = selectIsMobile(mockState);
expect(result).toBe(true);
});
it('selectIsMobile should return false for desktop', () => {
const mockState = {
device: {
isMobile: false,
},
};
const result = selectIsMobile(mockState);
expect(result).toBe(false);
});
});
describe('actions', () => {
it('updateScreenSize action should have correct type', () => {
const action = updateScreenSize();
expect(action.type).toBe('device/updateScreenSize');
});
});
});