Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robmosca committed Jul 9, 2023
1 parent 4ad0b68 commit dc2c48f
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 92 deletions.
298 changes: 206 additions & 92 deletions src/api/js-api.imo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe("window.importMapOverrides", () => {
});
});

describe("modify overrides", () => {
describe("Add/Remove/Enable/Disable overrides", () => {
const changeEventListener = jest.fn();

beforeEach(() => {
Expand Down Expand Up @@ -276,89 +276,121 @@ describe("window.importMapOverrides", () => {
describe("getDisabledOverrides", () => {
it("should return an empty array when there is no override", async () => {
await setDocumentAndLoadScript();
const disabledOverrides =
window.importMapOverrides.getDisabledOverrides();

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

describe("disableOverride", () => {
it("should return an array of disabled overrides", async () => {
await setDocumentAndLoadScript();
window.importMapOverrides.disableOverride("package1");
window.importMapOverrides.disableOverride("package2");
it("should return an array of disabled overrides", async () => {
await setDocumentAndLoadScript();
window.localStorage.setItem(
"import-map-overrides-disabled",
JSON.stringify(["package3", "package4"])
);

const disabledOverrides =
window.importMapOverrides.getDisabledOverrides();
expect(window.importMapOverrides.getDisabledOverrides()).toEqual([
"package3",
"package4",
]);
});
});

expect(disabledOverrides).toEqual(["package1", "package2"]);
await assertChangeEventListenerIsCalled();
});
describe("isDisabled", () => {
it("should return true if the override is disabled", async () => {
await setDocumentAndLoadScript();
window.localStorage.setItem(
"import-map-overrides-disabled",
JSON.stringify(["package3"])
);

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();
});
});
expect(window.importMapOverrides.isDisabled("package3")).toEqual(true);
});

describe("enableOverride", () => {
it("should enable an override", async () => {
await setDocumentAndLoadScript();
window.importMapOverrides.disableOverride("package1");
window.importMapOverrides.disableOverride("package2");
await window.importMapOverrides.enableOverride("package1");
it("should return false if the override is not disabled", async () => {
await setDocumentAndLoadScript();
window.localStorage.setItem(
"import-map-overrides-disabled",
JSON.stringify(["package3"])
);

expect(window.importMapOverrides.isDisabled("package4")).toEqual(false);
});
});

const disabledOverrides =
window.importMapOverrides.getDisabledOverrides();
describe("disableOverride", () => {
it("should disable an override and return true if it wasn't disabled before", async () => {
await setDocumentAndLoadScript();
const result = await window.importMapOverrides.disableOverride(
"package3"
);

expect(disabledOverrides).toEqual(["package2"]);
await assertChangeEventListenerIsCalled();
});
expect(window.importMapOverrides.getDisabledOverrides()).toEqual([
"package3",
]);
expect(result).toEqual(true);
await assertChangeEventListenerIsCalled();
});

it("should maintain override disabled and return false if it was already disabled before", async () => {
await setDocumentAndLoadScript();
window.localStorage.setItem(
"import-map-overrides-disabled",
JSON.stringify(["package3"])
);
const result = await window.importMapOverrides.disableOverride(
"package3"
);

expect(window.importMapOverrides.getDisabledOverrides()).toEqual([
"package3",
]);
expect(result).toEqual(false);
});
});

describe("enableOverride", () => {
it("should re-enable a disabled override and return true", async () => {
await setDocumentAndLoadScript();
window.localStorage.setItem(
"import-map-overrides-disabled",
JSON.stringify(["package3"])
);
const result = await window.importMapOverrides.enableOverride(
"package3"
);

expect(window.importMapOverrides.getDisabledOverrides()).toEqual([]);
expect(result).toEqual(true);
await assertChangeEventListenerIsCalled();
});

it("should return false if override was not disabled before", async () => {
await setDocumentAndLoadScript();
const result = await window.importMapOverrides.enableOverride(
"package3"
);

expect(window.importMapOverrides.getDisabledOverrides()).toEqual([]);
expect(result).toEqual(false);
});
});

describe("addOverride", () => {
it("should add an override", async () => {
await setDocumentAndLoadScript();
const map1 = await window.importMapOverrides.addOverride(
const map = 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(localStorage.getItem("import-map-override:package3")).toEqual(
"https://cdn.skypack.dev/package33.js"
);

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

await assertChangeEventListenerIsCalled();
});

Expand All @@ -383,60 +415,142 @@ describe("window.importMapOverrides", () => {
});

describe("removeOverride", () => {
it("should remove an override", async () => {
it("should remove an existing 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"
window.localStorage.setItem(
"import-map-override:package3",
"https://cdn.skypack.dev/package33"
);
const result = await window.importMapOverrides.removeOverride(
"package3"
);

expect(localStorage.getItem("import-map-override:package3")).toEqual(
null
);
expect(result).toBe(true);
await assertChangeEventListenerIsCalled();
});
});

describe("resetOverrides", () => {
it("should remove all overrides", async () => {
await setDocumentAndLoadScript();
window.localStorage.setItem(
"import-map-override:package3",
"https://cdn.skypack.dev/package33"
);
window.localStorage.setItem(
"import-map-override:package4",
"https://cdn.skypack.dev/package4"
);
const map = await window.importMapOverrides.resetOverrides();

const map = await window.importMapOverrides.getOverrideMap();
expect(localStorage.getItem("import-map-override:package3")).toEqual(
null
);
expect(localStorage.getItem("import-map-override:package4")).toEqual(
null
);
expect(map).toEqual({
imports: { package5: "https://cdn.skypack.dev/package5.js" },
imports: {},
scopes: {},
});

await assertChangeEventListenerIsCalled();
});
});

it("should remove an override and also remove it from the list of disabled overrides", async () => {
describe("hasOverrides", () => {
it("should return true if there are 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"
window.localStorage.setItem(
"import-map-override:package3",
"https://cdn.skypack.dev/package33"
);

expect(result).toBe(true);
expect(window.importMapOverrides.hasOverrides()).toEqual(true);
});

const map = await window.importMapOverrides.getOverrideMap();
expect(map).toEqual({
imports: { package5: "https://cdn.skypack.dev/package5.js" },
scopes: {},
});
it("should return false if there are no overrides", async () => {
await setDocumentAndLoadScript();

expect(window.importMapOverrides.getDisabledOverrides()).toEqual([]);
expect(window.importMapOverrides.hasOverrides()).toEqual(false);
});
});
});

await assertChangeEventListenerIsCalled();
describe("enableUI", () => {
it("should enable the UI", async () => {
await setDocumentAndLoadScript();
const customElement = document.createElement("import-map-overrides-full");
customElement.setAttribute("show-when-local-storage", "true");
customElement.renderWithPreact = jest.fn();
customElement.setAttribute("show-when-local-storage", "swlsKey");
document.body.appendChild(customElement);

window.importMapOverrides.enableUI();

expect(window.localStorage.getItem("swlsKey")).toBe("true");
expect(customElement.renderWithPreact).toHaveBeenCalledTimes(1);
});
});

describe("mergeImportMap", () => {
it("should merge an import map", async () => {
await setDocumentAndLoadScript();
const map = await window.importMapOverrides.mergeImportMap(
{
imports: {
package1: "https://cdn.skypack.dev/package10.js",
package3: "https://cdn.skypack.dev/package33.js",
},
scopes: {
scope1: {
package5: "https://cdn.skypack.dev/package55.js",
package6: "https://cdn.skypack.dev/package66.js",
},
scope3: {
package8: "https://cdn.skypack.dev/package89.js",
package3: "https://cdn.skypack.dev/package31.js",
},
},
},
{
imports: {
package1: "https://cdn.skypack.dev/package11.js",
package2: "https://cdn.skypack.dev/package20.js",
},
scopes: {
scope2: {
package7: "https://cdn.skypack.dev/package77.js",
},
scope3: {
package3: "https://cdn.skypack.dev/package32.js",
package9: "https://cdn.skypack.dev/package99.js",
},
},
}
);

expect(map).toEqual({
imports: {
package1: "https://cdn.skypack.dev/package11.js",
package2: "https://cdn.skypack.dev/package20.js",
package3: "https://cdn.skypack.dev/package33.js",
},
scopes: {
scope1: {
package5: "https://cdn.skypack.dev/package55.js",
package6: "https://cdn.skypack.dev/package66.js",
},
scope2: {
package7: "https://cdn.skypack.dev/package77.js",
},
scope3: {
package3: "https://cdn.skypack.dev/package32.js",
package9: "https://cdn.skypack.dev/package99.js",
},
},
});
});
});
Expand Down
23 changes: 23 additions & 0 deletions src/util/includes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { includes } from "./includes";

describe("includes", () => {
it("returns true if the item is in the array", () => {
expect(includes([1, 2, 3], 2)).toBe(true);
});

it("returns false if the item is not in the array", () => {
expect(includes([1, 2, 3], 4)).toBe(false);
});

it("throws an error if the first argument is not an array or string", () => {
expect(() => includes(123, 1)).toThrow();
});

it("returns true if the string contains the substring", () => {
expect(includes("hello-world", "world")).toBe(true);
});

it("returns false if the string does not contain the substring", () => {
expect(includes("hello-world", "goodbye")).toBe(false);
});
});
Loading

0 comments on commit dc2c48f

Please sign in to comment.