import React from 'react'; import { render } from '@testing-library/react'; import { initialWrapper, createRuntime, createApp, createPlugin } from '../src'; declare module '..' { interface RuntimeContext { test?: string; } } describe('create-app', () => { it('base, usage', () => { const runtime = createRuntime(); const wrap = initialWrapper([], runtime); interface Props { test: number; } function App({ test }: Props) { return
App:{test}
; } const AppWrapper = wrap(App, { context: {} as any }); const { container } = render(); expect(container.firstChild?.textContent).toEqual('App:1'); expect(container.innerHTML).toBe('
App:1
'); }); it('hoc', () => { const runtime = createRuntime(); const wrap = initialWrapper( [ runtime.createPlugin(() => ({ hoc: ({ App: App1 }) => ({ test }: Props) => , })), ], runtime, ); interface Props { test: number; } function App({ test }: Props) { return
App:{test}
; } const AppWrapper = wrap(App, { context: {} as any }); const { container } = render(); expect(container.firstChild?.textContent).toEqual('App:2'); expect(container.innerHTML).toBe('
App:2
'); }); it('provide', () => { const runtime = createRuntime(); const wrap = initialWrapper( [ runtime.createPlugin(() => ({ provide: ({ element }) =>
{element}
, })), ], runtime, ); interface Props { test: number; } function App({ test }: Props) { return
App:{test}
; } const AppWrapper = wrap(App, { context: {} as any }); const { container } = render(); expect(container.firstChild?.textContent).toEqual('App:1'); expect(container.innerHTML).toBe('
App:1
'); }); it('runtime context', () => { const runtime = createRuntime(); const wrap = initialWrapper( [ runtime.createPlugin(() => ({ provide: ({ element }) =>
{element}
, hoc: ({ App: App1 }, next) => next({ App: App1 }), client: ({ App: App1, rootElement }, next) => next({ App: App1, rootElement }), })), ], runtime, ); interface Props { test: number; } function App({ test }: Props) { return
App:{test}
; } const AppWrapper = wrap(App, {}); const { container } = render(); expect(container.firstChild?.textContent).toEqual('App:1'); expect(container.innerHTML).toBe('
App:1
'); }); it('createApp', () => { const wrap = createApp({ plugins: [ createPlugin(() => ({ provide: ({ element }) =>
{element}
, hoc: ({ App: App1 }, next) => next({ App: App1 }), client: ({ App: App1, rootElement }, next) => next({ App: App1, rootElement }), })), ], }); interface Props { test: number; } function App({ test }: Props) { return
App:{test}
; } const AppWrapper = wrap(App); const { container } = render(); expect(container.firstChild?.textContent).toEqual('App:1'); expect(container.innerHTML).toBe('
App:1
'); }); });