All files / tests/unit/__helpers__ mock-http-get-helper.ts

100% Statements 13/13
100% Branches 3/3
100% Functions 4/4
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 302x             7x 8x 1x         7x   9x 7x 7x   7x               2x  
import { Http, IDictionary } from '@zeedhi/core';
 
/**
 * Mocks Http.get to simulate a paginated backend. (using mockClear is necessary)
 * @param data data that will be returned
 * @returns the jest spy
 */
const mockGetUsingData = (data: IDictionary[]) => jest.spyOn(Http, 'get').mockImplementation((_url, config) => {
  if (!config?.params) {
    return Promise.resolve({
      data: { data },
    });
  }
 
  const { page, limit, search } = config.params;
 
  const searchFn = (item: IDictionary) => Object.values(item).some((value) => String(value).indexOf(search) > -1);
  const searched = search ? [...data].filter(searchFn) : [...data];
  const paginatedData = searched.splice((page - 1) * limit, limit);
 
  return Promise.resolve({
    data: {
      data: paginatedData,
      pagination: { page, total: data.length, limit },
    },
  });
});
 
export { mockGetUsingData };