"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
var __webpack_exports__ = {};
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const client_namespaceObject = require("@tachybase/test/client");
const external_axios_namespaceObject = require("axios");
var external_axios_default = /*#__PURE__*/ __webpack_require__.n(external_axios_namespaceObject);
const external_axios_mock_adapter_namespaceObject = require("axios-mock-adapter");
var external_axios_mock_adapter_default = /*#__PURE__*/ __webpack_require__.n(external_axios_mock_adapter_namespaceObject);
const external_react_router_dom_namespaceObject = require("react-router-dom");
const external_vitest_namespaceObject = require("vitest");
const external_Application_js_namespaceObject = require("../Application.js");
const external_Plugin_js_namespaceObject = require("../Plugin.js");
(0, external_vitest_namespaceObject.describe)('Application', ()=>{
    beforeAll(()=>{
        const mock = new (external_axios_mock_adapter_default())(external_axios_default());
        mock.onGet('pm:listEnabled').reply(200, {
            data: []
        });
    });
    const router = {
        type: 'memory',
        initialEntries: [
            '/'
        ]
    };
    const initialProvidersLength = 7;
    it('basic', ()=>{
        const app = new external_Application_js_namespaceObject.Application({
            router
        });
        expect(app.i18n).toBeDefined();
        expect(app.apiClient).toBeDefined();
        expect(app.components).toBeDefined();
        expect(app.pm).toBeDefined();
        expect(app.providers).toBeDefined();
        expect(app.router).toBeDefined();
        expect(app.scopes).toBeDefined();
        expect(app.providers.length).toBeGreaterThan(1);
        expect(Object.keys(app.components).length).toBeGreaterThan(1);
    });
    (0, external_vitest_namespaceObject.describe)('components', ()=>{
        const Hello = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                children: "Hello"
            });
        Hello.displayName = 'Hello';
        it('initial', ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router,
                components: {
                    Hello
                }
            });
            expect(app.components.Hello).toBe(Hello);
        });
        it('addComponents', ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router
            });
            app.addComponents({
                Hello
            });
            expect(app.components.Hello).toBe(Hello);
        });
        (0, external_vitest_namespaceObject.describe)('getComponent', ()=>{
            let originalConsoleError;
            beforeEach(()=>{
                originalConsoleError = console.error;
            });
            afterEach(()=>{
                console.error = originalConsoleError;
            });
            it('arg is Class component', ()=>{
                class Foo extends external_react_namespaceObject.Component {
                    render() {
                        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {});
                    }
                }
                const app = new external_Application_js_namespaceObject.Application({
                    router
                });
                expect(app.getComponent(Foo)).toBe(Foo);
            });
            it('arg is Function component', ()=>{
                const Foo = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {});
                const app = new external_Application_js_namespaceObject.Application({
                    router
                });
                expect(app.getComponent(Foo)).toBe(Foo);
            });
            it('arg is string', ()=>{
                const Foo = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {});
                const app = new external_Application_js_namespaceObject.Application({
                    router,
                    components: {
                        Foo
                    }
                });
                expect(app.getComponent('Foo')).toBe(Foo);
            });
            it('arg is string, but not found in components', ()=>{
                const app = new external_Application_js_namespaceObject.Application({
                    router
                });
                const fn = vitest.fn();
                console.error = fn;
                expect(app.getComponent('Foo')).toBeUndefined();
                expect(fn).toBeCalled();
            });
            it('arg is null or undefined', ()=>{
                const app = new external_Application_js_namespaceObject.Application({
                    router
                });
                const fn = vitest.fn();
                console.error = fn;
                expect(app.getComponent(null)).toBeUndefined();
                expect(fn).toBeCalled();
            });
            it('arg is other types', ()=>{
                const app = new external_Application_js_namespaceObject.Application({
                    router
                });
                const fn = vitest.fn();
                console.error = fn;
                expect(app.getComponent({})).toBeUndefined();
                expect(fn).toBeCalled();
            });
        });
        it('renderComponent', ()=>{
            const Foo = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    children: props.name
                });
            const app = new external_Application_js_namespaceObject.Application({
                router,
                components: {
                    Foo
                }
            });
            expect(app.renderComponent('Foo', {
                name: 'bar'
            })).toEqual(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Foo, {
                name: "bar"
            }));
        });
    });
    (0, external_vitest_namespaceObject.describe)('scopes', ()=>{
        it('initial', ()=>{
            const scopes = {
                foo: 'bar'
            };
            const app = new external_Application_js_namespaceObject.Application({
                router,
                scopes
            });
            expect(app.scopes).toEqual(scopes);
        });
        it('addScopes', ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router
            });
            app.addScopes({
                foo: 'bar'
            });
            expect(app.scopes).toEqual({
                foo: 'bar'
            });
        });
    });
    (0, external_vitest_namespaceObject.describe)('providers', ()=>{
        const Hello = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                children: [
                    "Hello ",
                    props.children
                ]
            });
        const World = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                        children: "World"
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                        children: props.name
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                        children: props.children
                    })
                ]
            });
        const Foo = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                children: [
                    "Foo ",
                    props.children
                ]
            });
        it('initial', ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router,
                providers: [
                    Hello,
                    [
                        World,
                        {
                            name: 'aaa'
                        }
                    ]
                ]
            });
            expect(app.providers.slice(initialProvidersLength)).toEqual([
                [
                    Hello,
                    void 0
                ],
                [
                    World,
                    {
                        name: 'aaa'
                    }
                ]
            ]);
        });
        it('addProviders', ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router,
                providers: [
                    Hello
                ]
            });
            app.addProviders([
                [
                    World,
                    {
                        name: 'aaa'
                    }
                ],
                Foo
            ]);
            expect(app.providers.slice(initialProvidersLength)).toEqual([
                [
                    Hello,
                    void 0
                ],
                [
                    World,
                    {
                        name: 'aaa'
                    }
                ],
                [
                    Foo,
                    void 0
                ]
            ]);
        });
        it('addProvider', ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router,
                providers: [
                    Hello
                ]
            });
            app.addProvider(World, {
                name: 'aaa'
            });
            expect(app.providers.slice(initialProvidersLength)).toEqual([
                [
                    Hello,
                    void 0
                ],
                [
                    World,
                    {
                        name: 'aaa'
                    }
                ]
            ]);
        });
        it('use', ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router,
                providers: [
                    Hello
                ]
            });
            app.use(World, {
                name: 'aaa'
            });
            expect(app.providers.slice(initialProvidersLength)).toEqual([
                [
                    Hello,
                    void 0
                ],
                [
                    World,
                    {
                        name: 'aaa'
                    }
                ]
            ]);
        });
        it('getComposeProviders', async ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router,
                providers: [
                    Hello,
                    [
                        World,
                        {
                            name: 'aaa'
                        }
                    ]
                ]
            });
            const Providers = app.getComposeProviders();
            (0, client_namespaceObject.render)(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Foo, {}), {
                wrapper: Providers
            });
            await (0, client_namespaceObject.waitFor)(()=>{
                expect(client_namespaceObject.screen.getByText('Hello')).toBeInTheDocument();
            });
            await (0, client_namespaceObject.waitFor)(()=>{
                expect(client_namespaceObject.screen.getByText('World')).toBeInTheDocument();
            });
            await (0, client_namespaceObject.waitFor)(()=>{
                expect(client_namespaceObject.screen.getByText('aaa')).toBeInTheDocument();
            });
            await (0, client_namespaceObject.waitFor)(()=>{
                expect(client_namespaceObject.screen.getByText('Foo')).toBeInTheDocument();
            });
        });
    });
    (0, external_vitest_namespaceObject.describe)('render', ()=>{
        it('getRootComponent', async ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router
            });
            const Layout = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                            children: [
                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_namespaceObject.Link, {
                                    to: "/",
                                    children: "Home"
                                }),
                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_namespaceObject.Link, {
                                    to: "/about",
                                    children: "About"
                                })
                            ]
                        }),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_namespaceObject.Outlet, {})
                    ]
                });
            const Home = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    children: "HomeComponent"
                });
            const About = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    children: "AboutComponent"
                });
            const HelloProvider = (param)=>{
                let { children } = param;
                return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                    children: [
                        "Hello ",
                        children
                    ]
                });
            };
            app.router.add('root', {
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Layout, {})
            });
            app.router.add('root.home', {
                path: '/',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Home, {})
            });
            app.router.add('root.about', {
                path: '/about',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(About, {})
            });
            app.addProviders([
                HelloProvider
            ]);
            const Root = app.getRootComponent();
            (0, client_namespaceObject.render)(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Root, {}));
            await (0, client_namespaceObject.waitFor)(()=>{
                expect(client_namespaceObject.screen.getByText('Loading...')).toBeInTheDocument();
            });
            await (0, client_namespaceObject.waitFor)(()=>{
                expect(client_namespaceObject.screen.getByText('HomeComponent')).toBeInTheDocument();
            });
            await client_namespaceObject.userEvent.click(client_namespaceObject.screen.getByText('About'));
            expect(client_namespaceObject.screen.getByText('AboutComponent')).toBeInTheDocument();
        });
        it('mount', async ()=>{
            const Hello = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    children: "Hello"
                });
            const app = new external_Application_js_namespaceObject.Application({
                router,
                providers: [
                    Hello
                ]
            });
            (0, client_namespaceObject.render)(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                id: "app"
            }));
            const root = app.mount('#app');
            expect(root).toBeDefined();
            await (0, client_namespaceObject.waitFor)(()=>{
                expect(client_namespaceObject.screen.getByText('Hello')).toBeInTheDocument();
            });
        });
        it('mount root error', ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router
            });
            const originalConsoleWarn = console.warn;
            const fn = vitest.fn();
            console.warn = fn;
            app.mount('#app');
            expect(fn).toBeCalled();
            console.warn = originalConsoleWarn;
        });
        it('plugin load error', async ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router
            });
            class DemoPlugin extends external_Plugin_js_namespaceObject.Plugin {
                async load() {
                    throw new Error('error');
                }
            }
            app.pm.add(DemoPlugin, {
                name: 'demo'
            });
            const Root = app.getRootComponent();
            (0, client_namespaceObject.render)(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Root, {}));
            await (0, client_namespaceObject.sleep)(10);
            expect(client_namespaceObject.screen.getByText('Load Plugin Error')).toBeInTheDocument();
        });
        it('replace Component', async ()=>{
            const AppSpin = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    children: "AppSpin"
                });
            const AppMain = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    children: "AppMain"
                });
            const app = new external_Application_js_namespaceObject.Application({
                router,
                components: {
                    AppSpin,
                    AppMain
                }
            });
            const Root = app.getRootComponent();
            (0, client_namespaceObject.render)(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Root, {}));
            expect(client_namespaceObject.screen.getByText('AppSpin')).toBeInTheDocument();
            await (0, client_namespaceObject.sleep)(10);
            expect(client_namespaceObject.screen.getByText('AppMain')).toBeInTheDocument();
        });
        it('render component error', async ()=>{
            const app = new external_Application_js_namespaceObject.Application({
                router
            });
            const AppError = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    children: "AppError"
                });
            const Foo = ()=>{
                throw new Error('error');
            };
            app.use(Foo);
            app.addComponents({
                AppError
            });
            const originalConsoleWarn = console.error;
            const fn = vitest.fn();
            console.error = fn;
            const Root = app.getRootComponent();
            (0, client_namespaceObject.render)(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Root, {}));
            await (0, client_namespaceObject.sleep)(10);
            expect(fn).toBeCalled();
            expect(client_namespaceObject.screen.getByText('AppError')).toBeInTheDocument();
            console.error = originalConsoleWarn;
        });
    });
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
