Skip to content

Commit

Permalink
chore: add more UT for src/notifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Apr 26, 2024
1 parent 3345761 commit f2c15da
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/notifiers/src/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class WebhookNotifier<T> {
};

constructor(options: WebhookNotifierOptions) {
this.webhookUrl = JSON.parse(JSON.stringify((options.webhookUrl)));
this.webhookUrl = options.webhookUrl;
this.data = JSON.parse(JSON.stringify((options.data)));
this.template = JSON.parse(JSON.stringify((options.template)));

Expand All @@ -76,7 +76,8 @@ export class WebhookNotifier<T> {
}

if (this.showSeverityEmoji && this.template.title) {
this.template.title = `${kSeverityEmoji[this.data.severity]} ${this.template.title}`;
const emoji = kSeverityEmoji[this.data.severity];
this.template.title = `${emoji} ${this.template.title}`;
}

return morphix(this.template.title, this.data, this.titleTemplateOptions());
Expand Down
72 changes: 66 additions & 6 deletions src/notifiers/test/webhook.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
// Import Node.js Dependencies
import assert from "node:assert";
import { describe, it, after, before } from "node:test";
import { describe, it, after, before, test } from "node:test";

// Import Third-party Dependencies
import { MockAgent, MockPool, getGlobalDispatcher, setGlobalDispatcher } from "@myunisoft/httpie";

// Import Internal Dependencies
import { WebhookNotifier } from "../src/webhook";

// CONSTANTS
const kMockAgent = new MockAgent();
const kDispatcher = getGlobalDispatcher();
const kDummyWebhoobURL = "https://webhook.test";

class DummyWebhookNotifier extends WebhookNotifier<{ foo: string }> {
async formatWebhookBody() {
return { foo: "bar" };
const kDummyNotifierOptions = {
webhookUrl: "https://webhook.test/dummy",
data: {
severity: "critical",
labelCount: 0,
labelMatchCount: 1
}
}
} as const;

describe("Webhook", () => {
let pool: MockPool;
Expand All @@ -32,7 +36,57 @@ describe("Webhook", () => {
setGlobalDispatcher(kDispatcher);
});

it("should send a webhook", async() => {
describe("formatTitle", () => {
it("should format title using severity variable that we capitalize using morphix", async() => {
const notifier = new DummyWebhookNotifier({
...kDummyNotifierOptions,
template: {
title: "foobar {severity | capitalize}",
content: []
}
});

const title = await notifier.formatTitle();
assert.strictEqual(title, "💥 foobar Critical");
});

it("should remove Emoji from title when showSeverityEmoji is disabled", async() => {
const notifier = new DummyWebhookNotifier({
...kDummyNotifierOptions,
template: {
title: "foobar",
content: []
}
});
notifier.showSeverityEmoji = false;

const title = await notifier.formatTitle();
assert.strictEqual(title, "foobar");
});
});

describe("formatContent", () => {
it("should format content injecting variables in it and formating with morphix when required", async() => {
const notifier = new DummyWebhookNotifier({
...kDummyNotifierOptions,
template: {
title: "",
content: [
"labelCount: {labelCount}",
"severity: {severity | capitalize}"
]
}
});

const content = await notifier.formatContent();
assert.deepEqual(
content,
["labelCount: 0", "severity: Critical"]
);
});
});

test("execute should trigger an HTTP request", async() => {
pool.intercept({
method: "POST",
path: "/dummy"
Expand All @@ -59,3 +113,9 @@ describe("Webhook", () => {
assert.strictEqual(response.statusCode, 200);
});
});

class DummyWebhookNotifier extends WebhookNotifier<{ foo: string }> {
async formatWebhookBody() {
return { foo: "bar" };
}
}

0 comments on commit f2c15da

Please sign in to comment.