Skip to content

Commit

Permalink
Merge pull request #1672 from miracles1919/test/use-fusion-table
Browse files Browse the repository at this point in the history
test(useFusionTable): add some tests
  • Loading branch information
crazylxr authored Jun 15, 2022
2 parents 28eb19f + 50d4b6a commit c8451f7
Showing 1 changed file with 203 additions and 0 deletions.
203 changes: 203 additions & 0 deletions packages/hooks/src/useFusionTable/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import { act, renderHook } from '@testing-library/react-hooks';
import useFusionTable from '../index';
import { sleep } from '../../utils/testingHelpers';

type Result = {
total: number;
list: any[];
};

let count = 0;
const total = 40;
const getTableData = async ({ current, pageSize }): Promise<Result> => {
if (count * current >= total) {
return {
total,
list: [],
};
}
await sleep(1000);
count++;
const list = new Array(pageSize).fill(1).map((item, i) => {
const index = current * pageSize + i;
return {
id: index,
name: 'test',
};
});

return {
total,
list,
};
};

let values = {};
const mockField = {
getNames() {
return [];
},
setValues(v) {
values = v;
},
getValues() {
return values;
},
resetToDefault() {
values = {};
},
validate(names, callback) {
callback(null, values);
},
};

const setup = (service, options = {}) => renderHook(() => useFusionTable(service, options));

describe('useFusionTable', () => {
beforeEach(() => {
count = 0;
values = {};
});

beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

it('should be defined', () => {
expect(useFusionTable).toBeDefined();
});

it('should get table & pagination props', async () => {
const { result } = setup(getTableData);
await act(async () => {
jest.runAllTimers();
});
expect(result.current.tableProps.loading).toBeTruthy();
await act(async () => {
jest.advanceTimersByTime(1000);
});
expect(result.current.tableProps.loading).toBeFalsy();
expect(result.current.tableProps.dataSource).toHaveLength(10);
expect(result.current.paginationProps.current).toBe(1);
expect(result.current.paginationProps.total).toBe(total);
});

it('should get table data when page change', async () => {
const { result } = setup(getTableData);
const current = 2;
await act(async () => {
jest.runAllTimers();
});
await act(async () => {
jest.advanceTimersByTime(1000);
});
act(() => {
result.current.paginationProps.onChange(current);
});
await act(async () => {
jest.advanceTimersByTime(1000);
});
expect(result.current.paginationProps.current).toBe(current);
expect(result.current.paginationProps.total).toBe(total);
});

it('search should work when set field instance', async () => {
const { result } = setup(getTableData, { field: mockField });
mockField.setValues({
name: 'ahooks',
});

result.current.search.submit();
await act(async () => {
jest.runAllTimers();
});
expect(result.current.loading).toBeTruthy();
expect(result.current.params[1]).toMatchObject({ name: 'ahooks' });
await act(async () => {
jest.advanceTimersByTime(1000);
});
expect(result.current.loading).toBeFalsy();

result.current.search.reset();
expect(result.current.params[1]).toMatchObject({});
await act(async () => {
jest.runAllTimers();
});
expect(result.current.loading).toBeTruthy();
await act(async () => {
jest.advanceTimersByTime(1000);
});
expect(result.current.loading).toBeFalsy();
});

it('defaultParams should be work', async () => {
const { result } = setup(getTableData, {
defaultParams: [{ current: 2, pageSize: 20 }],
});
await act(async () => {
jest.runAllTimers();
});
await act(async () => {
jest.advanceTimersByTime(1000);
});
expect(result.current.tableProps.dataSource).toHaveLength(20);
expect(result.current.paginationProps.current).toBe(2);
});

it('cache should be work', async () => {
const options = {
field: mockField,
cacheKey: 'cache',
defaultParams: [
{
current: 2,
pageSize: 5,
},
{ name: 'hello', phone: '123' },
],
};
const hook = setup(getTableData, options);
await act(async () => {
jest.runAllTimers();
});
await act(async () => {
jest.advanceTimersByTime(1000);
});

expect(hook.result.current.tableProps.dataSource).toHaveLength(5);
expect(hook.result.current.loading).toBeFalsy();

hook.unmount();
const hook2 = setup(getTableData, options);
await act(async () => {
jest.runAllTimers();
});
expect(hook2.result.current.loading).toBeFalsy();
expect(hook2.result.current.tableProps.dataSource).toHaveLength(5);
});

it('onSort should be work', async () => {
const { result } = setup(getTableData);
act(() => {
result.current.tableProps.onSort('dataIndex', 'asc');
});
expect(result.current.loading).toBeTruthy();
expect(result.current.params[0]?.sorter).toMatchObject({ field: 'dataIndex', order: 'asc' });
});

it('onFilter should be work', async () => {
const { result } = setup(getTableData);
const filterParams = {
version: 3,
};
act(() => {
result.current.tableProps.onFilter(filterParams);
});
expect(result.current.loading).toBeTruthy();
expect(result.current.params[0]?.filters).toMatchObject(filterParams);
});
});

0 comments on commit c8451f7

Please sign in to comment.