From 08441494f12dfa6a4a2fed71b76fa52579e001d6 Mon Sep 17 00:00:00 2001 From: Christopher Hase Date: Mon, 31 Mar 2025 13:59:41 +0200 Subject: [PATCH] unit tests part 1 --- README.md | 14 +++++++++++++- broker.test.ts | 27 +++++++++++++++++++++++++++ broker.ts | 4 +++- config.json | 2 +- jest.config.ts | 5 +++++ package.json | 2 +- 6 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 broker.test.ts create mode 100644 jest.config.ts diff --git a/README.md b/README.md index b1e6534..d634221 100644 --- a/README.md +++ b/README.md @@ -13,5 +13,17 @@ This app will send an I-Ching horoscope to the pre-configured mailhog instance i This app uses the I-Ching library app https://github.com/Velfi/i-ching.git. ## Configuration -It is possible to configure the intervall of days between the sending of horoscopes and the time of sending. +It is possible to configure the mail host and port. +It is also possible to configure the intervall of days between the sending of horoscopes and the time of sending. The default is to send one email every seven days at 8 am. +## First Start +The app can be deployed by running: + + kubectl apply -f deployment.yaml + +When a pod with the app is initally started, one email will be sent to the configured receiver. + +## Testing +The Jest unit tests can be run with + +npm test \ No newline at end of file diff --git a/broker.test.ts b/broker.test.ts new file mode 100644 index 0000000..4cf9ca8 --- /dev/null +++ b/broker.test.ts @@ -0,0 +1,27 @@ +import { html } from "./broker"; + +beforeAll(() => { + jest.spyOn(console, "log").mockImplementation(() => {}); +}); + +test("Generate HTML for hexagram", () => { + expect(html("Hexagram No. 45 ䷬\nGathering Together [Massing]\n萃 (cuì)")) + .toBe("

Hexagram No. 45 ䷬

Gathering Together [Massing] - 萃 (cuì)

"); + +}); + +test("Generate HTML for judgement", () => { + expect(html("Judgement:\nGathering Together. Success.\nThe king approaches his temple.\nIt furthers one to see the great man.\nThis brings success. Perseverance furthers.\nTo bring great offerings creates good fortune.\nIt furthers one to undertake something.")) + .toBe("

Judgement:

Gathering Together. Success.
The king approaches his temple.
It furthers one to see the great man.
This brings success. Perseverance furthers.
To bring great offerings creates good fortune.
It furthers one to undertake something.

"); + +}); + +test("Generate HTML for images", () => { + expect(html("Images:\nOver the earth, the lake:\nThe image of Gathering Together.\nThus the superior man renews his weapons\nIn order to meet the unforseen.")) + .toBe("

Images:

Over the earth, the lake:
The image of Gathering Together.
Thus the superior man renews his weapons
In order to meet the unforseen.

"); + + }); + +afterAll(() => { + (console.log as jest.Mock).mockRestore(); +}); diff --git a/broker.ts b/broker.ts index 785e31d..f48791b 100644 --- a/broker.ts +++ b/broker.ts @@ -137,7 +137,7 @@ function scheduleNextRun(nextRunDate: Date) { } // Generate 1) Parse Tree and 2) HTML -function html(inputText: string): string { +export function html(inputText: string): string { const parseTree = parse(inputText); const htmlOutput = render(parseTree); @@ -229,5 +229,7 @@ function render(node: Node): string { return outputHTML; } + + } \ No newline at end of file diff --git a/config.json b/config.json index 60134d4..92bec07 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,5 @@ { - "daysInterval": 2, + "daysInterval": 7, "timeOfDay": "8:00", "emailReceiver": "test@mailhog.local", "mailHost": "mailhog.mailhog.svc.cluster.local", diff --git a/jest.config.ts b/jest.config.ts new file mode 100644 index 0000000..c10c522 --- /dev/null +++ b/jest.config.ts @@ -0,0 +1,5 @@ +module.exports = { + transform: { + "^.+\\.tsx?$": "ts-jest" + } + }; \ No newline at end of file diff --git a/package.json b/package.json index a1318ce..c50fb98 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "1.0.0", "main": "broker.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "jest" }, "keywords": [], "author": "",