From 78cca16779402be461a1470efe99a37e98404dee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Fri, 19 Aug 2022 17:45:03 +0800 Subject: [PATCH] chore: default hashed (#37154) * chore: default hashed * test: try for clean default hashed * chore: clean up * chore: fix dist * chore: es inject * chore: no hashed in test env * chore: update comment * chore: hack in jest env * chore: try unique entry --- components/theme/export.ts | 5 ++++- components/theme/index.tsx | 10 +++++++--- tests/setupAfterEnv.ts | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/components/theme/export.ts b/components/theme/export.ts index 4968bdbfd6..1a6764f0fc 100644 --- a/components/theme/export.ts +++ b/components/theme/export.ts @@ -1,5 +1,5 @@ /* eslint-disable import/prefer-default-export */ -import { useToken as useInternalToken } from '.'; +import { useToken as useInternalToken, defaultConfig } from '.'; import defaultAlgorithm from './themes/default'; import darkAlgorithm from './themes/dark'; import { defaultAlgorithmV4, darkAlgorithmV4 } from './themes/v4'; @@ -15,6 +15,9 @@ function useToken() { } export default { + /** @private Test Usage. Do not use in production. */ + defaultConfig, + useToken, defaultAlgorithm, darkAlgorithm, diff --git a/components/theme/index.tsx b/components/theme/index.tsx index 175b473516..d6a43da52b 100644 --- a/components/theme/index.tsx +++ b/components/theme/index.tsx @@ -41,14 +41,18 @@ export type { }; // ================================ Context ================================= +// To ensure snapshot stable. We disable hashed in test env. +export const defaultConfig = { + token: defaultSeedToken, + hashed: true, +}; + export const DesignTokenContext = React.createContext<{ token: Partial; theme?: Theme; override?: OverrideToken; hashed?: string | boolean; -}>({ - token: defaultSeedToken, -}); +}>(defaultConfig); // ================================== Hook ================================== // In dev env, we refresh salt per hour to avoid user use this diff --git a/tests/setupAfterEnv.ts b/tests/setupAfterEnv.ts index 1bc9eb2caf..e908d5f976 100644 --- a/tests/setupAfterEnv.ts +++ b/tests/setupAfterEnv.ts @@ -1,4 +1,26 @@ import { toHaveNoViolations } from 'jest-axe'; import '@testing-library/jest-dom'; +import { defaultConfig } from '../components/theme/index'; + +// Not use dynamic hashed for test env since version will change hash dynamically. +defaultConfig.hashed = false; + +if (process.env.LIB_DIR === 'dist') { + jest.mock('../dist/antd', () => { + const antd = jest.requireActual('../dist/antd'); + antd.theme.defaultConfig.hashed = false; + + return antd; + }); +} else if (process.env.LIB_DIR === 'es') { + jest.mock('../es/theme', () => { + const esTheme = jest.requireActual('../es/theme'); + if (esTheme.defaultConfig) { + esTheme.defaultConfig.hashed = false; + } + + return esTheme; + }); +} expect.extend(toHaveNoViolations);