From 8cbeb2c5312227f548906c3c0db862c94deb8aa9 Mon Sep 17 00:00:00 2001 From: mathuo <{ID}+{username}@users.noreply.github.com> Date: Sun, 10 Oct 2021 16:18:43 +0100 Subject: [PATCH] test: add test --- .../src/__tests__/react/react.spec.tsx | 86 +++++++++++++++++++ packages/dockview/src/react/react.ts | 7 -- 2 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 packages/dockview/src/__tests__/react/react.spec.tsx diff --git a/packages/dockview/src/__tests__/react/react.spec.tsx b/packages/dockview/src/__tests__/react/react.spec.tsx new file mode 100644 index 000000000..4dd3109fb --- /dev/null +++ b/packages/dockview/src/__tests__/react/react.spec.tsx @@ -0,0 +1,86 @@ +import { ReactPart, ReactPortalStore } from '../../react/react'; +import * as React from 'react'; +import { render, fireEvent, waitFor, screen } from '@testing-library/react'; + +interface TestInterface { + valueA: string; + valueB: number; +} + +describe('react', () => { + describe('ReactPart', () => { + test('update underlying component via ReactPart class', () => { + let api: ReactPart; + + const onReady = (_api: ReactPart) => { + api = _api; + }; + + render(); + + expect(api).toBeTruthy(); + + expect(screen.getByTestId('valueA').textContent).toBe('stringA'); + expect(screen.getByTestId('valueB').textContent).toBe('42'); + + api.update({ valueB: '32' }); + + expect(screen.getByTestId('valueA').textContent).toBe('stringA'); + expect(screen.getByTestId('valueB').textContent).toBe('32'); + + api.update({ valueA: 'anotherStringA', valueB: '22' }); + + expect(screen.getByTestId('valueA').textContent).toBe( + 'anotherStringA' + ); + expect(screen.getByTestId('valueB').textContent).toBe('22'); + }); + }); +}); + +const Component = (props: TestInterface) => { + return ( +
+
{props.valueA}
+
{props.valueB}
+
+ ); +}; + +const TestWrapper = (props: { + component: React.FunctionComponent; + onReady: (api: ReactPart) => void; +}) => { + const [portal, setPortal] = React.useState([]); + const ref = React.useRef(null); + + React.useEffect(() => { + const cut = new ReactPart( + ref.current, + { + addPortal: (portal: React.ReactPortal) => { + setPortal((_) => [..._, portal]); + + return { + dispose: () => { + setPortal((_) => _.filter((_) => _ !== portal)); + }, + }; + }, + }, + props.component, + { + valueA: 'stringA', + valueB: 42, + } + ); + + props.onReady(cut); + + return () => { + cut.dispose(); + }; + }, []); + + return
{portal}
; +}; diff --git a/packages/dockview/src/react/react.ts b/packages/dockview/src/react/react.ts index 8035bd110..4024031af 100644 --- a/packages/dockview/src/react/react.ts +++ b/packages/dockview/src/react/react.ts @@ -49,13 +49,6 @@ const ReactComponentBridge: React.ForwardRefRenderFunction< [] ); - React.useEffect(() => { - // console.debug('[reactwrapper] component mounted '); - return () => { - // console.debug('[reactwrapper] component unmounted '); - }; - }, []); - return React.createElement(props.component, _props.current); }; ReactComponentBridge.displayName = 'DockviewReactJsBridge';