From ac189d75b5ee607e6f0e76e49b79c4f6472ec526 Mon Sep 17 00:00:00 2001 From: Lukas Date: Fri, 10 May 2024 15:28:07 +0300 Subject: [PATCH] [core] Refactor tests license setting (#13076) --- pnpm-lock.yaml | 3 +++ test/package.json | 1 + test/regressions/index.js | 9 +++------ test/utils/mochaHooks.js | 16 +++++++++------- test/utils/testLicense.js | 18 ++++++++++++++++++ 5 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 test/utils/testLicense.js diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2c6e7c2755832..5ab7762170c9f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1293,6 +1293,9 @@ importers: '@mui/x-date-pickers-pro': specifier: workspace:* version: link:../packages/x-date-pickers-pro/build + '@mui/x-license': + specifier: workspace:* + version: link:../packages/x-license/build '@playwright/test': specifier: ^1.43.1 version: 1.43.1 diff --git a/test/package.json b/test/package.json index d704642af68cc..166827da51207 100644 --- a/test/package.json +++ b/test/package.json @@ -14,6 +14,7 @@ "@mui/x-data-grid-pro": "workspace:*", "@mui/x-date-pickers": "workspace:*", "@mui/x-date-pickers-pro": "workspace:*", + "@mui/x-license": "workspace:*", "@react-spring/web": "^9.7.3", "@playwright/test": "^1.43.1", "@types/chai": "^4.3.16", diff --git a/test/regressions/index.js b/test/regressions/index.js index 50ae921bf03ed..c444cbddd7472 100644 --- a/test/regressions/index.js +++ b/test/regressions/index.js @@ -1,15 +1,12 @@ import * as React from 'react'; import * as ReactDOM from 'react-dom/client'; import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom'; -import { LicenseInfo } from '@mui/x-license'; import TestViewer from 'test/regressions/TestViewer'; import { useFakeTimers } from 'sinon'; import { Globals } from '@react-spring/web'; -// This license key is only valid for use with Material UI SAS's projects -// See the terms: https://mui.com/r/x-license-eula -LicenseInfo.setLicenseKey( - 'e43ff101678711136a9b81c18047cb69Tz1NVUktRG9jLEU9MTc0Njc4NzYxODIwMSxTPXByZW1pdW0sTE09c3Vic2NyaXB0aW9uLEtWPTI=', -); +import { setupTestLicenseKey } from '../utils/testLicense'; + +setupTestLicenseKey(); Globals.assign({ skipAnimation: true, diff --git a/test/utils/mochaHooks.js b/test/utils/mochaHooks.js index 766da79d50a2f..b5f113969c06e 100644 --- a/test/utils/mochaHooks.js +++ b/test/utils/mochaHooks.js @@ -1,9 +1,9 @@ import sinon from 'sinon'; -import { LicenseInfo } from '@mui/x-license'; import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingDataGrid } from '@mui/x-data-grid'; import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingDataGridPro } from '@mui/x-data-grid-pro'; import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingTreeView } from '@mui/x-tree-view'; import { clearWarningsCache } from '@mui/x-data-grid/internals'; +import { generateTestLicenseKey, setupTestLicenseKey } from './testLicense'; export function createXMochaHooks(coreMochaHooks = {}) { const mochaHooks = { @@ -13,12 +13,14 @@ export function createXMochaHooks(coreMochaHooks = {}) { afterEach: [...(coreMochaHooks.afterEach ?? [])], }; - mochaHooks.beforeEach.push(function setLicenseKey() { - // This license key is only valid for use with Material UI SAS's projects - // See the terms: https://mui.com/r/x-license-eula - LicenseInfo.setLicenseKey( - 'e43ff101678711136a9b81c18047cb69Tz1NVUktRG9jLEU9MTc0Njc4NzYxODIwMSxTPXByZW1pdW0sTE09c3Vic2NyaXB0aW9uLEtWPTI=', - ); + let licenseKey; + + mochaHooks.beforeAll.push(function func() { + licenseKey = generateTestLicenseKey(); + }); + + mochaHooks.beforeEach.push(function setupLicenseKey() { + setupTestLicenseKey(licenseKey); }); mochaHooks.afterEach.push(function resetCleanupTracking() { diff --git a/test/utils/testLicense.js b/test/utils/testLicense.js new file mode 100644 index 0000000000000..8a48c66f8b062 --- /dev/null +++ b/test/utils/testLicense.js @@ -0,0 +1,18 @@ +import { LicenseInfo, generateLicense } from '@mui/x-license'; + +export function generateTestLicenseKey() { + const expiryDate = new Date(); + // Set the expiry date to 1 hour from now just to be on the safe side. + // Tests usually take up to 15 minutes to run on CI. + expiryDate.setHours(expiryDate.getHours() + 1); + return generateLicense({ + licensingModel: 'subscription', + scope: 'premium', + orderNumber: 'MUI X tests', + expiryDate, + }); +} + +export function setupTestLicenseKey(licenseKey = generateTestLicenseKey()) { + LicenseInfo.setLicenseKey(licenseKey); +}