Skip to content

Commit

Permalink
test: add injectScript test code
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Jul 30, 2024
1 parent b0d2ac4 commit 94c042f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
76 changes: 76 additions & 0 deletions __tests__/unit/utils/injectScript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { expect, test } from "vitest";
import { ExternalResources } from "../../../src/models/externalResource";
import { InjectScript } from "../../../src/models/script";
import { getScripts } from "../../../src/utils/injectScript";

test("should return an empty array when externalResources is empty", () => {
const externalResources: Array<ExternalResources> = [];
const externalResourcesConfig: Array<any> = [];
const result = getScripts(externalResources, externalResourcesConfig);

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

test("should return an empty array when no JavaScript resources are found", () => {
const externalResources: Array<ExternalResources> = [
{ kind: "css", values: ["style.css"] },
{ kind: "js", values: ["example.js"] }
];
const externalResourcesConfig: Array<any> = [];
const result = getScripts(externalResources, externalResourcesConfig);

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

test("should return an array of InjectScript objects when JavaScript resources are found", () => {
const externalResources: Array<ExternalResources> = [
{ kind: "js", values: ["script1.js", "script2.js"] }
];
const externalResourcesConfig: Array<any> = [
{
key: "script1.js",
async: true,
src: "https://example.com/script1.js",
code: {
type: "inline",
onLoad: true,
code: "console.log('Script 1 loaded');"
}
},
{
key: "script2.js",
async: false,
src: "https://example.com/script2.js",
code: {
type: "external",
onLoad: false,
code: ""
}
}
];
const result = getScripts(externalResources, externalResourcesConfig);
const expected: Array<InjectScript> = [
{
key: "script1.js",
async: true,
src: "https://example.com/script1.js",
code: {
type: "inline",
onLoad: true,
code: "console.log('Script 1 loaded');"
}
},
{
key: "script2.js",
async: false,
src: "https://example.com/script2.js",
code: {
type: "external",
onLoad: false,
code: ""
}
}
];

expect(result).toEqual(expected);
});
1 change: 0 additions & 1 deletion src/utils/injectScript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ExternalResources } from "../models/externalResource";
import { InjectScript } from "../models/script";

// TODO: write test code
export function getScripts(
externalResources: Array<ExternalResources>,
externalResourcesConfig: Array<any>
Expand Down

0 comments on commit 94c042f

Please sign in to comment.