Files
vf_react/src/store/slices/deviceSlice.test.js
2025-11-25 17:04:30 +08:00

64 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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');
});
});
});