|
| 1 | +/* eslint-disable no-var */ |
| 2 | +import './overrides'; |
| 3 | +import { PluginManifest } from '@openshift/dynamic-plugin-sdk'; |
| 4 | +import { ScalprumProvider, ScalprumProviderProps, useScalprum } from '@scalprum/react-core'; |
| 5 | +import { fetch as fetchPolyfill } from 'whatwg-fetch'; |
| 6 | +import React, { useEffect } from 'react'; |
| 7 | +import { AppsConfig, getModuleIdentifier, getScalprum } from '@scalprum/core'; |
| 8 | + |
| 9 | +type SharedScope = Record<string, Record<string, { loaded?: 1; get: () => Promise<unknown>; from: string; eager: boolean }>>; |
| 10 | + |
| 11 | +declare global { |
| 12 | + var __webpack_share_scopes__: SharedScope; |
| 13 | + // var fetch: typeof fetchInternal; |
| 14 | +} |
| 15 | + |
| 16 | +export function mockWebpackShareScope() { |
| 17 | + const __webpack_share_scopes__: SharedScope = { |
| 18 | + default: {}, |
| 19 | + }; |
| 20 | + globalThis.__webpack_share_scopes__ = __webpack_share_scopes__; |
| 21 | +} |
| 22 | + |
| 23 | +export function mockFetch() { |
| 24 | + if (!globalThis.fetch) { |
| 25 | + globalThis.fetch = fetchPolyfill; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +export function mockScalprum() { |
| 30 | + mockWebpackShareScope(); |
| 31 | + mockFetch(); |
| 32 | +} |
| 33 | + |
| 34 | +type ModuleMock = { |
| 35 | + [importName: string]: React.ComponentType<any>; |
| 36 | +}; |
| 37 | + |
| 38 | +const ScalprumInitGate: React.ComponentType< |
| 39 | + React.PropsWithChildren<{ |
| 40 | + moduleMock: ModuleMock; |
| 41 | + pluginManifest: PluginManifest; |
| 42 | + moduleName: string; |
| 43 | + }> |
| 44 | +> = ({ children, moduleMock, pluginManifest, moduleName }) => { |
| 45 | + const scalprum = useScalprum(); |
| 46 | + const [mockReady, setMockReady] = React.useState(false); |
| 47 | + const { initialized } = scalprum; |
| 48 | + useEffect(() => { |
| 49 | + if (initialized && !mockReady) { |
| 50 | + const scalprum = getScalprum(); |
| 51 | + scalprum.exposedModules[getModuleIdentifier(pluginManifest.name, moduleName)] = moduleMock; |
| 52 | + setMockReady(true); |
| 53 | + } |
| 54 | + }, [initialized, mockReady]); |
| 55 | + if (!initialized || !mockReady) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + return <>{children}</>; |
| 60 | +}; |
| 61 | + |
| 62 | +export const DEFAULT_MODULE_TEST_ID = 'default-module-test-id'; |
| 63 | + |
| 64 | +export function mockPluginData( |
| 65 | + { |
| 66 | + headers = new Headers(), |
| 67 | + url = 'http://localhost:3000/test-plugin/plugin-manifest.json', |
| 68 | + type = 'default', |
| 69 | + ok = true, |
| 70 | + status = 200, |
| 71 | + statusText = 'OK', |
| 72 | + pluginManifest = { |
| 73 | + baseURL: 'http://localhost:3000', |
| 74 | + extensions: [], |
| 75 | + loadScripts: [], |
| 76 | + name: 'test-plugin', |
| 77 | + version: '1.0.0', |
| 78 | + registrationMethod: 'custom', |
| 79 | + }, |
| 80 | + module = 'ExposedModule', |
| 81 | + moduleMock = { |
| 82 | + default: () => <div data-testid={DEFAULT_MODULE_TEST_ID}>Default module</div>, |
| 83 | + }, |
| 84 | + config = { |
| 85 | + [pluginManifest.name]: { |
| 86 | + name: pluginManifest.name, |
| 87 | + manifestLocation: url, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }: { |
| 91 | + headers?: Headers; |
| 92 | + url?: string; |
| 93 | + type?: ResponseType; |
| 94 | + ok?: boolean; |
| 95 | + status?: number; |
| 96 | + statusText?: string; |
| 97 | + pluginManifest?: PluginManifest; |
| 98 | + module?: string; |
| 99 | + moduleMock?: ModuleMock; |
| 100 | + config?: AppsConfig; |
| 101 | + } = {}, |
| 102 | + api: ScalprumProviderProps['api'] = {} |
| 103 | +) { |
| 104 | + const response: Response = { |
| 105 | + blob: () => Promise.resolve(new Blob()), |
| 106 | + formData: () => Promise.resolve(new FormData()), |
| 107 | + headers, |
| 108 | + ok, |
| 109 | + redirected: false, |
| 110 | + status, |
| 111 | + statusText, |
| 112 | + url, |
| 113 | + type, |
| 114 | + body: null, |
| 115 | + bodyUsed: false, |
| 116 | + arrayBuffer: () => { |
| 117 | + return Promise.resolve(new ArrayBuffer(0)); |
| 118 | + }, |
| 119 | + text() { |
| 120 | + return Promise.resolve(JSON.stringify(pluginManifest)); |
| 121 | + }, |
| 122 | + json: () => { |
| 123 | + return Promise.resolve(pluginManifest); |
| 124 | + }, |
| 125 | + clone: () => response, |
| 126 | + }; |
| 127 | + |
| 128 | + const TestScalprumProvider: React.ComponentType<React.PropsWithChildren<{}>> = ({ children }) => { |
| 129 | + return ( |
| 130 | + <ScalprumProvider config={config} api={api}> |
| 131 | + <ScalprumInitGate moduleName={module} pluginManifest={pluginManifest} moduleMock={moduleMock}> |
| 132 | + {children} |
| 133 | + </ScalprumInitGate> |
| 134 | + </ScalprumProvider> |
| 135 | + ); |
| 136 | + }; |
| 137 | + |
| 138 | + return { response, TestScalprumProvider }; |
| 139 | +} |
| 140 | + |
| 141 | +mockScalprum(); |
0 commit comments