64 lines
1.7 KiB
JavaScript
64 lines
1.7 KiB
JavaScript
/**
|
||
* 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');
|
||
});
|
||
});
|
||
});
|