Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/calcite-components/src/tests/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
let globalError: jest.SpyInstance;
let globalLog: jest.SpyInstance;

beforeAll(() => {
globalError = jest.spyOn(global.console, "error");
globalLog = jest.spyOn(global.console, "info").mockImplementation(() => null);
});

beforeEach(() => globalError.mockClear());
beforeEach(() => {
globalError.mockClear();
globalLog.mockClear();
});

// eslint-disable-next-line jest/no-standalone-expect
afterEach(() => expect(globalError).not.toHaveBeenCalled());

afterAll(() => {
globalError.mockClear();
globalError.mockRestore();
globalLog.mockRestore();
});
17 changes: 13 additions & 4 deletions packages/calcite-components/src/utils/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ describe("config", () => {
describe("stampVersion", () => {
const calciteVersionPreBuildPlaceholder = "__CALCITE_VERSION__";

beforeEach(() => delete globalThis.calciteConfig);

it("creates global config and stamps the version onto it", async () => {
config = await loadConfig();
config.stampVersion();
Expand All @@ -43,12 +45,19 @@ describe("config", () => {
expect(globalThis.calciteConfig.version).toBe(calciteVersionPreBuildPlaceholder);
});

it("warns if the version is already set", async () => {
globalThis.calciteConfig = { version: "1.33.7" };
it("bails if version is already stamped onto existing config", async () => {
const testVersion = "1.33.7";
Comment thread
jcfranco marked this conversation as resolved.
globalThis.calciteConfig = { version: testVersion };
config = await loadConfig();
config.stampVersion();
expect(globalThis.calciteConfig.version).toBe(testVersion);
});

it("logs info with registered version", async () => {
expect(console.info).not.toHaveBeenCalled();
config = await loadConfig();
const warnSpy = jest.spyOn(console, "warn");
config.stampVersion();
expect(warnSpy).toHaveBeenCalled();
expect(console.info).toHaveBeenCalled();
});
});
});
18 changes: 10 additions & 8 deletions packages/calcite-components/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ export interface CalciteConfig {
version?: string;
}

const customConfig: CalciteConfig = globalThis["calciteConfig"];
const existingConfig: CalciteConfig = globalThis["calciteConfig"];

export const focusTrapStack: FocusTrap[] = customConfig?.focusTrapStack || [];
export const focusTrapStack: FocusTrap[] = existingConfig?.focusTrapStack || [];

const version = "__CALCITE_VERSION__"; // version number is set by build
// the following placeholders are replaced by the build
const version = "__CALCITE_VERSION__";
const buildDate = "__CALCITE_BUILD_DATE__";
const revision = "__CALCITE_REVISION__";

/**
* Stamp the version onto the global config.
*/
export function stampVersion(): void {
if (customConfig && customConfig.version) {
console.warn(
`[calcite-components] while initializing v${version}, an existing configuration with version "${customConfig.version}" was found. This may cause unexpected behavior. The version will not be added to the existing global configuration.`,
);
if (existingConfig && existingConfig.version) {
return;
}

const target = customConfig || globalThis["calciteConfig"] || {};
console.info(`Using Calcite Components ${version} [Date: ${buildDate}, Revision: ${revision}]`);

const target = existingConfig || globalThis["calciteConfig"] || {};

Object.defineProperty(target, "version", {
value: version,
Expand Down
3 changes: 3 additions & 0 deletions packages/calcite-components/stencil.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { execSync } from "child_process";
import { Config } from "@stencil/core";
import { postcss } from "@stencil-community/postcss";
import { sass } from "@stencil/sass";
Expand Down Expand Up @@ -146,6 +147,8 @@ export const create: () => Config = () => ({
before: [
replace({
values: {
__CALCITE_BUILD_DATE__: () => new Date().toISOString().split("T")[0],
__CALCITE_REVISION__: execSync("git rev-parse HEAD", { encoding: "utf-8" }).trim(),
__CALCITE_VERSION__: version,
},
include: ["src/utils/config.ts"],
Expand Down