From d31b1aaa22a71cd9e9f0949036cec5facb515616 Mon Sep 17 00:00:00 2001 From: stonedDiscord Date: Thu, 26 Jun 2025 18:49:00 +0200 Subject: move tests --- webAO/__tests__/aoml.test.ts | 109 ++++++++++++++++++++++++++ webAO/__tests__/downloadFile.test.ts | 27 +++++++ webAO/__tests__/tryUrls.test.ts | 28 +++++++ webAO/services/__tests__/downloadFile.test.ts | 27 ------- webAO/utils/__tests__/aoml.test.ts | 109 -------------------------- webAO/utils/__tests__/paths.test.ts | 11 --- webAO/utils/__tests__/tryUrls.test.ts | 28 ------- 7 files changed, 164 insertions(+), 175 deletions(-) create mode 100644 webAO/__tests__/aoml.test.ts create mode 100644 webAO/__tests__/downloadFile.test.ts create mode 100644 webAO/__tests__/tryUrls.test.ts delete mode 100644 webAO/services/__tests__/downloadFile.test.ts delete mode 100644 webAO/utils/__tests__/aoml.test.ts delete mode 100644 webAO/utils/__tests__/paths.test.ts delete mode 100644 webAO/utils/__tests__/tryUrls.test.ts diff --git a/webAO/__tests__/aoml.test.ts b/webAO/__tests__/aoml.test.ts new file mode 100644 index 0000000..b799da6 --- /dev/null +++ b/webAO/__tests__/aoml.test.ts @@ -0,0 +1,109 @@ +import request from "../services/request"; +import mlConfig from "../utils/aoml"; + +jest.mock("../services/request"); +const networkRequest = ` +c0 = 247, 247, 247 +c0_name = White +c0_talking = 1 + +c2 = 247, 0, 57 +c2_name = Red +c2_start = ~ +c2_end = ~ +c2_remove = 1 +c2_talking = 1 + +c4 = 107, 198, 247 +c4_name = Blue +c4_start = ( +c4_end = ) +c4_remove = 0 +c4_talking = 0 + +c5 = 107, 198, 247 +c5_name = Blue +c5_start = [ +c5_end = ] +c5_remove = 1 +c5_talking = 0 + +c6 = 107, 198, 247 +c6_name = Blue +c6_start = | +c6_end = | +c6_remove = 0 +c6_talking = 0 +`; + +const mockRequest = request as jest.MockedFunction; +mockRequest.mockReturnValue(Promise.resolve(networkRequest)); + +describe("mlConfig", () => { + beforeEach(() => { + // Clear all instances and calls to constructor and all methods: + mockRequest.mockClear(); + }); + + it("Should make a network request", () => { + mlConfig("localhost"); + expect(mockRequest).toHaveBeenCalledTimes(1); + }); +}); +describe("applyMarkdown", () => { + const config = mlConfig("localhost"); + + beforeEach(() => { + // Clear all instances and calls to constructor and all methods: + mockRequest.mockClear(); + }); + + it("Should create an array of spans containing letters", async () => { + const word = `hello`; + const actual = await config.applyMarkdown(`hello`, `blue`); + let index = 0; + for (const element of actual) { + expect(element.innerHTML).toBe(word[index]); + index++; + } + }); + it("Should add colors based on settings", async () => { + const config = mlConfig("localhost"); + const actual = await config.applyMarkdown(`(heya)`, `blue`); + expect(actual[0].getAttribute("style")).toBe("color: rgb(107, 198, 247);"); + }); + it("Should keep a letter if remove = 0", async () => { + const config = mlConfig("localhost"); + + const actual = await config.applyMarkdown(`(What())Hey!`, `white`); + const expected = `(`; + expect(actual[5].innerHTML).toBe(expected); + }); + it("Should remove a letter if remove = 1", async () => { + const config = mlConfig("localhost"); + + const actual = await config.applyMarkdown(`~What~()Hey!`, `white`); + const expected = ``; + expect(actual[0].innerHTML).toBe(expected); + }); + it("Should remove a letter if remove = 1", async () => { + const config = mlConfig("localhost"); + + const actual = await config.applyMarkdown(`~What~()Hey!`, `white`); + const expected = ``; + expect(actual[0].innerHTML).toBe(expected); + }); + it("Should keep a closing letter if remove = 0", async () => { + const config = mlConfig("localhost"); + + const actual = await config.applyMarkdown(`~NO[]~!`, `white`); + const expected = ``; + expect(actual[4].innerHTML).toBe(expected); + }); + it("Should remove a closing letter if remove = 1", async () => { + const config = mlConfig("localhost"); + const actual = await config.applyMarkdown(`~NO||~!`, `white`); + const expected = ``; + expect(actual[5].innerHTML).toBe(expected); + }); +}); diff --git a/webAO/__tests__/downloadFile.test.ts b/webAO/__tests__/downloadFile.test.ts new file mode 100644 index 0000000..738d6f3 --- /dev/null +++ b/webAO/__tests__/downloadFile.test.ts @@ -0,0 +1,27 @@ +import downloadFile from "../services/downloadFile"; +jest.useFakeTimers().setSystemTime(new Date("2020-01-01").getTime()); + +global.URL.createObjectURL = jest.fn(); +(window as any).global.Blob = function (content, options) { + return { content, options }; +}; + +describe("downloadFile", () => { + it("Creates an tag", () => { + const createElementSpy = jest.spyOn(document, "createElement"); + downloadFile("hi", "filename"); + expect(createElementSpy).toBeCalled(); + }); + it("Creates the blob with the correct data", () => { + const data = "writingtestsishard"; + global.URL.createObjectURL = jest.fn(() => data); + downloadFile(data, "filename"); + const expected = { + content: [data], + options: { + type: "text", + }, + }; + expect(global.URL.createObjectURL).toBeCalledWith(expected); + }); +}); diff --git a/webAO/__tests__/tryUrls.test.ts b/webAO/__tests__/tryUrls.test.ts new file mode 100644 index 0000000..4a32f32 --- /dev/null +++ b/webAO/__tests__/tryUrls.test.ts @@ -0,0 +1,28 @@ +import fileExists from "../utils/fileExists"; +import tryUrls from "../utils/tryUrls"; +import transparentPng from "../constants/transparentPng"; +jest.mock("../utils/fileExists"); + +const mockFileExists = fileExists as jest.MockedFunction; + +describe("tryUrls", () => { + it("Should try multiple file extensions", async () => { + const url = "localhost/stoneddiscord/assets"; + mockFileExists + .mockReturnValueOnce(Promise.resolve(false)) + .mockReturnValueOnce(Promise.resolve(false)) + .mockReturnValueOnce(Promise.resolve(false)) + .mockReturnValueOnce(Promise.resolve(true)); + const actual = await tryUrls(url); + const expected = "localhost/stoneddiscord/assets.apng"; + expect(actual).toBe(expected); + }); + + it("Should return a transparent png if it cant find any assets", async () => { + const url = "localhost/stoneddiscord/assets"; + mockFileExists.mockReturnValue(Promise.resolve(false)); + const actual = await tryUrls(url); + const expected = transparentPng; + expect(actual).toBe(expected); + }); +}); diff --git a/webAO/services/__tests__/downloadFile.test.ts b/webAO/services/__tests__/downloadFile.test.ts deleted file mode 100644 index c37c735..0000000 --- a/webAO/services/__tests__/downloadFile.test.ts +++ /dev/null @@ -1,27 +0,0 @@ -import downloadFile from "../downloadFile"; -jest.useFakeTimers().setSystemTime(new Date("2020-01-01").getTime()); - -global.URL.createObjectURL = jest.fn(); -(window as any).global.Blob = function (content, options) { - return { content, options }; -}; - -describe("downloadFile", () => { - it("Creates an tag", () => { - const createElementSpy = jest.spyOn(document, "createElement"); - downloadFile("hi", "filename"); - expect(createElementSpy).toBeCalled(); - }); - it("Creates the blob with the correct data", () => { - const data = "writingtestsishard"; - global.URL.createObjectURL = jest.fn(() => data); - downloadFile(data, "filename"); - const expected = { - content: [data], - options: { - type: "text", - }, - }; - expect(global.URL.createObjectURL).toBeCalledWith(expected); - }); -}); diff --git a/webAO/utils/__tests__/aoml.test.ts b/webAO/utils/__tests__/aoml.test.ts deleted file mode 100644 index b5cd9b1..0000000 --- a/webAO/utils/__tests__/aoml.test.ts +++ /dev/null @@ -1,109 +0,0 @@ -import request from "../../services/request"; -import mlConfig from "../aoml"; - -jest.mock("../../services/request"); -const networkRequest = ` -c0 = 247, 247, 247 -c0_name = White -c0_talking = 1 - -c2 = 247, 0, 57 -c2_name = Red -c2_start = ~ -c2_end = ~ -c2_remove = 1 -c2_talking = 1 - -c4 = 107, 198, 247 -c4_name = Blue -c4_start = ( -c4_end = ) -c4_remove = 0 -c4_talking = 0 - -c5 = 107, 198, 247 -c5_name = Blue -c5_start = [ -c5_end = ] -c5_remove = 1 -c5_talking = 0 - -c6 = 107, 198, 247 -c6_name = Blue -c6_start = | -c6_end = | -c6_remove = 0 -c6_talking = 0 -`; - -const mockRequest = request as jest.MockedFunction; -mockRequest.mockReturnValue(Promise.resolve(networkRequest)); - -describe("mlConfig", () => { - beforeEach(() => { - // Clear all instances and calls to constructor and all methods: - mockRequest.mockClear(); - }); - - it("Should make a network request", () => { - mlConfig("localhost"); - expect(mockRequest).toHaveBeenCalledTimes(1); - }); -}); -describe("applyMarkdown", () => { - const config = mlConfig("localhost"); - - beforeEach(() => { - // Clear all instances and calls to constructor and all methods: - mockRequest.mockClear(); - }); - - it("Should create an array of spans containing letters", async () => { - const word = `hello`; - const actual = await config.applyMarkdown(`hello`, `blue`); - let index = 0; - for (const element of actual) { - expect(element.innerHTML).toBe(word[index]); - index++; - } - }); - it("Should add colors based on settings", async () => { - const config = mlConfig("localhost"); - const actual = await config.applyMarkdown(`(heya)`, `blue`); - expect(actual[0].getAttribute("style")).toBe("color: rgb(107, 198, 247);"); - }); - it("Should keep a letter if remove = 0", async () => { - const config = mlConfig("localhost"); - - const actual = await config.applyMarkdown(`(What())Hey!`, `white`); - const expected = `(`; - expect(actual[5].innerHTML).toBe(expected); - }); - it("Should remove a letter if remove = 1", async () => { - const config = mlConfig("localhost"); - - const actual = await config.applyMarkdown(`~What~()Hey!`, `white`); - const expected = ``; - expect(actual[0].innerHTML).toBe(expected); - }); - it("Should remove a letter if remove = 1", async () => { - const config = mlConfig("localhost"); - - const actual = await config.applyMarkdown(`~What~()Hey!`, `white`); - const expected = ``; - expect(actual[0].innerHTML).toBe(expected); - }); - it("Should keep a closing letter if remove = 0", async () => { - const config = mlConfig("localhost"); - - const actual = await config.applyMarkdown(`~NO[]~!`, `white`); - const expected = ``; - expect(actual[4].innerHTML).toBe(expected); - }); - it("Should remove a closing letter if remove = 1", async () => { - const config = mlConfig("localhost"); - const actual = await config.applyMarkdown(`~NO||~!`, `white`); - const expected = ``; - expect(actual[5].innerHTML).toBe(expected); - }); -}); diff --git a/webAO/utils/__tests__/paths.test.ts b/webAO/utils/__tests__/paths.test.ts deleted file mode 100644 index fe7b1bf..0000000 --- a/webAO/utils/__tests__/paths.test.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { getFilenameFromPath } from "../paths"; -jest.mock("../fileExists"); - -describe("getFilenameFromPath", () => { - const EXAMPLE_PATH = "localhost/stoneddiscord/assets.png"; - it("Should get the last value from a path", async () => { - const actual = getFilenameFromPath(EXAMPLE_PATH); - const expected = "assets.png"; - expect(actual).toBe(expected); - }); -}); diff --git a/webAO/utils/__tests__/tryUrls.test.ts b/webAO/utils/__tests__/tryUrls.test.ts deleted file mode 100644 index f85392c..0000000 --- a/webAO/utils/__tests__/tryUrls.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import fileExists from "../fileExists"; -import tryUrls from "../tryUrls"; -import transparentPng from "../../constants/transparentPng"; -jest.mock("../fileExists"); - -const mockFileExists = fileExists as jest.MockedFunction; - -describe("tryUrls", () => { - it("Should try multiple file extensions", async () => { - const url = "localhost/stoneddiscord/assets"; - mockFileExists - .mockReturnValueOnce(Promise.resolve(false)) - .mockReturnValueOnce(Promise.resolve(false)) - .mockReturnValueOnce(Promise.resolve(false)) - .mockReturnValueOnce(Promise.resolve(true)); - const actual = await tryUrls(url); - const expected = "localhost/stoneddiscord/assets.apng"; - expect(actual).toBe(expected); - }); - - it("Should return a transparent png if it cant find any assets", async () => { - const url = "localhost/stoneddiscord/assets"; - mockFileExists.mockReturnValue(Promise.resolve(false)); - const actual = await tryUrls(url); - const expected = transparentPng; - expect(actual).toBe(expected); - }); -}); -- cgit