"use strict";
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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var react_2 = require("@testing-library/react");
var AsyncMountContext_1 = require("../../contexts/AsyncMountContext");
var withAsyncMount_1 = require("./withAsyncMount");
describe('withAsyncMount tests', function () {
    beforeAll(function () {
        jest.useFakeTimers();
    });
    var defaultContextValue = { enabled: true, queueSizeThreshold: 0 };
    var Component = function (_a) {
        var value = _a.value;
        return react_1.default.createElement("div", { "data-reltio-id": value });
    };
    var Placeholder = function () { return react_1.default.createElement("span", { "data-reltio-id": "placeholder" }); };
    var setUp = function (_a) {
        var _b = _a === void 0 ? {} : _a, _c = _b.contextValue, contextValue = _c === void 0 ? {} : _c, _d = _b.withProvider, withProvider = _d === void 0 ? true : _d, _e = _b.withPlaceholder, withPlaceholder = _e === void 0 ? false : _e;
        var Providers = function (_a) {
            var children = _a.children;
            return (react_1.default.createElement(AsyncMountContext_1.AsyncMountContext.Provider, { value: __assign(__assign({}, defaultContextValue), contextValue) }, children));
        };
        var AsyncMountedComponent = (0, withAsyncMount_1.withAsyncMount)(Component, withPlaceholder ? Placeholder : undefined);
        return (0, react_2.render)(react_1.default.createElement(AsyncMountedComponent, { value: "test-value" }), { wrapper: withProvider ? Providers : undefined });
    };
    it('should mount wrapped component in sync way without AsyncMountContext.Provider', function () {
        setUp({ withProvider: false });
        expect(react_2.screen.getByTestId('test-value')).toBeInTheDocument();
    });
    it('should mount wrapped component in sync way with AsyncMountContext.Provider if value.enabled = false', function () {
        setUp({ contextValue: { enabled: false } });
        expect(react_2.screen.getByTestId('test-value')).toBeInTheDocument();
    });
    it('should mount wrapped component in async way with AsyncMountContext.Provider if value.enabled = true', function () {
        setUp({ contextValue: { enabled: true } });
        expect(react_2.screen.queryByTestId('test-value')).not.toBeInTheDocument();
        (0, react_2.act)(function () {
            jest.runOnlyPendingTimers();
        });
        expect(react_2.screen.getByTestId('test-value')).toBeInTheDocument();
    });
    it('should render PlaceholderComponent instead of Component before mounting if its passed as second argument', function () {
        setUp({ withPlaceholder: true });
        expect(react_2.screen.queryByTestId('test-value')).not.toBeInTheDocument();
        expect(react_2.screen.getByTestId('placeholder')).toBeInTheDocument();
        (0, react_2.act)(function () {
            jest.runOnlyPendingTimers();
        });
        expect(react_2.screen.getByTestId('test-value')).toBeInTheDocument();
        expect(react_2.screen.queryByTestId('placeholder')).not.toBeInTheDocument();
    });
    describe('queueSizeThreshold behavior', function () {
        it('should not use context value.queueSizeThreshold by default', function () {
            var AsyncMountedComponent = (0, withAsyncMount_1.withAsyncMount)(Component);
            (0, react_2.render)(react_1.default.createElement(AsyncMountContext_1.AsyncMountContext.Provider, { value: { enabled: true, queueSizeThreshold: 0 } },
                react_1.default.createElement(AsyncMountedComponent, { value: "test-value" }),
                react_1.default.createElement(AsyncMountedComponent, { value: "test-value" })));
            expect(react_2.screen.queryAllByTestId('test-value').length).toBe(0);
            (0, react_2.act)(function () {
                jest.runOnlyPendingTimers();
            });
            expect(react_2.screen.queryAllByTestId('test-value').length).toBe(2);
        });
        it('should use async mount only after exceeding of "value.queueSizeThreshold" number of component mounts', function () {
            var AsyncMountedComponent = (0, withAsyncMount_1.withAsyncMount)(Component);
            var Providers = function (_a) {
                var children = _a.children;
                return (react_1.default.createElement(AsyncMountContext_1.AsyncMountContext.Provider, { value: { enabled: true, queueSizeThreshold: 2 } }, children));
            };
            (0, react_2.render)(react_1.default.createElement(react_1.default.Fragment, null,
                react_1.default.createElement(AsyncMountedComponent, { value: "test-value" }),
                react_1.default.createElement(AsyncMountedComponent, { value: "test-value" }),
                react_1.default.createElement(AsyncMountedComponent, { value: "test-value" })), { wrapper: Providers });
            expect(react_2.screen.queryAllByTestId('test-value').length).toBe(2);
            (0, react_2.act)(function () {
                jest.runOnlyPendingTimers();
            });
            expect(react_2.screen.queryAllByTestId('test-value').length).toBe(3);
        });
    });
});
