From 0a5354dc329d60b318aacc2fde46e9ce26399c82 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Tue, 22 Mar 2022 14:30:14 +0800 Subject: [PATCH] refactor(core): move browserContext and docusaurusContext out of client exports (#6955) --- jest.config.mjs | 4 +++- .../src/utils/__tests__/docsUtils.test.tsx | 2 +- .../src/utils/__tests__/generalUtils.test.tsx | 2 +- .../src/utils/__tests__/useAlternatePageUtils.test.tsx | 2 +- .../src/utils/__tests__/useLocalPathname.test.tsx | 2 +- .../src/utils/__tests__/usePluralForm.test.tsx | 2 +- packages/docusaurus/src/client/App.tsx | 4 ++-- .../client/{exports => }/__tests__/browserContext.test.tsx | 2 +- .../client/{exports => }/__tests__/docusaurusContext.test.tsx | 2 +- .../docusaurus/src/client/{exports => }/browserContext.tsx | 0 .../docusaurus/src/client/{exports => }/docusaurusContext.tsx | 0 .../src/client/exports/__tests__/BrowserOnly.test.tsx | 2 +- .../src/client/exports/__tests__/useBaseUrl.test.tsx | 2 +- .../src/client/exports/__tests__/useGlobalData.test.tsx | 2 +- .../docusaurus/src/client/exports/useDocusaurusContext.ts | 2 +- packages/docusaurus/src/client/exports/useIsBrowser.ts | 2 +- .../src/webpack/__tests__/__snapshots__/base.test.ts.snap | 4 ---- 17 files changed, 17 insertions(+), 19 deletions(-) rename packages/docusaurus/src/client/{exports => }/__tests__/browserContext.test.tsx (94%) rename packages/docusaurus/src/client/{exports => }/__tests__/docusaurusContext.test.tsx (95%) rename packages/docusaurus/src/client/{exports => }/browserContext.tsx (100%) rename packages/docusaurus/src/client/{exports => }/docusaurusContext.tsx (100%) diff --git a/jest.config.mjs b/jest.config.mjs index bd194757810d..c927d764c522 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -40,8 +40,10 @@ export default { '^.+\\.(css|jpe?g|png|svg|webp)$': '/jest/emptyModule.ts', // Using src instead of lib, so we always get fresh source - '@docusaurus/(browserContext|BrowserOnly|ComponentCreator|constants|docusaurusContext|ExecutionEnvironment|Head|Interpolate|isInternalUrl|Link|Noop|renderRoutes|router|Translate|use.*)': + '@docusaurus/(BrowserOnly|ComponentCreator|constants|ExecutionEnvironment|Head|Interpolate|isInternalUrl|Link|Noop|renderRoutes|router|Translate|use.*)': '@docusaurus/core/src/client/exports/$1', + + // TODO create dedicated testing utility for mocking contexts // Maybe point to a fixture? '@generated/.*': '/jest/emptyModule.ts', // TODO use "projects" + multiple configs if we work on another theme? diff --git a/packages/docusaurus-theme-common/src/utils/__tests__/docsUtils.test.tsx b/packages/docusaurus-theme-common/src/utils/__tests__/docsUtils.test.tsx index 8c76436c9cc5..f0cc92830c1e 100644 --- a/packages/docusaurus-theme-common/src/utils/__tests__/docsUtils.test.tsx +++ b/packages/docusaurus-theme-common/src/utils/__tests__/docsUtils.test.tsx @@ -20,7 +20,7 @@ import { useSidebarBreadcrumbs, } from '../docsUtils'; import {StaticRouter} from 'react-router-dom'; -import {Context} from '@docusaurus/docusaurusContext'; +import {Context} from '@docusaurus/core/src/client/docusaurusContext'; import type { PropSidebar, PropSidebarItem, diff --git a/packages/docusaurus-theme-common/src/utils/__tests__/generalUtils.test.tsx b/packages/docusaurus-theme-common/src/utils/__tests__/generalUtils.test.tsx index 661069efe4cc..71c49527abad 100644 --- a/packages/docusaurus-theme-common/src/utils/__tests__/generalUtils.test.tsx +++ b/packages/docusaurus-theme-common/src/utils/__tests__/generalUtils.test.tsx @@ -8,7 +8,7 @@ import React from 'react'; import {useTitleFormatter} from '../generalUtils'; import {renderHook} from '@testing-library/react-hooks'; -import {Context} from '@docusaurus/docusaurusContext'; +import {Context} from '@docusaurus/core/src/client/docusaurusContext'; import type {DocusaurusContext} from '@docusaurus/types'; describe('useTitleFormatter', () => { diff --git a/packages/docusaurus-theme-common/src/utils/__tests__/useAlternatePageUtils.test.tsx b/packages/docusaurus-theme-common/src/utils/__tests__/useAlternatePageUtils.test.tsx index 24f35e70b5ff..79ebd6cbca7f 100644 --- a/packages/docusaurus-theme-common/src/utils/__tests__/useAlternatePageUtils.test.tsx +++ b/packages/docusaurus-theme-common/src/utils/__tests__/useAlternatePageUtils.test.tsx @@ -9,7 +9,7 @@ import React from 'react'; import {useAlternatePageUtils} from '../useAlternatePageUtils'; import {renderHook} from '@testing-library/react-hooks'; import {StaticRouter} from 'react-router-dom'; -import {Context} from '@docusaurus/docusaurusContext'; +import {Context} from '@docusaurus/core/src/client/docusaurusContext'; import type {DocusaurusContext} from '@docusaurus/types'; describe('useAlternatePageUtils', () => { diff --git a/packages/docusaurus-theme-common/src/utils/__tests__/useLocalPathname.test.tsx b/packages/docusaurus-theme-common/src/utils/__tests__/useLocalPathname.test.tsx index c8298c5c640d..163e2f69bdb7 100644 --- a/packages/docusaurus-theme-common/src/utils/__tests__/useLocalPathname.test.tsx +++ b/packages/docusaurus-theme-common/src/utils/__tests__/useLocalPathname.test.tsx @@ -9,7 +9,7 @@ import React from 'react'; import {useLocalPathname} from '../useLocalPathname'; import {renderHook} from '@testing-library/react-hooks'; import {StaticRouter} from 'react-router-dom'; -import {Context} from '@docusaurus/docusaurusContext'; +import {Context} from '@docusaurus/core/src/client/docusaurusContext'; import type {DocusaurusContext} from '@docusaurus/types'; describe('useLocalPathname', () => { diff --git a/packages/docusaurus-theme-common/src/utils/__tests__/usePluralForm.test.tsx b/packages/docusaurus-theme-common/src/utils/__tests__/usePluralForm.test.tsx index d7ab24b9ebcb..13777535a6e4 100644 --- a/packages/docusaurus-theme-common/src/utils/__tests__/usePluralForm.test.tsx +++ b/packages/docusaurus-theme-common/src/utils/__tests__/usePluralForm.test.tsx @@ -9,7 +9,7 @@ import {jest} from '@jest/globals'; import React from 'react'; import {usePluralForm} from '../usePluralForm'; import {renderHook} from '@testing-library/react-hooks'; -import {Context} from '@docusaurus/docusaurusContext'; +import {Context} from '@docusaurus/core/src/client/docusaurusContext'; import type {DocusaurusContext} from '@docusaurus/types'; describe('usePluralForm', () => { diff --git a/packages/docusaurus/src/client/App.tsx b/packages/docusaurus/src/client/App.tsx index 6d434b2c6161..01d74bb1cdd3 100644 --- a/packages/docusaurus/src/client/App.tsx +++ b/packages/docusaurus/src/client/App.tsx @@ -9,8 +9,8 @@ import React from 'react'; import routes from '@generated/routes'; import renderRoutes from './exports/renderRoutes'; -import {BrowserContextProvider} from './exports/browserContext'; -import {DocusaurusContextProvider} from './exports/docusaurusContext'; +import {BrowserContextProvider} from './browserContext'; +import {DocusaurusContextProvider} from './docusaurusContext'; import PendingNavigation from './PendingNavigation'; import BaseUrlIssueBanner from './baseUrlIssueBanner/BaseUrlIssueBanner'; import SiteMetadataDefaults from './SiteMetadataDefaults'; diff --git a/packages/docusaurus/src/client/exports/__tests__/browserContext.test.tsx b/packages/docusaurus/src/client/__tests__/browserContext.test.tsx similarity index 94% rename from packages/docusaurus/src/client/exports/__tests__/browserContext.test.tsx rename to packages/docusaurus/src/client/__tests__/browserContext.test.tsx index 2802f91532f1..bfb8c211cf03 100644 --- a/packages/docusaurus/src/client/exports/__tests__/browserContext.test.tsx +++ b/packages/docusaurus/src/client/__tests__/browserContext.test.tsx @@ -12,7 +12,7 @@ import React from 'react'; import {renderHook} from '@testing-library/react-hooks/server'; import {BrowserContextProvider} from '../browserContext'; -import useIsBrowser from '../useIsBrowser'; +import useIsBrowser from '../exports/useIsBrowser'; describe('BrowserContextProvider', () => { const {result, hydrate} = renderHook(() => useIsBrowser(), { diff --git a/packages/docusaurus/src/client/exports/__tests__/docusaurusContext.test.tsx b/packages/docusaurus/src/client/__tests__/docusaurusContext.test.tsx similarity index 95% rename from packages/docusaurus/src/client/exports/__tests__/docusaurusContext.test.tsx rename to packages/docusaurus/src/client/__tests__/docusaurusContext.test.tsx index 1fa41bed0483..d11bb52a68aa 100644 --- a/packages/docusaurus/src/client/exports/__tests__/docusaurusContext.test.tsx +++ b/packages/docusaurus/src/client/__tests__/docusaurusContext.test.tsx @@ -12,7 +12,7 @@ import React from 'react'; import {renderHook} from '@testing-library/react-hooks/server'; import {DocusaurusContextProvider} from '../docusaurusContext'; -import useDocusaurusContext from '../useDocusaurusContext'; +import useDocusaurusContext from '../exports/useDocusaurusContext'; // This test currently isn't quite useful because the @generated aliases point // to the empty modules. Maybe we can point that to fixtures in the future. diff --git a/packages/docusaurus/src/client/exports/browserContext.tsx b/packages/docusaurus/src/client/browserContext.tsx similarity index 100% rename from packages/docusaurus/src/client/exports/browserContext.tsx rename to packages/docusaurus/src/client/browserContext.tsx diff --git a/packages/docusaurus/src/client/exports/docusaurusContext.tsx b/packages/docusaurus/src/client/docusaurusContext.tsx similarity index 100% rename from packages/docusaurus/src/client/exports/docusaurusContext.tsx rename to packages/docusaurus/src/client/docusaurusContext.tsx diff --git a/packages/docusaurus/src/client/exports/__tests__/BrowserOnly.test.tsx b/packages/docusaurus/src/client/exports/__tests__/BrowserOnly.test.tsx index 14a12b8d06e0..e836cfc855b2 100644 --- a/packages/docusaurus/src/client/exports/__tests__/BrowserOnly.test.tsx +++ b/packages/docusaurus/src/client/exports/__tests__/BrowserOnly.test.tsx @@ -12,7 +12,7 @@ import React from 'react'; import renderer from 'react-test-renderer'; import BrowserOnly from '../BrowserOnly'; -import {Context} from '../browserContext'; +import {Context} from '../../browserContext'; describe('', () => { const originalEnv = process.env; diff --git a/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.test.tsx b/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.test.tsx index 228a6cd483ae..4cca965759fb 100644 --- a/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.test.tsx +++ b/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.test.tsx @@ -8,7 +8,7 @@ import React from 'react'; import {renderHook} from '@testing-library/react-hooks'; import useBaseUrl, {useBaseUrlUtils} from '../useBaseUrl'; -import {Context} from '../docusaurusContext'; +import {Context} from '../../docusaurusContext'; import type {DocusaurusContext} from '@docusaurus/types'; import type {BaseUrlOptions} from '@docusaurus/useBaseUrl'; diff --git a/packages/docusaurus/src/client/exports/__tests__/useGlobalData.test.tsx b/packages/docusaurus/src/client/exports/__tests__/useGlobalData.test.tsx index 670a7e3bcc8f..3e5d370c4e0c 100644 --- a/packages/docusaurus/src/client/exports/__tests__/useGlobalData.test.tsx +++ b/packages/docusaurus/src/client/exports/__tests__/useGlobalData.test.tsx @@ -11,7 +11,7 @@ import useGlobalData, { useAllPluginInstancesData, usePluginData, } from '../useGlobalData'; -import {Context} from '../docusaurusContext'; +import {Context} from '../../docusaurusContext'; describe('useGlobalData', () => { it('returns global data from context', () => { diff --git a/packages/docusaurus/src/client/exports/useDocusaurusContext.ts b/packages/docusaurus/src/client/exports/useDocusaurusContext.ts index 132d8c0b4fe2..0f1cb4b3af9b 100644 --- a/packages/docusaurus/src/client/exports/useDocusaurusContext.ts +++ b/packages/docusaurus/src/client/exports/useDocusaurusContext.ts @@ -6,7 +6,7 @@ */ import {useContext} from 'react'; -import {Context} from './docusaurusContext'; +import {Context} from '../docusaurusContext'; import type {DocusaurusContext} from '@docusaurus/types'; export default function useDocusaurusContext(): DocusaurusContext { diff --git a/packages/docusaurus/src/client/exports/useIsBrowser.ts b/packages/docusaurus/src/client/exports/useIsBrowser.ts index e1e7caff51ed..ce1ebb739296 100644 --- a/packages/docusaurus/src/client/exports/useIsBrowser.ts +++ b/packages/docusaurus/src/client/exports/useIsBrowser.ts @@ -6,7 +6,7 @@ */ import {useContext} from 'react'; -import {Context} from './browserContext'; +import {Context} from '../browserContext'; export default function useIsBrowser(): boolean { return useContext(Context); diff --git a/packages/docusaurus/src/webpack/__tests__/__snapshots__/base.test.ts.snap b/packages/docusaurus/src/webpack/__tests__/__snapshots__/base.test.ts.snap index a0a080259f3d..cfad026638f5 100644 --- a/packages/docusaurus/src/webpack/__tests__/__snapshots__/base.test.ts.snap +++ b/packages/docusaurus/src/webpack/__tests__/__snapshots__/base.test.ts.snap @@ -11,9 +11,7 @@ exports[`base webpack config creates webpack aliases 1`] = ` "@docusaurus/Link": "../../../../client/exports/Link.tsx", "@docusaurus/Noop": "../../../../client/exports/Noop.ts", "@docusaurus/Translate": "../../../../client/exports/Translate.tsx", - "@docusaurus/browserContext": "../../../../client/exports/browserContext.tsx", "@docusaurus/constants": "../../../../client/exports/constants.ts", - "@docusaurus/docusaurusContext": "../../../../client/exports/docusaurusContext.tsx", "@docusaurus/isInternalUrl": "../../../../client/exports/isInternalUrl.ts", "@docusaurus/renderRoutes": "../../../../client/exports/renderRoutes.ts", "@docusaurus/router": "../../../../client/exports/router.ts", @@ -61,9 +59,7 @@ exports[`getDocusaurusAliases() returns appropriate webpack aliases 1`] = ` "@docusaurus/Link": "../../client/exports/Link.tsx", "@docusaurus/Noop": "../../client/exports/Noop.ts", "@docusaurus/Translate": "../../client/exports/Translate.tsx", - "@docusaurus/browserContext": "../../client/exports/browserContext.tsx", "@docusaurus/constants": "../../client/exports/constants.ts", - "@docusaurus/docusaurusContext": "../../client/exports/docusaurusContext.tsx", "@docusaurus/isInternalUrl": "../../client/exports/isInternalUrl.ts", "@docusaurus/renderRoutes": "../../client/exports/renderRoutes.ts", "@docusaurus/router": "../../client/exports/router.ts",