Skip to content

Commit

Permalink
[license] Correctly throw errors (#10924)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari authored Nov 7, 2023
1 parent 947619d commit 42dd75d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,37 @@ import {
import { sharedLicenseStatuses } from './useLicenseVerifier';
import { generateReleaseInfo } from '../verifyLicense';

const oneDayInMS = 1000 * 60 * 60 * 24;
const releaseDate = new Date(3000, 0, 0, 0, 0, 0, 0);
const releaseInfo = generateReleaseInfo(releaseDate);
const RELEASE_INFO = generateReleaseInfo(releaseDate);

function TestComponent() {
const licesenStatus = useLicenseVerifier('x-date-pickers-pro', releaseInfo);
const licesenStatus = useLicenseVerifier('x-date-pickers-pro', RELEASE_INFO);
return <div data-testid="status">Status: {licesenStatus.status}</div>;
}

describe('useLicenseVerifier', () => {
describe('useLicenseVerifier', function test() {
// Can't change the process.env.NODE_ENV in Karma
if (!/jsdom/.test(window.navigator.userAgent)) {
return;
}

const { render } = createRenderer();

let env: any;
beforeEach(() => {
env = process.env.NODE_ENV;
// Avoid Karma "Invalid left-hand side in assignment" SyntaxError
// eslint-disable-next-line no-useless-concat
process.env['NODE_' + 'ENV'] = 'test';
});

afterEach(() => {
// Avoid Karma "Invalid left-hand side in assignment" SyntaxError
// eslint-disable-next-line no-useless-concat
process.env['NODE_' + 'ENV'] = env;
});

describe('error', () => {
beforeEach(() => {
Object.keys(sharedLicenseStatuses).forEach((key) => {
Expand Down Expand Up @@ -56,5 +76,33 @@ describe('useLicenseVerifier', () => {

expect(screen.getByTestId('status')).to.have.text('Status: Valid');
});

it('should throw if the license is expired by more than a 30 days', () => {
// Avoid Karma "Invalid left-hand side in assignment" SyntaxError
// eslint-disable-next-line no-useless-concat
process.env['NODE_' + 'ENV'] = 'development';

const expiredLicenseKey = generateLicense({
expiryDate: new Date(new Date().getTime() - oneDayInMS * 30),
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'subscription',
});
LicenseInfo.setLicenseKey(expiredLicenseKey);

let actualErrorMsg;
expect(() => {
try {
render(<TestComponent />);
} catch (error: any) {
actualErrorMsg = error.message;
}
}).to.toErrorDev([
'MUI: Expired license key',
'MUI: Expired license key',
'The above error occurred in the <TestComponent> component',
]);
expect(actualErrorMsg).to.match(/MUI: Expired license key/);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export function useLicenseVerifier(
acceptedScopes,
});

sharedLicenseStatuses[packageName] = { key: licenseKey, licenseVerifier: licenseStatus };
const fullPackageName = `@mui/${packageName}`;

if (licenseStatus.status === LICENSE_STATUS.Valid) {
Expand All @@ -77,6 +76,7 @@ export function useLicenseVerifier(
throw new Error('missing status handler');
}

sharedLicenseStatuses[packageName] = { key: licenseKey, licenseVerifier: licenseStatus };
return licenseStatus;
}, [packageName, releaseInfo, contextKey]);
}

0 comments on commit 42dd75d

Please sign in to comment.