ICS React Native App
1import request from "supertest";
2import { build } from "../src/server";
3import { Database } from "sqlite-async";
4import { DATABASE_URL } from "../src/config";
5import { seed, testUser } from "../src/seeder";
6import { PaginatedTransactions, Transaction } from "../generated";
7
8type App = ReturnType<typeof build>;
9let app: App | null = null;
10
11beforeAll(async () => {
12 const db = await Database.open(DATABASE_URL);
13 await seed(db);
14
15 app = build({ db });
16});
17
18describe("GET /accounts", () => {
19 it("it can fetch accounts", async () => {
20 const auth = await request(app).post("/login").send(testUser);
21
22 await request(app)
23 .get("/accounts")
24 .set("Authorization", `Bearer ${auth.body.accessToken}`)
25 .expect(200)
26 .expect(({ body }) => expect(body).not.toHaveLength(0));
27 });
28
29 it("it fails getting transactions without a token", async () => {
30 await request(app).get("/accounts").expect(401);
31 });
32});
33
34describe("GET /accounts/:id", () => {
35 it("it can fetch a single accouns", async () => {
36 const auth = await request(app).post("/login").send(testUser);
37
38 const accounts = await request(app)
39 .get("/accounts")
40 .set("Authorization", `Bearer ${auth.body.accessToken}`)
41 .expect(({ body }) => expect(body).not.toHaveLength(0));
42
43 await request(app)
44 .get(`/accounts/${accounts.body[0].id}`)
45 .set("Authorization", `Bearer ${auth.body.accessToken}`)
46 .expect(200)
47 .expect(({ body }) => expect(body.id).toBe(accounts.body[0].id));
48 });
49});
50
51describe("GET /transactions", () => {
52 it("it can fetch transactions", async () => {
53 const auth = await request(app).post("/login").send(testUser);
54
55 await request(app)
56 .get("/transactions")
57 .set("Authorization", `Bearer ${auth.body.accessToken}`)
58 .expect(200);
59 });
60
61 it("it fails getting transactions without a token", async () => {
62 await request(app).get("/transactions").expect(401);
63 });
64
65 it("it fails getting transactions with an invalid token", async () => {
66 await request(app)
67 .get("/transactions")
68 .set("Authorization", "foo")
69 .expect(401);
70 });
71
72 it("can filter transactions by account", async () => {
73 const auth = await request(app).post("/login").send(testUser);
74
75 const account = await request(app)
76 .get("/accounts")
77 .set("Authorization", `Bearer ${auth.body.accessToken}`)
78 .expect(200)
79 .expect(({ body }) => expect(body).not.toHaveLength(0));
80
81 await request(app)
82 .get(`/transactions?accountId=${account.body[0].id}`)
83 .set("Authorization", `Bearer ${auth.body.accessToken}`)
84 .expect(200)
85 .expect(({ body }) => expect(body.data).not.toHaveLength(0))
86 .expect(({ body }: { body: PaginatedTransactions }) => {
87 body.data.forEach((transaction) => {
88 expect(transaction.accountId).toBe(account.body[0].id);
89 });
90 });
91 });
92
93 it("can filter transactions by type", async () => {
94 const auth = await request(app).post("/login").send(testUser);
95
96 const transactionType = await request(app)
97 .get("/transaction-types")
98 .set("Authorization", `Bearer ${auth.body.accessToken}`)
99 .expect(200)
100 .expect(({ body }) => expect(body).not.toHaveLength(0));
101
102 await request(app)
103 .get(`/transactions?type=${transactionType.body[0].name}`)
104 .set("Authorization", `Bearer ${auth.body.accessToken}`)
105 .expect(200)
106 .expect(({ body }) => expect(body.data).not.toHaveLength(0))
107 .expect(({ body }) => {
108 body.data.forEach(({ type }: Transaction) => {
109 expect(type).toBe(transactionType.body[0].name);
110 });
111 });
112 });
113});