var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import React from 'react';
import { renderHook, act, waitFor } from '@testing-library/react';
import * as helpers from './helpers';
import { useSearchNavigation } from './useSearchNavigation';
import { ActionsHookProvider } from '../contexts/ActionsHookContext';
import { MdmModuleProvider } from '../contexts/MdmModuleContext';
import { useEntityLoadingIndication } from '../contexts/EntityLoadingIndicationContext';
import { createMdmValues } from './store.test-data';
import { REQUEST_PAGE_SIZE } from './constants';
jest.mock('../contexts/EntityLoadingIndicationContext', function () { return (__assign(__assign({}, jest.requireActual('../contexts/EntityLoadingIndicationContext')), { useEntityLoadingIndication: jest.fn() })); });
var mdmActions = {
    openPerspective: jest.fn(),
    updateSearchNavigationDataFields: jest.fn(),
    openEntity: jest.fn()
};
var defaultMdmValues = createMdmValues();
var setUp = function (_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.mdmValues, mdmValues = _c === void 0 ? defaultMdmValues : _c;
    var triggerAction;
    var listenToActions = function (listener) {
        triggerAction = function (action, state) { return listener(action, state); };
        return function () { };
    };
    var Providers = function (_a) {
        var children = _a.children;
        return (React.createElement(MdmModuleProvider, { values: mdmValues, actions: mdmActions },
            React.createElement(ActionsHookProvider, { hook: listenToActions }, children)));
    };
    return __assign(__assign({}, renderHook(useSearchNavigation, { wrapper: Providers })), { triggerAction: triggerAction });
};
describe('useSearchNavigation behavior', function () {
    var updateEntitiesNavigationLoadingState = jest.fn();
    var searchProvider, getSearchProviderSpy;
    beforeAll(function () {
        useEntityLoadingIndication.mockReturnValue({ updateEntitiesNavigationLoadingState: updateEntitiesNavigationLoadingState });
        getSearchProviderSpy = jest.spyOn(helpers, 'getSearchProvider').mockImplementation(function () { return searchProvider; });
    });
    beforeEach(function () {
        searchProvider = {
            checkEntity: jest.fn().mockResolvedValue(true),
            requestEntities: jest.fn().mockResolvedValue([{ uri: 'uri4' }]),
            requestTotal: jest.fn().mockResolvedValue(100)
        };
    });
    afterEach(function () {
        jest.clearAllMocks();
        sessionStorage.clear();
    });
    describe('general behavior', function () {
        it('should return total value according store.search.searchNavigationData.total', function () {
            var searchNavigationData = __assign(__assign({}, defaultMdmValues.searchNavigationData), { total: 42 });
            var mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData });
            var result = setUp({ mdmValues: mdmValues }).result;
            var _a = result.current, total = _a.total, isPrevDisabled = _a.isPrevDisabled, isNextDisabled = _a.isNextDisabled;
            expect(total).toBe(42);
            expect(isPrevDisabled).toBe(false);
            expect(isNextDisabled).toBe(false);
        });
        it('should return isPrevDisabled equals true if store.search.searchNavigationData.total.index is 0', function () {
            var searchNavigationData = __assign(__assign({}, defaultMdmValues.searchNavigationData), { index: 0 });
            var mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData });
            var result = setUp({ mdmValues: mdmValues }).result;
            var _a = result.current, isPrevDisabled = _a.isPrevDisabled, isNextDisabled = _a.isNextDisabled;
            expect(isPrevDisabled).toBe(true);
            expect(isNextDisabled).toBe(false);
        });
        it('should return isPrevDisabled equals true if store.search.searchNavigationData.total.index more or equal total - 1', function () {
            var searchNavigationData = __assign(__assign({}, defaultMdmValues.searchNavigationData), { index: defaultMdmValues.searchNavigationData.total - 1 });
            var mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData });
            var result = setUp({ mdmValues: mdmValues }).result;
            var _a = result.current, isPrevDisabled = _a.isPrevDisabled, isNextDisabled = _a.isNextDisabled;
            expect(isPrevDisabled).toBe(false);
            expect(isNextDisabled).toBe(true);
        });
        it('should create searchProvider with correct arguments according store state', function () {
            var searchNavigationData = __assign(__assign({}, defaultMdmValues.searchNavigationData), { tenant: 'dt1' });
            var mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData });
            setUp({ mdmValues: mdmValues });
            expect(getSearchProviderSpy).toHaveBeenCalledWith(mdmValues.searchProviderData.type, {
                data: mdmValues.searchProviderData.data,
                tenant: 't1',
                searchTenant: 'dt1',
                apiPath: mdmValues.apiPath,
                dtssPath: mdmValues.dtssPath,
                globalSearchRequestOptions: {
                    activityFilter: 'active',
                    searchOptions: 'searchByOv',
                    globalFilter: '123'
                }
            });
        });
        it('should dispatch openPerspective action on onSearchResultsClick prop calling', function () {
            var result = setUp().result;
            act(function () {
                result.current.onSearchResultsClick();
            });
            expect(mdmActions.openPerspective).toHaveBeenCalledWith(defaultMdmValues.searchProviderData.type);
        });
        it('should set index as total-1 if searchNavigationData.index is more or equal searchNavigationData.total', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, mdmValues;
            return __generator(this, function (_a) {
                searchNavigationData = __assign(__assign({}, defaultMdmValues.searchNavigationData), { index: defaultMdmValues.searchNavigationData.total });
                mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData });
                setUp({ mdmValues: mdmValues });
                expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                    index: searchNavigationData.total - 1
                });
                return [2 /*return*/];
            });
        }); });
        it('should set index as 0 if searchNavigationData.index is less then 0', function () {
            var searchNavigationData = __assign(__assign({}, defaultMdmValues.searchNavigationData), { index: -1 });
            var mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData });
            setUp({ mdmValues: mdmValues });
            expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ index: 0 });
        });
        it('should not update search navigation data fields if total and index are equals 0', function () {
            var searchNavigationData = __assign(__assign({}, defaultMdmValues.searchNavigationData), { total: 0, index: 0 });
            var mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData });
            setUp({ mdmValues: mdmValues });
            expect(mdmActions.updateSearchNavigationDataFields).not.toHaveBeenCalled();
        });
    });
    describe('onPrev behavior', function () {
        it('should do nothing if cache or uri in searchNavigationData are not specified', function () {
            var searchNavigationData = __assign(__assign({}, defaultMdmValues.searchNavigationData), { uri: null });
            var mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData });
            var result = setUp({ mdmValues: mdmValues }).result;
            act(function () {
                result.current.onPrev();
            });
            expect(mdmActions.updateSearchNavigationDataFields).not.toHaveBeenCalled();
            expect(mdmActions.openEntity).not.toHaveBeenCalled();
            expect(searchProvider.checkEntity).not.toHaveBeenCalled();
            expect(searchProvider.requestEntities).not.toHaveBeenCalled();
            expect(searchProvider.requestTotal).not.toHaveBeenCalled();
        });
        it('should get prev entity uri from cache, check it and if it exists update navigation data', function () { return __awaiter(void 0, void 0, void 0, function () {
            var result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        result = setUp().result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(1);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            index: defaultMdmValues.searchNavigationData.index - 1,
                            uri: 'uri0',
                            cache: defaultMdmValues.searchNavigationData.cache,
                            total: defaultMdmValues.searchNavigationData.total
                        });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri0', source: 'navigation' });
                        expect(searchProvider.checkEntity).toHaveBeenNthCalledWith(1, 'uri0');
                        expect(searchProvider.requestEntities).not.toHaveBeenCalled();
                        expect(searchProvider.requestTotal).not.toHaveBeenCalled();
                        expect(result.current.isNextDisabled).toBe(false);
                        expect(result.current.isPrevDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should set isPrevDisabled and isNextDisabled props as true and update entities navigation loading state during loading process', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchNavigationData = __assign(__assign({}, defaultMdmValues.searchNavigationData), { index: 2 });
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData });
                        result = setUp({ mdmValues: mdmValues }).result;
                        expect(result.current.isNextDisabled).toBe(false);
                        expect(result.current.isPrevDisabled).toBe(false);
                        act(function () {
                            result.current.onPrev();
                        });
                        expect(result.current.isNextDisabled).toBe(true);
                        expect(result.current.isPrevDisabled).toBe(true);
                        expect(updateEntitiesNavigationLoadingState).toHaveBeenCalledWith(true);
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 2:
                        _a.sent();
                        expect(updateEntitiesNavigationLoadingState).toHaveBeenCalledWith(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should get prev entity uri from cache, check it and, if it does not exist, try to check prev prev entity', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.checkEntity.mockResolvedValueOnce(false);
                        searchProvider.checkEntity.mockResolvedValueOnce(true);
                        searchNavigationData = {
                            uri: 'uri2',
                            cache: ['uri0', 'uri1', 'uri2'],
                            index: 2,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri2' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(1);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            index: searchNavigationData.index - 2,
                            uri: 'uri0',
                            cache: ['uri0', 'uri2'],
                            total: searchNavigationData.total - 1
                        });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri0', source: 'navigation' });
                        expect(searchProvider.checkEntity).toHaveBeenCalledTimes(2);
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri1');
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri0');
                        expect(searchProvider.requestEntities).not.toHaveBeenCalled();
                        expect(searchProvider.requestTotal).not.toHaveBeenCalled();
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should stop process of checking entities if there are no more entities (index is 0)', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.checkEntity.mockResolvedValue(false);
                        searchNavigationData = {
                            uri: 'uri2',
                            cache: ['uri0', 'uri1', 'uri2'],
                            index: 2,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri2' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(1);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(0);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            index: searchNavigationData.index - 2,
                            cache: ['uri2'],
                            total: searchNavigationData.total - 2
                        });
                        expect(searchProvider.checkEntity).toHaveBeenCalledTimes(2);
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri1');
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri0');
                        expect(searchProvider.requestEntities).not.toHaveBeenCalled();
                        expect(searchProvider.requestTotal).not.toHaveBeenCalled();
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should request previous entities page and update total if there are no entities in cache before current', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchProvider.requestEntities.mockResolvedValue([{ uri: 'uri0' }, { uri: 'uri1' }, { uri: 'uri2' }]);
                        searchNavigationData = {
                            uri: 'uri3',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 3,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri3' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(2);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            uri: 'uri2',
                            index: searchNavigationData.index - 1,
                            cache: ['uri0', 'uri1', 'uri2', 'uri3', 'uri4', 'uri5']
                        });
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ total: 11 });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri2', source: 'navigation' });
                        expect(searchProvider.checkEntity).not.toHaveBeenCalled();
                        expect(searchProvider.requestEntities).toHaveBeenCalledTimes(1);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(3, 0);
                        expect(searchProvider.requestTotal).toHaveBeenCalledTimes(1);
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not add duplicates to cache if previous entities page contains them', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.requestEntities.mockResolvedValue([{ uri: 'uri1' }, { uri: 'uri2' }, { uri: 'uri3' }]);
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchNavigationData = {
                            uri: 'uri3',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 3,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri3' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(2);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            uri: 'uri2',
                            index: searchNavigationData.index - 2,
                            cache: ['uri1', 'uri2', 'uri3', 'uri4', 'uri5']
                        });
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ total: 11 });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri2', source: 'navigation' });
                        expect(searchProvider.checkEntity).not.toHaveBeenCalled();
                        expect(searchProvider.requestEntities).toHaveBeenCalledTimes(1);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(3, 0);
                        expect(searchProvider.requestTotal).toHaveBeenCalledTimes(1);
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should request additional page if loaded entities page does not contain new entities', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.requestEntities.mockResolvedValueOnce([{ uri: 'uri3' }, { uri: 'uri4' }, { uri: 'uri5' }]);
                        searchProvider.requestEntities.mockResolvedValueOnce([{ uri: 'uri1' }, { uri: 'uri2' }, { uri: 'uri3' }]);
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchNavigationData = {
                            uri: 'uri3',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 6,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri3' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(3);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            uri: 'uri2',
                            index: 1,
                            cache: ['uri1', 'uri2', 'uri3', 'uri4', 'uri5']
                        });
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ total: 11 });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri2', source: 'navigation' });
                        expect(searchProvider.checkEntity).not.toHaveBeenCalled();
                        expect(searchProvider.requestEntities).toHaveBeenCalledTimes(2);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(6, 0);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(3, 0);
                        expect(searchProvider.requestTotal).toHaveBeenCalledTimes(2);
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should stop request additional pages if there are no more new entities according index', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.requestEntities.mockResolvedValueOnce([{ uri: 'uri3' }, { uri: 'uri4' }, { uri: 'uri5' }]);
                        searchProvider.requestEntities.mockResolvedValueOnce([{ uri: 'uri3' }, { uri: 'uri4' }, { uri: 'uri5' }]);
                        searchProvider.requestEntities.mockResolvedValueOnce([]);
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchNavigationData = {
                            uri: 'uri3',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 6,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri3' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(3);
                        expect(mdmActions.openEntity).not.toHaveBeenCalled();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ index: 0 });
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ total: 11 });
                        expect(searchProvider.checkEntity).not.toHaveBeenCalled();
                        expect(searchProvider.requestEntities).toHaveBeenCalledTimes(2);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(6, 0);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(3, 0);
                        expect(searchProvider.requestTotal).toHaveBeenCalledTimes(2);
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should request previous entities page after unsuccessful entity checking if there are no more entities in cache', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.checkEntity.mockResolvedValueOnce(false);
                        searchProvider.requestEntities.mockResolvedValueOnce([{ uri: 'uri0' }, { uri: 'uri1' }, { uri: 'uri2' }]);
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchNavigationData = {
                            uri: 'uri4',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 6,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri4' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(2);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            uri: 'uri2',
                            index: 4,
                            cache: ['uri0', 'uri1', 'uri2', 'uri4', 'uri5']
                        });
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ total: 11 });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri2', source: 'navigation' });
                        expect(searchProvider.checkEntity).toHaveBeenCalledTimes(1);
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri3');
                        expect(searchProvider.requestEntities).toHaveBeenCalledTimes(1);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(5, 0);
                        expect(searchProvider.requestTotal).toHaveBeenCalledTimes(1);
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should catch and handle checkEntity error when checking previous entity', function () { return __awaiter(void 0, void 0, void 0, function () {
            var warnSpy, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        warnSpy = jest.spyOn(global.console, 'warn');
                        searchProvider.checkEntity.mockRejectedValue({ errorCode: 403, errorMessage: 'Access is denied' });
                        result = setUp().result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(warnSpy).toHaveBeenCalledWith("Can't load previous page of search", {
                            errorCode: 403,
                            errorMessage: 'Access is denied'
                        });
                        warnSpy.mockRestore();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should catch and handle requestTotal error when requesting previous entities page', function () { return __awaiter(void 0, void 0, void 0, function () {
            var warnSpy, searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        warnSpy = jest.spyOn(global.console, 'warn');
                        searchProvider.requestTotal.mockRejectedValue({ errorCode: 403, errorMessage: 'Access is denied' });
                        searchNavigationData = {
                            uri: 'uri3',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 3,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri3' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(warnSpy).toHaveBeenCalledWith('Failed to load total count of entities', {
                            errorCode: 403,
                            errorMessage: 'Access is denied'
                        });
                        warnSpy.mockRestore();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should catch and handle requestEntities error when requesting previous entities page', function () { return __awaiter(void 0, void 0, void 0, function () {
            var warnSpy, searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        warnSpy = jest.spyOn(global.console, 'warn');
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchProvider.requestEntities.mockRejectedValue({ errorCode: 403, errorMessage: 'Access is denied' });
                        searchNavigationData = {
                            uri: 'uri3',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 5,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri3' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onPrev();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(warnSpy).toHaveBeenCalledWith("Can't load previous page of search", {
                            errorCode: 403,
                            errorMessage: 'Access is denied'
                        });
                        warnSpy.mockRestore();
                        return [2 /*return*/];
                }
            });
        }); });
    });
    describe('onNext behavior', function () {
        it('should do nothing if cache or uri in searchNavigationData are not specified', function () {
            var searchNavigationData = __assign(__assign({}, defaultMdmValues.searchNavigationData), { uri: null });
            var mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData });
            var result = setUp({ mdmValues: mdmValues }).result;
            act(function () {
                result.current.onNext();
            });
            expect(mdmActions.updateSearchNavigationDataFields).not.toHaveBeenCalled();
            expect(mdmActions.openEntity).not.toHaveBeenCalled();
            expect(searchProvider.checkEntity).not.toHaveBeenCalled();
            expect(searchProvider.requestEntities).not.toHaveBeenCalled();
            expect(searchProvider.requestTotal).not.toHaveBeenCalled();
        });
        it('should get next entity uri from cache, check it and if it exists update navigation data', function () { return __awaiter(void 0, void 0, void 0, function () {
            var result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        result = setUp().result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(1);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            index: defaultMdmValues.searchNavigationData.index + 1,
                            uri: 'uri2',
                            cache: defaultMdmValues.searchNavigationData.cache,
                            total: defaultMdmValues.searchNavigationData.total
                        });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri2', source: 'navigation' });
                        expect(searchProvider.checkEntity).toHaveBeenCalledTimes(1);
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri2');
                        expect(searchProvider.requestEntities).not.toHaveBeenCalled();
                        expect(searchProvider.requestTotal).not.toHaveBeenCalled();
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should set isPrevDisabled and isNextDisabled props as true during loading process', function () { return __awaiter(void 0, void 0, void 0, function () {
            var result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        result = setUp().result;
                        expect(result.current.isNextDisabled).toBe(false);
                        expect(result.current.isPrevDisabled).toBe(false);
                        act(function () {
                            result.current.onNext();
                        });
                        expect(result.current.isNextDisabled).toBe(true);
                        expect(result.current.isPrevDisabled).toBe(true);
                        expect(updateEntitiesNavigationLoadingState).toHaveBeenCalledWith(true);
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isPrevDisabled).toBe(false); })];
                    case 2:
                        _a.sent();
                        expect(updateEntitiesNavigationLoadingState).toHaveBeenCalledWith(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should get next entity uri from cache, check it and, if it does not exist, try to check the entity after the next', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.checkEntity.mockResolvedValueOnce(false);
                        searchProvider.checkEntity.mockResolvedValueOnce(true);
                        searchNavigationData = {
                            uri: 'uri0',
                            cache: ['uri0', 'uri1', 'uri2'],
                            index: 3,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri0' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(1);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            index: searchNavigationData.index + 1,
                            uri: 'uri2',
                            cache: ['uri0', 'uri2'],
                            total: searchNavigationData.total - 1
                        });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri2', source: 'navigation' });
                        expect(searchProvider.checkEntity).toHaveBeenCalledTimes(2);
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri1');
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri2');
                        expect(searchProvider.requestEntities).not.toHaveBeenCalled();
                        expect(searchProvider.requestTotal).not.toHaveBeenCalled();
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should stop process of checking entities if there are no more entities (index is more or equal than total - 1)', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.checkEntity.mockResolvedValue(false);
                        searchNavigationData = {
                            uri: 'uri3',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 3,
                            total: 6,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri3' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(1);
                        expect(mdmActions.openEntity).not.toHaveBeenCalled();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            index: 3,
                            cache: ['uri3'],
                            total: searchNavigationData.total - 2
                        });
                        expect(searchProvider.checkEntity).toHaveBeenCalledTimes(2);
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri4');
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri5');
                        expect(searchProvider.requestEntities).not.toHaveBeenCalled();
                        expect(searchProvider.requestTotal).not.toHaveBeenCalled();
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should request next entities page and update total if there are no entities in cache after current', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.requestEntities.mockResolvedValue([{ uri: 'uri6' }, { uri: 'uri7' }, { uri: 'uri8' }]);
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchNavigationData = {
                            uri: 'uri5',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 5,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri5' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(2);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ total: 11 });
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            uri: 'uri6',
                            index: searchNavigationData.index + 1,
                            cache: ['uri3', 'uri4', 'uri5', 'uri6', 'uri7', 'uri8']
                        });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri6', source: 'navigation' });
                        expect(searchProvider.checkEntity).not.toHaveBeenCalled();
                        expect(searchProvider.requestEntities).toHaveBeenCalledTimes(1);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(REQUEST_PAGE_SIZE, 6);
                        expect(searchProvider.requestTotal).toHaveBeenCalledTimes(1);
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not add duplicates to cache if next entities page contains them', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.requestEntities.mockResolvedValue([{ uri: 'uri5' }, { uri: 'uri6' }, { uri: 'uri7' }]);
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchNavigationData = {
                            uri: 'uri5',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 5,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri5' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(2);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ total: 11 });
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            uri: 'uri6',
                            index: searchNavigationData.index + 2,
                            cache: ['uri3', 'uri4', 'uri5', 'uri6', 'uri7']
                        });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri6', source: 'navigation' });
                        expect(searchProvider.checkEntity).not.toHaveBeenCalled();
                        expect(searchProvider.requestEntities).toHaveBeenCalledTimes(1);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(REQUEST_PAGE_SIZE, 6);
                        expect(searchProvider.requestTotal).toHaveBeenCalledTimes(1);
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should request additional page if loaded entities page does not contain new entities', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.requestEntities.mockResolvedValueOnce([{ uri: 'uri3' }, { uri: 'uri4' }, { uri: 'uri5' }]);
                        searchProvider.requestEntities.mockResolvedValueOnce([{ uri: 'uri5' }, { uri: 'uri6' }, { uri: 'uri7' }]);
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchNavigationData = {
                            uri: 'uri5',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 5,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri5' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(3);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ total: 11 });
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            uri: 'uri6',
                            index: 10,
                            cache: ['uri3', 'uri4', 'uri5', 'uri6', 'uri7']
                        });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri6', source: 'navigation' });
                        expect(searchProvider.checkEntity).not.toHaveBeenCalled();
                        expect(searchProvider.requestEntities).toHaveBeenCalledTimes(2);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(REQUEST_PAGE_SIZE, 6);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(REQUEST_PAGE_SIZE, 9);
                        expect(searchProvider.requestTotal).toHaveBeenCalledTimes(2);
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should stop request additional pages if there are no more new entities according index', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.requestEntities.mockResolvedValueOnce([{ uri: 'uri3' }, { uri: 'uri4' }, { uri: 'uri5' }]);
                        searchProvider.requestEntities.mockResolvedValueOnce([{ uri: 'uri3' }, { uri: 'uri4' }, { uri: 'uri5' }]);
                        searchProvider.requestEntities.mockResolvedValueOnce([]);
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchNavigationData = {
                            uri: 'uri5',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 5,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri5' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(3);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ total: 11 });
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            index: searchNavigationData.total - 1
                        });
                        expect(searchProvider.checkEntity).not.toHaveBeenCalled();
                        expect(searchProvider.requestEntities).toHaveBeenCalledTimes(2);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(REQUEST_PAGE_SIZE, 6);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(REQUEST_PAGE_SIZE, 9);
                        expect(searchProvider.requestTotal).toHaveBeenCalledTimes(2);
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should request next entities page after unsuccessful entity checking if there are no more entities in cache', function () { return __awaiter(void 0, void 0, void 0, function () {
            var searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        searchProvider.checkEntity.mockResolvedValueOnce(false);
                        searchProvider.requestEntities.mockResolvedValueOnce([{ uri: 'uri6' }, { uri: 'uri7' }, { uri: 'uri8' }]);
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchNavigationData = {
                            uri: 'uri4',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 6,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri4' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledTimes(2);
                        expect(mdmActions.openEntity).toHaveBeenCalledTimes(1);
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({ total: 11 });
                        expect(mdmActions.updateSearchNavigationDataFields).toHaveBeenCalledWith({
                            uri: 'uri6',
                            index: 7,
                            cache: ['uri3', 'uri4', 'uri6', 'uri7', 'uri8']
                        });
                        expect(mdmActions.openEntity).toHaveBeenCalledWith({ uri: 'uri6', source: 'navigation' });
                        expect(searchProvider.checkEntity).toHaveBeenCalledTimes(1);
                        expect(searchProvider.checkEntity).toHaveBeenCalledWith('uri5');
                        expect(searchProvider.requestEntities).toHaveBeenCalledTimes(1);
                        expect(searchProvider.requestEntities).toHaveBeenCalledWith(REQUEST_PAGE_SIZE, 7);
                        expect(searchProvider.requestTotal).toHaveBeenCalledTimes(1);
                        expect(result.current.isPrevDisabled).toBe(false);
                        expect(result.current.isNextDisabled).toBe(false);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should catch and handle checkEntity error when checking next entity', function () { return __awaiter(void 0, void 0, void 0, function () {
            var warnSpy, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        warnSpy = jest.spyOn(global.console, 'warn');
                        searchProvider.checkEntity.mockRejectedValue({ errorCode: 403, errorMessage: 'Access is denied' });
                        result = setUp().result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(warnSpy).toHaveBeenCalledWith("Can't load next page of search", {
                            errorCode: 403,
                            errorMessage: 'Access is denied'
                        });
                        warnSpy.mockRestore();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should catch and handle requestTotal error when requesting next entities page', function () { return __awaiter(void 0, void 0, void 0, function () {
            var warnSpy, searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        warnSpy = jest.spyOn(global.console, 'warn');
                        searchProvider.requestTotal.mockRejectedValue({ errorCode: 403, errorMessage: 'Access is denied' });
                        searchNavigationData = {
                            uri: 'uri5',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 5,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri5' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(warnSpy).toHaveBeenCalledWith('Failed to load total count of entities', {
                            errorCode: 403,
                            errorMessage: 'Access is denied'
                        });
                        warnSpy.mockRestore();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should catch and handle requestEntities error when requesting next entities page', function () { return __awaiter(void 0, void 0, void 0, function () {
            var warnSpy, searchNavigationData, entity, mdmValues, result;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        warnSpy = jest.spyOn(global.console, 'warn');
                        searchProvider.requestTotal.mockResolvedValue(11);
                        searchProvider.requestEntities.mockRejectedValue({ errorCode: 403, errorMessage: 'Access is denied' });
                        searchNavigationData = {
                            uri: 'uri5',
                            cache: ['uri3', 'uri4', 'uri5'],
                            index: 5,
                            total: 10,
                            tenant: 't1'
                        };
                        entity = { uri: 'uri5' };
                        mdmValues = __assign(__assign({}, defaultMdmValues), { searchNavigationData: searchNavigationData, entity: entity });
                        result = setUp({ mdmValues: mdmValues }).result;
                        act(function () {
                            result.current.onNext();
                        });
                        return [4 /*yield*/, waitFor(function () { return expect(result.current.isNextDisabled).toBe(false); })];
                    case 1:
                        _a.sent();
                        expect(warnSpy).toHaveBeenCalledWith("Can't load next page of search", {
                            errorCode: 403,
                            errorMessage: 'Access is denied'
                        });
                        warnSpy.mockRestore();
                        return [2 /*return*/];
                }
            });
        }); });
    });
});
