import { model } from './grid-exp-ctrl-controller-model'; import { useEventBind, useNavParamsBind } from '@/hooks/use-ctrl'; import { GridExpBarController, IParam } from '@/core'; const evt = useEventBind(emit); const params: any = { name: model.name, model, evt, }; const controller: any = new GridExpBarController(params); const context = {}; const viewParams = {}; useNavParamsBind(controller, { context, viewParams }); const ability: any = { load: (params: IParam) => { return { ok: true, data: null, rowData: { status: 200 }, }; }, }; describe('ExpCtrlController unit test', () => { const getAbilitySpy = vi .spyOn(controller, 'getMDCtrlAbility') .mockImplementation(() => ability); test('load', () => { controller.load(); expect(getAbilitySpy).toHaveBeenCalled(); }), test('search', () => { const searchValue = '查询数据'; const loadSpy = vi.spyOn(ability, 'load'); controller.search(searchValue); expect(controller).toHaveProperty('searchValue', searchValue); expect(loadSpy).toHaveBeenCalled(); expect(loadSpy).toHaveBeenCalledWith({ query: searchValue }); }); test('handleSelectionChange', () => { const item = { srfkey: '512351', srfmajortext: 'dgasg' }; controller.handleSelectionChange([item]); expect(emit).toHaveBeenCalled(); // TODO 后续判断值 // expect(emit).toHaveBeenLastCalledWith('ctrl-action', model.name, 'selectionChange'); }); test('handleMDCtrlBeforeLoad', () => { const param = { value: '25125' }; controller.searchValue = '查询数据'; controller.handleMDCtrlBeforeLoad([param]); expect(param).toHaveProperty('query', '查询数据'); expect(emit).toHaveBeenCalled(); expect(emit).toHaveBeenLastCalledWith( 'ctrl-action', model.name, 'beforeLoad', [param] ); }); });