UNPKG

@cainiaofe/cn-ui-m

Version:
91 lines (90 loc) 3.83 kB
import { __awaiter, __generator } from "tslib"; import { renderHook, act } from '@testing-library/react-hooks'; import { useCnListDataSource } from '../service'; import { useCnRequest } from '@cainiaofe/cn-ui-common'; jest.mock('@cainiaofe/cn-ui-common'); describe('useCnListDataSource', function () { beforeEach(function () { jest.resetAllMocks(); }); it('returns correct initial state when no requestConfig is provided', function () { useCnRequest.mockReturnValue({ data: { paging: { currentPage: 1, totalCount: 2 }, tableData: [{ id: 1 }, { id: 2 }], }, runAsync: jest.fn(), loading: false, }); var result = renderHook(function () { return useCnListDataSource({}); }).result; expect(result.current.loading).toBe(false); expect(result.current.hasNextPage).toBe(false); expect(result.current.dataSource).toEqual([]); expect(result.current.fullPageLoading).toBe(false); }); it('returns correct state when requestConfig is provided', function () { return __awaiter(void 0, void 0, void 0, function () { var result; return __generator(this, function (_a) { useCnRequest.mockReturnValue({ data: { paging: { currentPage: 1, totalCount: 2 }, tableData: [{ id: 1 }, { id: 2 }], }, runAsync: jest.fn(), loading: false, }); result = renderHook(function () { return useCnListDataSource({ requestConfig: { url: 'test-url' } }); }).result; expect(result.current.loading).toBe(false); expect(result.current.hasNextPage).toBe(false); expect(result.current.dataSource).toEqual([{ id: 1 }, { id: 2 }]); expect(result.current.fullPageLoading).toBe(false); return [2 /*return*/]; }); }); }); it('handles refreshAsync correctly', function () { return __awaiter(void 0, void 0, void 0, function () { var runAsyncMock, result; return __generator(this, function (_a) { runAsyncMock = jest.fn(); useCnRequest.mockReturnValue({ data: { paging: { currentPage: 1, totalCount: 2 }, tableData: [{ id: 1 }, { id: 2 }], }, runAsync: runAsyncMock, loading: false, }); result = renderHook(function () { return useCnListDataSource({ requestConfig: { url: 'test-url' } }); }).result; act(function () { result.current.refreshAsync(); }); expect(runAsyncMock).toHaveBeenCalledWith({ currentPage: 1, pageSize: 10 }); return [2 /*return*/]; }); }); }); it('handles loadNextPage correctly', function () { return __awaiter(void 0, void 0, void 0, function () { var runAsyncMock, result; return __generator(this, function (_a) { runAsyncMock = jest.fn(); useCnRequest.mockReturnValue({ data: { paging: { currentPage: 1, totalCount: 2 }, tableData: [{ id: 1 }, { id: 2 }], }, runAsync: runAsyncMock, loading: false, }); result = renderHook(function () { return useCnListDataSource({ requestConfig: { url: 'test-url' } }); }).result; act(function () { result.current.loadNextPage(); }); expect(runAsyncMock).toHaveBeenCalledWith({ currentPage: 2, pageSize: 10 }); return [2 /*return*/]; }); }); }); });