Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Cowton committed Nov 20, 2023
1 parent 92e80f2 commit 2923905
Showing 1 changed file with 97 additions and 29 deletions.
126 changes: 97 additions & 29 deletions anghammarad-client-node/test/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Anghammarad} from "../src/main";
import {NotifyParams, RequestedChannel} from "../src/interfaces";
import { Anghammarad } from "../src/main";
import { NotifyParams, RequestedChannel } from "../src/interfaces";

describe("The messageJson function", () => {
// Pass something in here so that we don't bother instantiating a client
// each time we run the tests
// each time we run tests
const client = new Anghammarad({} as any);

const defaultParams: NotifyParams = {
Expand All @@ -17,50 +17,118 @@ describe("The messageJson function", () => {
};

it("sets message as provided", () => {
expect(JSON.parse(client.messageJson(defaultParams))).toMatchObject({message: "message"});
expect(JSON.parse(client.messageJson(defaultParams))).toMatchObject({
message: "message",
});
});

it("sets sender as provided", () =>{
expect(JSON.parse(client.messageJson(defaultParams))).toMatchObject({sender: "source"})
})
it("sets sender as provided", () => {
expect(JSON.parse(client.messageJson(defaultParams))).toMatchObject({
sender: "source",
});
});

describe("represents channel correctly", () => {
it("for 'All'", () => {
expect(JSON.parse(client.messageJson(defaultParams))).toMatchObject({channel: "all"})
})
expect(JSON.parse(client.messageJson(defaultParams))).toMatchObject({
channel: "all",
});
});

it("for 'Email'", () => {
expect(JSON.parse(client.messageJson({...defaultParams, channel: RequestedChannel.Email}))).toMatchObject({channel: "email"})
})
expect(
JSON.parse(
client.messageJson({
...defaultParams,
channel: RequestedChannel.Email,
})
)
).toMatchObject({ channel: "email" });
});

it("for 'Hangouts'", () => {
expect(JSON.parse(client.messageJson({...defaultParams, channel: RequestedChannel.HangoutsChat}))).toMatchObject({channel:"hangouts"})
})
expect(
JSON.parse(
client.messageJson({
...defaultParams,
channel: RequestedChannel.HangoutsChat,
})
)
).toMatchObject({ channel: "hangouts" });
});

it("for 'Prefer Email'", () => {
expect(JSON.parse(client.messageJson({...defaultParams, channel: RequestedChannel.PreferEmail}))).toMatchObject({channel:"prefer email"})
})
expect(
JSON.parse(
client.messageJson({
...defaultParams,
channel: RequestedChannel.PreferEmail,
})
)
).toMatchObject({ channel: "prefer email" });
});

it("for 'Prefer Hangouts'", () => {
expect(JSON.parse(client.messageJson({...defaultParams, channel: RequestedChannel.PreferHangouts}))).toMatchObject({channel:"prefer hangouts"})
})
})
expect(
JSON.parse(
client.messageJson({
...defaultParams,
channel: RequestedChannel.PreferHangouts,
})
)
).toMatchObject({ channel: "prefer hangouts" });
});
});

it("includes target", () => {
const result = JSON.parse(client.messageJson({...defaultParams, target: {Stack: "stack-name", App: "app-name"}}))
expect(result).toMatchObject({target:{Stack:"stack-name", App: "app-name"}})
})
const result = JSON.parse(
client.messageJson({
...defaultParams,
target: { Stack: "stack-name", App: "app-name" },
})
);
expect(result).toMatchObject({
target: { Stack: "stack-name", App: "app-name" },
});
});

it("includes actions", () => {
const result = JSON.parse(client.messageJson({...defaultParams, actions: [{cta: "cta1", url: "url1"}, {cta: "cta2", url: "url2"}]}))
expect(result).toMatchObject({actions: [{cta: "cta1", url: "url1"}, {cta: "cta2", url: "url2"}]})
})
const result = JSON.parse(
client.messageJson({
...defaultParams,
actions: [
{ cta: "cta1", url: "url1" },
{ cta: "cta2", url: "url2" },
],
})
);
expect(result).toMatchObject({
actions: [
{ cta: "cta1", url: "url1" },
{ cta: "cta2", url: "url2" },
],
});
});

it("produces valid JSON when quotes are contained in the data", () => {
expect(() => JSON.parse(client.messageJson({...defaultParams, message: `Message with "quotes"`}))).not.toThrow()
})
expect(() =>
JSON.parse(
client.messageJson({
...defaultParams,
message: `Message with "quotes"`,
})
)
).not.toThrow();
});

it("properly escapes input", () => {
expect(JSON.parse(client.messageJson({...defaultParams, message: `Message with "quotes"`}))).toMatchObject({message: `Message with "quotes"`})
})
})
expect(
JSON.parse(
client.messageJson({
...defaultParams,
message: `Message with "quotes"`,
})
)
).toMatchObject({ message: `Message with "quotes"` });
});
});

0 comments on commit 2923905

Please sign in to comment.