UNPKG

react-carousel-query

Version:

A infinite carousel component made with react that handles the pagination for you.

98 lines (97 loc) 2.86 kB
"use strict"; var _reactHooks = require("@testing-library/react-hooks"); var _useQueryManager = require("./useQueryManager"); var _utils = require("../utils"); describe('useQueryManager', () => { const MOCK_ITEMS = [{ id: '1', name: 'first' }, { id: '2', name: 'second' }]; const OFFSET = 3; const TOTAL = 6; const props = { getData: () => ({ offset: OFFSET, total: TOTAL, items: MOCK_ITEMS }) }; test('should save the items and total', async () => { const { result, waitForNextUpdate } = (0, _reactHooks.renderHook)(() => (0, _useQueryManager.useQueryManagerProvider)(props)); await waitForNextUpdate(); expect(result.current.items).toEqual(MOCK_ITEMS); expect(result.current.total).toEqual(TOTAL); }); test('should update offset', async () => { const { result, waitForNextUpdate } = (0, _reactHooks.renderHook)(() => (0, _useQueryManager.useQueryManagerProvider)(props)); await waitForNextUpdate(); expect(result.current.offset).toEqual(OFFSET * 2); }); test('should concat items', async () => { const getData = jest.fn(() => { return { offset: 2, total: 6, items: MOCK_ITEMS }; }); const { result, waitForNextUpdate } = (0, _reactHooks.renderHook)(() => (0, _useQueryManager.useQueryManagerProvider)({ getData })); await waitForNextUpdate(); (0, _utils.callTimes)(5, () => { (0, _reactHooks.act)(() => { result.current.next(); }); }); await waitForNextUpdate(); expect(result.current.items).toEqual(MOCK_ITEMS.concat(MOCK_ITEMS)); expect(getData).toBeCalledTimes(2); }); test('should update currentIndex on next', async () => { const { result, waitForNextUpdate } = (0, _reactHooks.renderHook)(() => (0, _useQueryManager.useQueryManagerProvider)(props)); await waitForNextUpdate(); (0, _reactHooks.act)(() => { result.current.next(); }); expect(result.current.currentIndex).toEqual(1); }); test('should update currentIndex on previous', async () => { const { result, waitForNextUpdate } = (0, _reactHooks.renderHook)(() => (0, _useQueryManager.useQueryManagerProvider)(props)); await waitForNextUpdate(); (0, _reactHooks.act)(() => { result.current.next(); result.current.previous(); }); expect(result.current.currentIndex).toEqual(0); }); test('should setCurrentIndex', async () => { const { result, waitForNextUpdate } = (0, _reactHooks.renderHook)(() => (0, _useQueryManager.useQueryManagerProvider)(props)); await waitForNextUpdate(); (0, _reactHooks.act)(() => { result.current.setCurrentIndex(2); }); expect(result.current.currentIndex).toEqual(2); }); });