-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: importedModule helper to load modules in declarative way
- Loading branch information
Showing
16 changed files
with
218 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as React from 'react'; | ||
import {mount} from 'enzyme'; | ||
import {act} from "react-dom/test-utils"; | ||
|
||
import {importedModule, ImportedModule} from '../src/Module'; | ||
import {done} from "../src/loadable"; | ||
|
||
describe('Module Component', () => { | ||
|
||
describe('hoc', () => { | ||
it('should load component', async () => { | ||
const Component = importedModule(() => Promise.resolve((x: number) => x + 42)); | ||
|
||
const wrapper = mount(<Component fallback={"loading"}>{(fn) => <span>{fn(42)}</span>}</Component>); | ||
|
||
expect(wrapper.update().html()).toContain("loading"); | ||
|
||
await act(async () => { | ||
await done(); | ||
}); | ||
|
||
expect(wrapper.update().html()).toContain("84"); | ||
}); | ||
}); | ||
|
||
describe('component', () => { | ||
it('should load component', async () => { | ||
const importer = () => Promise.resolve((x: number) => x + 42); | ||
|
||
const wrapper = mount( | ||
<ImportedModule | ||
import={importer} | ||
fallback={"loading"} | ||
> | ||
{(fn) => <span>{fn(42)}</span>} | ||
</ImportedModule> | ||
); | ||
|
||
expect(wrapper.update().html()).toContain("loading"); | ||
|
||
await act(async () => { | ||
await done(); | ||
}); | ||
|
||
expect(wrapper.update().html()).toContain("84"); | ||
}); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as React from "react"; | ||
import { | ||
DefaultImport, | ||
FullImportModuleProps, | ||
HOCModuleType, | ||
ImportModuleProps, | ||
|
||
} from "./types"; | ||
import {useImported} from "./useImported"; | ||
import {getLoadable} from "./loadable"; | ||
|
||
|
||
export function ImportedModule<T>(props: FullImportModuleProps<T>) { | ||
const {error, loadable, imported: module} = useImported(props.import); | ||
if (error) { | ||
throw error; | ||
} | ||
if (module) { | ||
return props.children(module); | ||
} | ||
if (!props.fallback){ | ||
throw loadable.resolution; | ||
} | ||
return props.fallback as any; | ||
} | ||
|
||
export function importedModule<T>( | ||
loaderFunction: DefaultImport<T> | ||
): HOCModuleType<T> { | ||
const loadable = getLoadable(loaderFunction); | ||
|
||
const Module = ((props: ImportModuleProps<T>) => ( | ||
<ImportedModule | ||
{...props} | ||
import={loadable} | ||
fallback={props.fallback} | ||
/> | ||
)) as HOCModuleType<T>; | ||
|
||
Module.preload = () => { | ||
loadable.load().catch(() => ({})); | ||
|
||
return loadable.resolution; | ||
}; | ||
Module.done = loadable.resolution; | ||
|
||
return Module; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.