Skip to content

Commit

Permalink
refactor(core-webhooks): adapt to new container
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Sep 3, 2019
1 parent 2d3b05f commit eceab20
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 52 deletions.
157 changes: 157 additions & 0 deletions __tests__/unit/core-webhooks/conditions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import "jest-extended";
import {
between,
contains,
eq,
falsy,
gt,
gte,
lt,
lte,
ne,
notBetween,
regexp,
truthy,
} from "@packages/core-webhooks/src/conditions";

describe("Conditions - between", () => {
it("should be true", () => {
expect(
between(1.5, {
min: 1,
max: 2,
}),
).toBeTrue();
});

it("should be false", () => {
expect(
between(3, {
min: 1,
max: 2,
}),
).toBeFalse();
});
});

describe("Conditions - contains", () => {
it("should be true", () => {
expect(contains("Hello World", "Hello")).toBeTrue();
});

it("should be false", () => {
expect(contains("Hello World", "invalid")).toBeFalse();
});
});

describe("Conditions - equal", () => {
it("should be true", () => {
expect(eq(1, 1)).toBeTrue();
});

it("should be false", () => {
expect(eq(1, 2)).toBeFalse();
});
});

describe("Conditions - falsy", () => {
it("should be true", () => {
expect(falsy(false)).toBeTrue();
});

it("should be false", () => {
expect(falsy(true)).toBeFalse();
});
});

describe("Conditions - greater than", () => {
it("should be true", () => {
expect(gt(2, 1)).toBeTrue();
});

it("should be false", () => {
expect(gt(1, 2)).toBeFalse();
});
});

describe("Conditions - greater than or equal", () => {
it("should be true", () => {
expect(gte(2, 1)).toBeTrue();
expect(gte(2, 2)).toBeTrue();
});

it("should be false", () => {
expect(gte(1, 2)).toBeFalse();
});
});

describe("Conditions - less than", () => {
it("should be true", () => {
expect(lt(1, 2)).toBeTrue();
});

it("should be false", () => {
expect(lt(2, 1)).toBeFalse();
});
});

describe("Conditions - less than or equal", () => {
it("should be true", () => {
expect(lte(1, 2)).toBeTrue();
expect(lte(1, 1)).toBeTrue();
});

it("should be false", () => {
expect(lte(2, 1)).toBeFalse();
});
});

describe("Conditions - not equal", () => {
it("should be true", () => {
expect(ne(1, 2)).toBeTrue();
});

it("should be false", () => {
expect(ne(1, 1)).toBeFalse();
});
});

describe("Conditions - not-between", () => {
it("should be true", () => {
expect(
notBetween(3, {
min: 1,
max: 2,
}),
).toBeTrue();
});

it("should be false", () => {
expect(
notBetween(1.5, {
min: 1,
max: 2,
}),
).toBeFalse();
});
});

describe("Conditions - regexp", () => {
it("should be true", () => {
expect(regexp("hello world!", "hello")).toBeTrue();
});

it("should be false", () => {
expect(regexp(123, "w+")).toBeFalse();
});
});

describe("Conditions - truthy", () => {
it("should be true", () => {
expect(truthy(true)).toBeTrue();
});

it("should be false", () => {
expect(truthy(false)).toBeFalse();
});
});
94 changes: 94 additions & 0 deletions __tests__/unit/core-webhooks/database.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import "jest-extended";

import { Application } from "@packages/core-kernel/src/application";
import { Container, interfaces } from "@packages/core-kernel/src/ioc";
import { Database } from "@packages/core-webhooks/src/database";
import { Webhook } from "@packages/core-webhooks/src/interfaces";
import { dirSync, setGracefulCleanup } from "tmp";

const dummyWebhook: Webhook = {
id: "id",
token: "token",
event: "event",
target: "target",
enabled: true,
conditions: [
{
key: "key",
value: "value",
condition: "condition",
},
],
};

let app: Application;
let container: interfaces.Container;
let database: Database;

beforeEach(() => {
container = new Container();
container.snapshot();

app = new Application(container);
app.bind("path.cache").toConstantValue(dirSync().name);

app.bind<Database>("webhooks.db")
.to(Database)
.inSingletonScope();

database = app.get<Database>("webhooks.db");
});

afterEach(() => container.restore());

afterAll(() => setGracefulCleanup());

describe("Database", () => {
it("should return all webhooks", () => {
database.create(dummyWebhook);

expect(database.all()).toHaveLength(1);
});

it("should find a webhook by its id", () => {
const webhook = database.create(dummyWebhook);

expect(database.findById(webhook.id)).toEqual(webhook);
});

it("should find webhooks by their event", () => {
const webhook: Webhook = database.create(dummyWebhook);

const rows = database.findByEvent("event");

expect(rows).toHaveLength(1);
expect(rows[0]).toEqual(webhook);
});

it("should return an empty array if there are no webhooks for an event", () => {
expect(database.findByEvent("event")).toHaveLength(0);
});

it("should create a new webhook", () => {
const webhook: Webhook = database.create(dummyWebhook);

expect(database.create(webhook)).toEqual(webhook);
});

it("should update an existing webhook", () => {
const webhook: Webhook = database.create(dummyWebhook);
const updated: Webhook = database.update(webhook.id, dummyWebhook);

expect(database.findById(webhook.id)).toEqual(updated);
});

it("should delete an existing webhook", () => {
const webhook: Webhook = database.create(dummyWebhook);

expect(database.findById(webhook.id)).toEqual(webhook);

database.destroy(webhook.id);

expect(database.findById(webhook.id)).toBeUndefined();
});
});
Loading

0 comments on commit eceab20

Please sign in to comment.