Skip to content

Commit eceab20

Browse files
committed
refactor(core-webhooks): adapt to new container
1 parent 2d3b05f commit eceab20

File tree

8 files changed

+461
-52
lines changed

8 files changed

+461
-52
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import "jest-extended";
2+
import {
3+
between,
4+
contains,
5+
eq,
6+
falsy,
7+
gt,
8+
gte,
9+
lt,
10+
lte,
11+
ne,
12+
notBetween,
13+
regexp,
14+
truthy,
15+
} from "@packages/core-webhooks/src/conditions";
16+
17+
describe("Conditions - between", () => {
18+
it("should be true", () => {
19+
expect(
20+
between(1.5, {
21+
min: 1,
22+
max: 2,
23+
}),
24+
).toBeTrue();
25+
});
26+
27+
it("should be false", () => {
28+
expect(
29+
between(3, {
30+
min: 1,
31+
max: 2,
32+
}),
33+
).toBeFalse();
34+
});
35+
});
36+
37+
describe("Conditions - contains", () => {
38+
it("should be true", () => {
39+
expect(contains("Hello World", "Hello")).toBeTrue();
40+
});
41+
42+
it("should be false", () => {
43+
expect(contains("Hello World", "invalid")).toBeFalse();
44+
});
45+
});
46+
47+
describe("Conditions - equal", () => {
48+
it("should be true", () => {
49+
expect(eq(1, 1)).toBeTrue();
50+
});
51+
52+
it("should be false", () => {
53+
expect(eq(1, 2)).toBeFalse();
54+
});
55+
});
56+
57+
describe("Conditions - falsy", () => {
58+
it("should be true", () => {
59+
expect(falsy(false)).toBeTrue();
60+
});
61+
62+
it("should be false", () => {
63+
expect(falsy(true)).toBeFalse();
64+
});
65+
});
66+
67+
describe("Conditions - greater than", () => {
68+
it("should be true", () => {
69+
expect(gt(2, 1)).toBeTrue();
70+
});
71+
72+
it("should be false", () => {
73+
expect(gt(1, 2)).toBeFalse();
74+
});
75+
});
76+
77+
describe("Conditions - greater than or equal", () => {
78+
it("should be true", () => {
79+
expect(gte(2, 1)).toBeTrue();
80+
expect(gte(2, 2)).toBeTrue();
81+
});
82+
83+
it("should be false", () => {
84+
expect(gte(1, 2)).toBeFalse();
85+
});
86+
});
87+
88+
describe("Conditions - less than", () => {
89+
it("should be true", () => {
90+
expect(lt(1, 2)).toBeTrue();
91+
});
92+
93+
it("should be false", () => {
94+
expect(lt(2, 1)).toBeFalse();
95+
});
96+
});
97+
98+
describe("Conditions - less than or equal", () => {
99+
it("should be true", () => {
100+
expect(lte(1, 2)).toBeTrue();
101+
expect(lte(1, 1)).toBeTrue();
102+
});
103+
104+
it("should be false", () => {
105+
expect(lte(2, 1)).toBeFalse();
106+
});
107+
});
108+
109+
describe("Conditions - not equal", () => {
110+
it("should be true", () => {
111+
expect(ne(1, 2)).toBeTrue();
112+
});
113+
114+
it("should be false", () => {
115+
expect(ne(1, 1)).toBeFalse();
116+
});
117+
});
118+
119+
describe("Conditions - not-between", () => {
120+
it("should be true", () => {
121+
expect(
122+
notBetween(3, {
123+
min: 1,
124+
max: 2,
125+
}),
126+
).toBeTrue();
127+
});
128+
129+
it("should be false", () => {
130+
expect(
131+
notBetween(1.5, {
132+
min: 1,
133+
max: 2,
134+
}),
135+
).toBeFalse();
136+
});
137+
});
138+
139+
describe("Conditions - regexp", () => {
140+
it("should be true", () => {
141+
expect(regexp("hello world!", "hello")).toBeTrue();
142+
});
143+
144+
it("should be false", () => {
145+
expect(regexp(123, "w+")).toBeFalse();
146+
});
147+
});
148+
149+
describe("Conditions - truthy", () => {
150+
it("should be true", () => {
151+
expect(truthy(true)).toBeTrue();
152+
});
153+
154+
it("should be false", () => {
155+
expect(truthy(false)).toBeFalse();
156+
});
157+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import "jest-extended";
2+
3+
import { Application } from "@packages/core-kernel/src/application";
4+
import { Container, interfaces } from "@packages/core-kernel/src/ioc";
5+
import { Database } from "@packages/core-webhooks/src/database";
6+
import { Webhook } from "@packages/core-webhooks/src/interfaces";
7+
import { dirSync, setGracefulCleanup } from "tmp";
8+
9+
const dummyWebhook: Webhook = {
10+
id: "id",
11+
token: "token",
12+
event: "event",
13+
target: "target",
14+
enabled: true,
15+
conditions: [
16+
{
17+
key: "key",
18+
value: "value",
19+
condition: "condition",
20+
},
21+
],
22+
};
23+
24+
let app: Application;
25+
let container: interfaces.Container;
26+
let database: Database;
27+
28+
beforeEach(() => {
29+
container = new Container();
30+
container.snapshot();
31+
32+
app = new Application(container);
33+
app.bind("path.cache").toConstantValue(dirSync().name);
34+
35+
app.bind<Database>("webhooks.db")
36+
.to(Database)
37+
.inSingletonScope();
38+
39+
database = app.get<Database>("webhooks.db");
40+
});
41+
42+
afterEach(() => container.restore());
43+
44+
afterAll(() => setGracefulCleanup());
45+
46+
describe("Database", () => {
47+
it("should return all webhooks", () => {
48+
database.create(dummyWebhook);
49+
50+
expect(database.all()).toHaveLength(1);
51+
});
52+
53+
it("should find a webhook by its id", () => {
54+
const webhook = database.create(dummyWebhook);
55+
56+
expect(database.findById(webhook.id)).toEqual(webhook);
57+
});
58+
59+
it("should find webhooks by their event", () => {
60+
const webhook: Webhook = database.create(dummyWebhook);
61+
62+
const rows = database.findByEvent("event");
63+
64+
expect(rows).toHaveLength(1);
65+
expect(rows[0]).toEqual(webhook);
66+
});
67+
68+
it("should return an empty array if there are no webhooks for an event", () => {
69+
expect(database.findByEvent("event")).toHaveLength(0);
70+
});
71+
72+
it("should create a new webhook", () => {
73+
const webhook: Webhook = database.create(dummyWebhook);
74+
75+
expect(database.create(webhook)).toEqual(webhook);
76+
});
77+
78+
it("should update an existing webhook", () => {
79+
const webhook: Webhook = database.create(dummyWebhook);
80+
const updated: Webhook = database.update(webhook.id, dummyWebhook);
81+
82+
expect(database.findById(webhook.id)).toEqual(updated);
83+
});
84+
85+
it("should delete an existing webhook", () => {
86+
const webhook: Webhook = database.create(dummyWebhook);
87+
88+
expect(database.findById(webhook.id)).toEqual(webhook);
89+
90+
database.destroy(webhook.id);
91+
92+
expect(database.findById(webhook.id)).toBeUndefined();
93+
});
94+
});

0 commit comments

Comments
 (0)