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);
};
import React from 'react';
import { render, screen, act } from '@testing-library/react';
import { AsyncMountContext } from '../../contexts/AsyncMountContext';
import { withAsyncMount } from './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.createElement("div", { "data-reltio-id": value });
    };
    var Placeholder = function () { return React.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.createElement(AsyncMountContext.Provider, { value: __assign(__assign({}, defaultContextValue), contextValue) }, children));
        };
        var AsyncMountedComponent = withAsyncMount(Component, withPlaceholder ? Placeholder : undefined);
        return render(React.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(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(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(screen.queryByTestId('test-value')).not.toBeInTheDocument();
        act(function () {
            jest.runOnlyPendingTimers();
        });
        expect(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(screen.queryByTestId('test-value')).not.toBeInTheDocument();
        expect(screen.getByTestId('placeholder')).toBeInTheDocument();
        act(function () {
            jest.runOnlyPendingTimers();
        });
        expect(screen.getByTestId('test-value')).toBeInTheDocument();
        expect(screen.queryByTestId('placeholder')).not.toBeInTheDocument();
    });
    describe('queueSizeThreshold behavior', function () {
        it('should not use context value.queueSizeThreshold by default', function () {
            var AsyncMountedComponent = withAsyncMount(Component);
            render(React.createElement(AsyncMountContext.Provider, { value: { enabled: true, queueSizeThreshold: 0 } },
                React.createElement(AsyncMountedComponent, { value: "test-value" }),
                React.createElement(AsyncMountedComponent, { value: "test-value" })));
            expect(screen.queryAllByTestId('test-value').length).toBe(0);
            act(function () {
                jest.runOnlyPendingTimers();
            });
            expect(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 = withAsyncMount(Component);
            var Providers = function (_a) {
                var children = _a.children;
                return (React.createElement(AsyncMountContext.Provider, { value: { enabled: true, queueSizeThreshold: 2 } }, children));
            };
            render(React.createElement(React.Fragment, null,
                React.createElement(AsyncMountedComponent, { value: "test-value" }),
                React.createElement(AsyncMountedComponent, { value: "test-value" }),
                React.createElement(AsyncMountedComponent, { value: "test-value" })), { wrapper: Providers });
            expect(screen.queryAllByTestId('test-value').length).toBe(2);
            act(function () {
                jest.runOnlyPendingTimers();
            });
            expect(screen.queryAllByTestId('test-value').length).toBe(3);
        });
    });
});
