Skip to content

Commit

Permalink
remove old testkit (#240)
Browse files Browse the repository at this point in the history
* remove old testkit

* remove old testkit

* remove old testkit

* remove old testkit
  • Loading branch information
amiryonatan authored Oct 30, 2023
1 parent daf1cd9 commit 53d5985
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 903 deletions.
1 change: 0 additions & 1 deletion docs/3rd-party-licenses.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Repluggable uses these awesome languages, libraries, and tools:
- [react-redux](https://github.com/reduxjs/react-redux) - [license](https://github.com/reduxjs/react-redux/blob/master/LICENSE.md)
- [redux-devtools-extension](https://github.com/zalmoxisus/redux-devtools-extension) - [license](https://github.com/zalmoxisus/redux-devtools-extension/blob/master/LICENSE)
- [jest](https://github.com/facebook/jest) - [license](https://github.com/facebook/jest/blob/master/LICENSE)
- [enzyme](https://github.com/enzymejs/enzyme) - [license](https://github.com/enzymejs/enzyme/blob/master/LICENSE.md)
- [react-test-renderer](https://github.com/facebook/react/tree/main/packages/react-test-renderer) - [license](https://github.com/facebook/react/blob/master/LICENSE)
- [WebPack](https://github.com/webpack/webpack) - [license](https://github.com/webpack/webpack/blob/master/LICENSE)
- [husky](https://github.com/typicode/husky) - [license](https://github.com/typicode/husky/blob/master/LICENSE)
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@
"redux-devtools-extension": "^2.13.8"
},
"devDependencies": {
"@types/enzyme": "^3.10.7",
"@types/jest": "^26.0.15",
"@types/lodash": "^4.14.162",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/react-test-renderer": "^18.0.0",
"@types/webpack-env": "^1.15.3",
"case-sensitive-paths-webpack-plugin": "^2.3.0",
"enzyme": "^3.11.0",
"husky": "^4.3.0",
"jest": "^26.6.0",
"jest-teamcity-reporter": "^0.9.0",
Expand Down
2 changes: 1 addition & 1 deletion test/appHost.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
mockShellStateKey,
addMockShell,
emptyLoggerOptions
} from '../testKit/v2'
} from '../testKit'

import { AppHostAPI, AppHostServicesEntryPointName, AppHostServicesProvider } from '../src/appHostServices'
import { createCircularEntryPoints, createDirectCircularEntryPoints } from './appHost.mock'
Expand Down
2 changes: 1 addition & 1 deletion test/connectWithShell.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
withThrowOnError,
TOGGLE_MOCK_VALUE,
collectAllTexts
} from '../testKit/v2'
} from '../testKit'
import { ReactTestRenderer, act, create } from 'react-test-renderer'
import { AnyAction } from 'redux'
import { ObservedSelectorsMap, observeWithShell } from '../src'
Expand Down
2 changes: 1 addition & 1 deletion test/customExtensionSlot.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash'
import { EntryPoint, SlotKey, Shell, CustomExtensionSlot, CustomExtensionSlotHandler, ExtensionItem } from '../src'
import { createAppHost, addMockShell } from '../testKit/v2'
import { createAppHost, addMockShell } from '../testKit'

interface TestItem {
value: string
Expand Down
2 changes: 1 addition & 1 deletion test/errorBoundary.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash'
import React from 'react'
import { createAppHost, addMockShell, renderInHost, withConsoleErrors } from '../testKit/v2'
import { createAppHost, addMockShell, renderInHost, withConsoleErrors } from '../testKit'
import { ErrorBoundary } from '../src'
import { act } from 'react-test-renderer'

Expand Down
2 changes: 1 addition & 1 deletion test/extensionSlot.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SlotKey } from '../src'
import { addMockShell, createAppHost } from '../testKit/v2'
import { addMockShell, createAppHost } from '../testKit'
import { PrivateExtensionSlot, Shell, EntryPoint } from '../src/API'

describe('ExtensionSlot', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/interceptEntryPoints.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash'
import { EntryPoint, EntryPointInterceptor } from '../src/API'
import { interceptEntryPoints, interceptEntryPointsMap } from '../src/interceptEntryPoints'
import { createAppHost } from '../testKit/v2'
import { createAppHost } from '../testKit'

type LogSpy = jest.Mock<void, string[]>

Expand Down
2 changes: 1 addition & 1 deletion test/renderSlotComponents.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash'
import React, { FunctionComponent, PropsWithChildren } from 'react'
import { SlotKey, ReactComponentContributor, Shell } from '../src/API'
import { createAppHost, addMockShell, renderInHost, connectWithShell, SlotRenderer } from '../testKit/v2'
import { createAppHost, addMockShell, renderInHost, connectWithShell, SlotRenderer } from '../testKit'
import { Provider } from 'react-redux'
import { AnyAction, createStore } from 'redux'
import { act, create, ReactTestRenderer } from 'react-test-renderer'
Expand Down
2 changes: 1 addition & 1 deletion test/testKit.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
dependsOnMockPackageEntryPoint,
MockPublicAPI,
createAppHostAndWaitForLoading
} from '../testKit/v2'
} from '../testKit'

interface APIKeys {
[name: string]: AnyExtensionSlot
Expand Down
66 changes: 48 additions & 18 deletions testKit/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { mount, ReactWrapper } from 'enzyme'
import { create, act, ReactTestRenderer, ReactTestInstance, TestRendererOptions } from 'react-test-renderer'
import _ from 'lodash'
import React, { ReactElement } from 'react'
import { createRoot } from 'react-dom/client'
import { EntryPoint, ObservableState, PrivateShell, ShellBoundaryAspect } from '../src/API'
import { AnySlotKey, AppHost, AppMainView, createAppHost as _createAppHost, EntryPointOrPackage, Shell, SlotKey } from '../src/index'
import { ShellRenderer } from '../src/renderSlotComponents'
import { createShellLogger } from '../src/loggers'
import { emptyLoggerOptions } from './emptyLoggerOptions'

export { emptyLoggerOptions }
export { AppHost } from '../src/index'
export { connectWithShell, connectWithShellAndObserve } from '../src/connectWithShell'
export { SlotRenderer } from '../src/renderSlotComponents'
Expand Down Expand Up @@ -56,7 +58,7 @@ export const getPackagesDependencies = (
loadedEntryPoints.add(currEntryPoint.name)
packagesList.push(currEntryPoint)
const dependencies = currEntryPoint.getDependencyAPIs ? currEntryPoint.getDependencyAPIs() : []
const dependencyEntryPoints = dependencies.map(API => apiToEntryPoint.get(API.name))
const dependencyEntryPoints = dependencies.map((API: AnySlotKey) => apiToEntryPoint.get(API.name))
entryPointsQueue.push(..._.compact(dependencyEntryPoints))
}

Expand Down Expand Up @@ -116,31 +118,53 @@ export async function createAppHostAndWaitForLoading(packages: EntryPointOrPacka
return Promise.race([timeoutPromise, loadingPromise]).then(() => appHost)
}

export type RenderHostType = (host: AppHost) => { root: ReactWrapper | null; DOMNode: HTMLElement | null }
export const renderHost: RenderHostType = (host: AppHost) => {
const root = mount(<AppMainView host={host} />) as ReactWrapper
return { root, DOMNode: root && (root.getDOMNode() as HTMLElement) }
export const renderHost = (host: AppHost): ReactTestRenderer => {
let renderer: ReactTestRenderer | undefined
act(() => {
renderer = create(<AppMainView host={host} />)
})

return renderer as unknown as ReactTestRenderer
}

export interface WrappedComponent {
root: ReactWrapper | null
parentWrapper: ReactWrapper | null
DOMNode: HTMLElement | null
testKit: ReturnType<typeof create>
host: AppHost
parentWrapper: ReactTestInstance | undefined
}

export const renderDOMInHost = (reactElement: ReactElement<any>, host: AppHost = createAppHost([]), customShell?: Shell) => {
const shell = customShell || createShell(host)

const Component = (
<ShellRenderer host={host} shell={shell as PrivateShell} component={<div data-shell-in-host="true">{reactElement}</div>} key="" />
)

const container = document.body.querySelector('div')
const root = container && createRoot(container)
root?.render(Component)
}

export const renderInHost = (reactElement: ReactElement<any>, host: AppHost = createAppHost([]), customShell?: Shell): WrappedComponent => {
export const renderInHost = (
reactElement: ReactElement<any>,
host: AppHost = createAppHost([]),
customShell?: Shell,
options?: TestRendererOptions
): WrappedComponent => {
const shell = customShell || createShell(host)

const root = mount(
const Component = (
<ShellRenderer host={host} shell={shell as PrivateShell} component={<div data-shell-in-host="true">{reactElement}</div>} key="" />
)
let root: ReactTestRenderer | undefined
act(() => {
root = create(Component, options)
})

const parentWrapper = root.find('[data-shell-in-host="true"]')
const parentWrapper = root?.root.find(x => x.props['data-shell-in-host'])

return {
root,
DOMNode: parentWrapper.children().first().getDOMNode() as HTMLElement,
testKit: root as unknown as ReactTestRenderer,
parentWrapper,
host
}
Expand All @@ -158,7 +182,7 @@ export const addMockShell = (host: AppHost, entryPointOverrides: EntryPointOverr
{
name: _.uniqueId('__MOCK_SHELL_'),
...entryPointOverrides,
attach(_shell) {
attach(_shell: Shell) {
shell = _shell
if (entryPointOverrides.attach) {
entryPointOverrides.attach(_shell)
Expand All @@ -176,10 +200,10 @@ export const addMockShell = (host: AppHost, entryPointOverrides: EntryPointOverr
return false
}
}
const missing = dependencies.filter(key => !canGetAPI(key))
const missing = dependencies.filter((key: AnySlotKey) => !canGetAPI(key))
throw new Error(
`addMockShell: overridden entry point is not ready (missing dependency APIs?) host could not find: ${missing.map(
v => `"${v.name}"`
(v: AnySlotKey) => `"${v.name}"`
)}`
)
}
Expand Down Expand Up @@ -234,7 +258,7 @@ function createShell(host: AppHost): PrivateShell {
clearCache: _.noop,
getHostOptions: () => host.options,
log: createShellLogger(host, entryPoint),
wrapWithShellRenderer: component => component
wrapWithShellRenderer: (component: JSX.Element) => component
}
}

Expand All @@ -248,3 +272,9 @@ export function mockObservable<T>(value: T): ObservableState<T> {
}
}
}

export function collectAllTexts(instance: ReactTestInstance | undefined) {
return (instance
?.findAll(x => x.children?.some(child => typeof child === 'string'))
.flatMap(x => x.children.filter(child => typeof child === 'string')) || []) as string[]
}
Loading

0 comments on commit 53d5985

Please sign in to comment.