diff --git a/web/jest.config.js b/web/jest.config.js index d59fbf90..5ec5e591 100644 --- a/web/jest.config.js +++ b/web/jest.config.js @@ -21,7 +21,7 @@ module.exports = { '/../test/fileTransformer.js', }, transformIgnorePatterns: ['/node_modules/'], - setupFiles: ['/../test/setup.js'], + setupFiles: ['/../test/setup.js', '/test/setup.js'], setupFilesAfterEnv: [], globals: { window: {}, diff --git a/web/src/components/__mocks__/Loadable.tsx b/web/src/components/__mocks__/Loadable.tsx new file mode 100644 index 00000000..042b6409 --- /dev/null +++ b/web/src/components/__mocks__/Loadable.tsx @@ -0,0 +1,23 @@ +import React from 'react'; + +// Reference: https://medium.com/pixel-and-ink/testing-loadable-components-with-jest-97bfeaa6da0b + +// Loadable components is tied to webpack, seems most people use webpack in their tests. +// Rather than that, we mock the loadable function to load the module eagarly and expose a load() function to be able to await the load +export function Loadable(load: any) { + let Component: any; + // Capture the component from the module load function + const loadPromise = load().then((val: any) => (Component = val.default)); + // Create a react component which renders the loaded component + const Loadable = (props: any) => { + if (!Component) { + throw new Error( + 'Bundle split module not loaded yet, ensure you beforeAll(() => MyLazyComponent.load()) in your test, import statement: ' + + load.toString() + ); + } + return ; + }; + Loadable.load = () => loadPromise; + return Loadable; +} diff --git a/web/src/components/modals/GroupPanel/ModifyGroupPanel.tsx b/web/src/components/modals/GroupPanel/ModifyGroupPanel.tsx index ecf54642..dfd9d07c 100644 --- a/web/src/components/modals/GroupPanel/ModifyGroupPanel.tsx +++ b/web/src/components/modals/GroupPanel/ModifyGroupPanel.tsx @@ -1,5 +1,5 @@ import { LoadingSpinner } from '@/components/LoadingSpinner'; -import React, { useEffect, useLayoutEffect, useState } from 'react'; +import React, { useState } from 'react'; import { t, useAsyncRequest, diff --git a/web/test/setup.js b/web/test/setup.js new file mode 100644 index 00000000..349c35d5 --- /dev/null +++ b/web/test/setup.js @@ -0,0 +1 @@ +jest.mock('../src/components/Loadable');