|
| 1 | +import React from 'react' |
| 2 | + |
| 3 | +import { lazyCreateConnectedComponent } from '../src' |
| 4 | +import { Shell } from '../src/API' |
| 5 | +import { addMockShell, collectAllTexts, connectWithShell, createAppHost, renderInHost } from '../testKit' |
| 6 | + |
| 7 | +function createFallbackTrackerComponent(label: string = 'loading...') { |
| 8 | + let handleMounted: () => void |
| 9 | + let handleUnmounted: () => void |
| 10 | + const mountedPromise = new Promise<void>(resolve => { |
| 11 | + handleMounted = resolve |
| 12 | + }) |
| 13 | + const unmountedPromise = new Promise<void>(resolve => { |
| 14 | + handleUnmounted = resolve |
| 15 | + }) |
| 16 | + |
| 17 | + const Fallback: React.FC = () => { |
| 18 | + React.useEffect(() => { |
| 19 | + handleMounted() |
| 20 | + return handleUnmounted |
| 21 | + }, []) |
| 22 | + return <div>{label}</div> |
| 23 | + } |
| 24 | + |
| 25 | + // NOTE: `Object.assign` is OK here, |
| 26 | + // so disable "prefer-object-spread" |
| 27 | + /* tslint:disable-next-line */ |
| 28 | + return Object.assign(Fallback, { |
| 29 | + LABEL: label, |
| 30 | + waitMounted: () => mountedPromise, |
| 31 | + waitUnmounted: () => unmountedPromise |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +const TEXT_IN_LAZY_COMPONENT = 'HELLO FROM LAZY COMPONENT' |
| 36 | + |
| 37 | +const lazyComponentFactoryMock = (() => { |
| 38 | + interface MyComponentStateProps { |
| 39 | + foo: string |
| 40 | + } |
| 41 | + |
| 42 | + interface MyComponentOwnProps { |
| 43 | + bar?: string |
| 44 | + } |
| 45 | + |
| 46 | + const MyComponent: React.FC<MyComponentStateProps & MyComponentOwnProps> = props => ( |
| 47 | + <div className="my-component"> |
| 48 | + {props.foo} |
| 49 | + <span>{props.bar}</span> |
| 50 | + </div> |
| 51 | + ) |
| 52 | + |
| 53 | + const createMyComponent = (boundShell: Shell) => |
| 54 | + connectWithShell<{}, MyComponentOwnProps, MyComponentStateProps>( |
| 55 | + () => ({ |
| 56 | + foo: TEXT_IN_LAZY_COMPONENT |
| 57 | + }), |
| 58 | + undefined, |
| 59 | + boundShell |
| 60 | + )(MyComponent) |
| 61 | + |
| 62 | + return { |
| 63 | + /** |
| 64 | + * represents a module with a named export |
| 65 | + * @example |
| 66 | + * ``` |
| 67 | + * export function createMyComponent(shell: Shell) {} |
| 68 | + * ``` |
| 69 | + */ |
| 70 | + loadWithNamedExport: async () => ({ |
| 71 | + createMyComponent |
| 72 | + }), |
| 73 | + /** |
| 74 | + * represents a module with a default export |
| 75 | + * @example |
| 76 | + * ``` |
| 77 | + * export default function createMyComponent(shell: Shell) {} |
| 78 | + * ``` |
| 79 | + */ |
| 80 | + loadWithDefaultExport: async () => ({ |
| 81 | + default: createMyComponent |
| 82 | + }) |
| 83 | + } |
| 84 | +})() |
| 85 | + |
| 86 | +describe.each([ |
| 87 | + ["with 'default export' module", () => lazyComponentFactoryMock.loadWithDefaultExport()], |
| 88 | + ["with 'named export' module", () => lazyComponentFactoryMock.loadWithNamedExport().then(module => module.createMyComponent)] |
| 89 | +])('lazyCreateConnectedComponent (loadComponentFactory %s)', (_, loadComponentFactory) => { |
| 90 | + it('should create and render connected component from lazy factory', async () => { |
| 91 | + const host = createAppHost([]) |
| 92 | + const boundShell = addMockShell(host) |
| 93 | + |
| 94 | + const MyComponent = lazyCreateConnectedComponent(boundShell, loadComponentFactory) |
| 95 | + |
| 96 | + const FallbackTracker = createFallbackTrackerComponent() |
| 97 | + |
| 98 | + const { parentWrapper } = renderInHost( |
| 99 | + <React.Suspense fallback={<FallbackTracker />}> |
| 100 | + <MyComponent /> |
| 101 | + </React.Suspense>, |
| 102 | + host |
| 103 | + ) |
| 104 | + |
| 105 | + // Check that the component shows the fallback, while the lazy component is loading |
| 106 | + expect(collectAllTexts(parentWrapper)).toContain(FallbackTracker.LABEL) |
| 107 | + expect(collectAllTexts(parentWrapper)).not.toContain(TEXT_IN_LAZY_COMPONENT) |
| 108 | + |
| 109 | + await FallbackTracker.waitUnmounted() |
| 110 | + |
| 111 | + // Check that the component shows the loaded content |
| 112 | + expect(collectAllTexts(parentWrapper)).not.toContain(FallbackTracker.LABEL) |
| 113 | + expect(collectAllTexts(parentWrapper)).toContain(TEXT_IN_LAZY_COMPONENT) |
| 114 | + }) |
| 115 | + |
| 116 | + it('should create and render connected componet with forwarding own props', async () => { |
| 117 | + const host = createAppHost([]) |
| 118 | + const boundShell = addMockShell(host) |
| 119 | + |
| 120 | + const MyComponent = lazyCreateConnectedComponent(boundShell, loadComponentFactory) |
| 121 | + |
| 122 | + const FallbackTracker = createFallbackTrackerComponent() |
| 123 | + |
| 124 | + const TEST_PROP = 'HELLO_FROM_PROPS' |
| 125 | + |
| 126 | + const { parentWrapper } = renderInHost( |
| 127 | + <React.Suspense fallback={<FallbackTracker />}> |
| 128 | + <MyComponent bar={TEST_PROP} /> |
| 129 | + </React.Suspense>, |
| 130 | + host |
| 131 | + ) |
| 132 | + |
| 133 | + expect(collectAllTexts(parentWrapper)).not.toContain(TEST_PROP) |
| 134 | + |
| 135 | + await FallbackTracker.waitUnmounted() |
| 136 | + |
| 137 | + expect(collectAllTexts(parentWrapper)).toContain(TEST_PROP) |
| 138 | + }) |
| 139 | + |
| 140 | + it('should call `loadComponentFactory` only after first render', async () => { |
| 141 | + const host = createAppHost([]) |
| 142 | + const boundShell = addMockShell(host) |
| 143 | + |
| 144 | + const loadComponentFactorySpy = jest.fn(() => loadComponentFactory()) |
| 145 | + |
| 146 | + const MyComponent = lazyCreateConnectedComponent(boundShell, loadComponentFactorySpy) |
| 147 | + |
| 148 | + expect(loadComponentFactorySpy).not.toHaveBeenCalled() |
| 149 | + |
| 150 | + renderInHost( |
| 151 | + <React.Suspense fallback={null}> |
| 152 | + <MyComponent /> |
| 153 | + </React.Suspense>, |
| 154 | + host |
| 155 | + ) |
| 156 | + |
| 157 | + expect(loadComponentFactorySpy).toHaveBeenCalled() |
| 158 | + }) |
| 159 | + |
| 160 | + it('should preload component with `loadComponentFactory` on `Component.preload` call', async () => { |
| 161 | + const host = createAppHost([]) |
| 162 | + const boundShell = addMockShell(host) |
| 163 | + |
| 164 | + const loadComponentFactorySpy = jest.fn(() => loadComponentFactory()) |
| 165 | + |
| 166 | + const MyComponent = lazyCreateConnectedComponent(boundShell, loadComponentFactorySpy) |
| 167 | + |
| 168 | + expect(loadComponentFactorySpy).not.toHaveBeenCalled() |
| 169 | + |
| 170 | + MyComponent.preload() |
| 171 | + |
| 172 | + expect(loadComponentFactorySpy).toHaveBeenCalled() |
| 173 | + |
| 174 | + renderInHost( |
| 175 | + <React.Suspense fallback={null}> |
| 176 | + <MyComponent /> |
| 177 | + </React.Suspense>, |
| 178 | + host |
| 179 | + ) |
| 180 | + |
| 181 | + expect(loadComponentFactorySpy).toHaveBeenCalledTimes(1) |
| 182 | + }) |
| 183 | +}) |
0 commit comments