From 94c042f1530879959ea6b43a7c48476b2c8bf94e Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Tue, 30 Jul 2024 23:46:09 +0900 Subject: [PATCH] test: add `injectScript` test code --- __tests__/unit/utils/injectScript.test.ts | 76 +++++++++++++++++++++++ src/utils/injectScript.ts | 1 - 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 __tests__/unit/utils/injectScript.test.ts diff --git a/__tests__/unit/utils/injectScript.test.ts b/__tests__/unit/utils/injectScript.test.ts new file mode 100644 index 0000000..a748aa1 --- /dev/null +++ b/__tests__/unit/utils/injectScript.test.ts @@ -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 = []; + const externalResourcesConfig: Array = []; + const result = getScripts(externalResources, externalResourcesConfig); + + expect(result).toEqual([]); +}); + +test("should return an empty array when no JavaScript resources are found", () => { + const externalResources: Array = [ + { kind: "css", values: ["style.css"] }, + { kind: "js", values: ["example.js"] } + ]; + const externalResourcesConfig: Array = []; + const result = getScripts(externalResources, externalResourcesConfig); + + expect(result).toEqual([]); +}); + +test("should return an array of InjectScript objects when JavaScript resources are found", () => { + const externalResources: Array = [ + { kind: "js", values: ["script1.js", "script2.js"] } + ]; + const externalResourcesConfig: Array = [ + { + 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 = [ + { + 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); +}); diff --git a/src/utils/injectScript.ts b/src/utils/injectScript.ts index 02f356c..aa57ad5 100644 --- a/src/utils/injectScript.ts +++ b/src/utils/injectScript.ts @@ -1,7 +1,6 @@ import { ExternalResources } from "../models/externalResource"; import { InjectScript } from "../models/script"; -// TODO: write test code export function getScripts( externalResources: Array, externalResourcesConfig: Array