Skip to content

Commit

Permalink
Add tests for add and remove overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
robmosca committed Jul 9, 2023
1 parent 62fa638 commit 4ad0b68
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 2 deletions.
215 changes: 213 additions & 2 deletions src/api/js-api.imo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const localStorageMock = (function () {
let store = {};

return {
get length() {
return Object.keys(store).length;
},
key(index) {
return Object.keys(store)[index] || null;
},
getItem(key) {
return store[key] || null;
},
Expand All @@ -21,6 +27,8 @@ const localStorageMock = (function () {
})();

describe("window.importMapOverrides", () => {
let localStorageBackup;

const defaultMap = `<script type="importmap">
{
"imports": {
Expand All @@ -34,6 +42,15 @@ describe("window.importMapOverrides", () => {
beforeEach(() => {
jest.resetModules();
fetch.resetMocks();
localStorageBackup = window.localStorage;
Object.defineProperty(window, "localStorage", { value: localStorageMock });
window.localStorage.clear();
});

afterEach(() => {
Object.defineProperty(window, "localStorage", {
value: localStorageBackup,
});
});

const setDocumentAndLoadScript = (maps = [defaultMap]) => {
Expand Down Expand Up @@ -210,11 +227,11 @@ describe("window.importMapOverrides", () => {

it("should return return an override map when overrides are stored in the local storage", async () => {
await setDocumentAndLoadScript();
localStorage.setItem(
window.localStorage.setItem(
"import-map-override:package3",
"https://cdn.skypack.dev/package33"
);
localStorage.setItem(
window.localStorage.setItem(
"import-map-override:package4",
"https://cdn.skypack.dev/package4"
);
Expand All @@ -229,4 +246,198 @@ describe("window.importMapOverrides", () => {
});
});
});

describe("modify overrides", () => {
const changeEventListener = jest.fn();

beforeEach(() => {
window.addEventListener(
"import-map-overrides:change",
changeEventListener
);
});

afterEach(() => {
window.removeEventListener(
"import-map-overrides:change",
changeEventListener
);
});

async function assertChangeEventListenerIsCalled() {
// Verify that the event listener is called
await new Promise((resolve) =>
changeEventListener.mockImplementation(() => {
resolve();
})
);
}

describe("getDisabledOverrides", () => {
it("should return an empty array when there is no override", async () => {
await setDocumentAndLoadScript();
const disabledOverrides =
window.importMapOverrides.getDisabledOverrides();

expect(disabledOverrides).toEqual([]);
});

describe("disableOverride", () => {
it("should return an array of disabled overrides", async () => {
await setDocumentAndLoadScript();
window.importMapOverrides.disableOverride("package1");
window.importMapOverrides.disableOverride("package2");

const disabledOverrides =
window.importMapOverrides.getDisabledOverrides();

expect(disabledOverrides).toEqual(["package1", "package2"]);
await assertChangeEventListenerIsCalled();
});

describe("isDisabled", () => {
it("should return true if the override is disabled", async () => {
await setDocumentAndLoadScript();
window.importMapOverrides.disableOverride("package1");
expect(window.importMapOverrides.isDisabled("package1")).toEqual(
true
);
});

it("should return false if the override is not disabled", async () => {
await setDocumentAndLoadScript();
window.importMapOverrides.disableOverride("package1");
expect(window.importMapOverrides.isDisabled("package2")).toEqual(
false
);
await assertChangeEventListenerIsCalled();
});
});
});

describe("enableOverride", () => {
it("should enable an override", async () => {
await setDocumentAndLoadScript();
window.importMapOverrides.disableOverride("package1");
window.importMapOverrides.disableOverride("package2");
await window.importMapOverrides.enableOverride("package1");

const disabledOverrides =
window.importMapOverrides.getDisabledOverrides();

expect(disabledOverrides).toEqual(["package2"]);
await assertChangeEventListenerIsCalled();
});
});
});

describe("addOverride", () => {
it("should add an override", async () => {
await setDocumentAndLoadScript();
const map1 = await window.importMapOverrides.addOverride(
"package3",
"https://cdn.skypack.dev/package33.js"
);

expect(map1).toEqual({
imports: {
package3: "https://cdn.skypack.dev/package33.js",
},
scopes: {},
});

const map2 = await window.importMapOverrides.addOverride(
"package4",
"https://cdn.skypack.dev/package4.js"
);

expect(map2).toEqual({
imports: {
package3: "https://cdn.skypack.dev/package33.js",
package4: "https://cdn.skypack.dev/package4.js",
},
scopes: {},
});

await assertChangeEventListenerIsCalled();
});

it("should add an override by specifying only the port number", async () => {
await setDocumentAndLoadScript();
const map = await window.importMapOverrides.addOverride(
"@demo/package33",
"8080"
);

expect(
localStorage.getItem("import-map-override:@demo/package33")
).toEqual("//localhost:8080/demo-package33.js");
expect(map).toEqual({
imports: {
"@demo/package33": "//localhost:8080/demo-package33.js",
},
scopes: {},
});
await assertChangeEventListenerIsCalled();
});
});

describe("removeOverride", () => {
it("should remove an override", async () => {
await setDocumentAndLoadScript();
await window.importMapOverrides.addOverride(
"package3",
"https://cdn.skypack.dev/package33.js"
);
await window.importMapOverrides.addOverride(
"package5",
"https://cdn.skypack.dev/package5.js"
);
const result = await window.importMapOverrides.removeOverride(
"package3"
);

expect(result).toBe(true);

const map = await window.importMapOverrides.getOverrideMap();
expect(map).toEqual({
imports: { package5: "https://cdn.skypack.dev/package5.js" },
scopes: {},
});

await assertChangeEventListenerIsCalled();
});

it("should remove an override and also remove it from the list of disabled overrides", async () => {
await setDocumentAndLoadScript();
await window.importMapOverrides.addOverride(
"package3",
"https://cdn.skypack.dev/package33.js"
);
await window.importMapOverrides.addOverride(
"package5",
"https://cdn.skypack.dev/package5.js"
);
window.importMapOverrides.disableOverride("package3");
expect(window.importMapOverrides.getDisabledOverrides()).toEqual([
"package3",
]);
const result = await window.importMapOverrides.removeOverride(
"package3"
);

expect(result).toBe(true);

const map = await window.importMapOverrides.getOverrideMap();
expect(map).toEqual({
imports: { package5: "https://cdn.skypack.dev/package5.js" },
scopes: {},
});

expect(window.importMapOverrides.getDisabledOverrides()).toEqual([]);

await assertChangeEventListenerIsCalled();
});
});
});
});
1 change: 1 addition & 0 deletions src/api/js-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ function init() {

function fireEvent(type) {
// Set timeout so that event fires after the change has totally finished

setTimeout(() => {
const eventType = `import-map-overrides:${type}`;
const event = canFireCustomEvents
Expand Down

0 comments on commit 4ad0b68

Please sign in to comment.