import request from "supertest"; import { build } from "../src/server"; import { Database } from "sqlite-async"; import { DATABASE_URL } from "../src/config"; import { seed, testUser } from "../src/seeder"; import { PaginatedTransactions, Transaction } from "../generated"; type App = ReturnType; let app: App | null = null; beforeAll(async () => { const db = await Database.open(DATABASE_URL); await seed(db); app = build({ db }); }); describe("GET /accounts", () => { it("it can fetch accounts", async () => { const auth = await request(app).post("/login").send(testUser); await request(app) .get("/accounts") .set("Authorization", `Bearer ${auth.body.accessToken}`) .expect(200) .expect(({ body }) => expect(body).not.toHaveLength(0)); }); it("it fails getting transactions without a token", async () => { await request(app).get("/accounts").expect(401); }); }); describe("GET /accounts/:id", () => { it("it can fetch a single accouns", async () => { const auth = await request(app).post("/login").send(testUser); const accounts = await request(app) .get("/accounts") .set("Authorization", `Bearer ${auth.body.accessToken}`) .expect(({ body }) => expect(body).not.toHaveLength(0)); await request(app) .get(`/accounts/${accounts.body[0].id}`) .set("Authorization", `Bearer ${auth.body.accessToken}`) .expect(200) .expect(({ body }) => expect(body.id).toBe(accounts.body[0].id)); }); }); describe("GET /transactions", () => { it("it can fetch transactions", async () => { const auth = await request(app).post("/login").send(testUser); await request(app) .get("/transactions") .set("Authorization", `Bearer ${auth.body.accessToken}`) .expect(200); }); it("it fails getting transactions without a token", async () => { await request(app).get("/transactions").expect(401); }); it("it fails getting transactions with an invalid token", async () => { await request(app) .get("/transactions") .set("Authorization", "foo") .expect(401); }); it("can filter transactions by account", async () => { const auth = await request(app).post("/login").send(testUser); const account = await request(app) .get("/accounts") .set("Authorization", `Bearer ${auth.body.accessToken}`) .expect(200) .expect(({ body }) => expect(body).not.toHaveLength(0)); await request(app) .get(`/transactions?accountId=${account.body[0].id}`) .set("Authorization", `Bearer ${auth.body.accessToken}`) .expect(200) .expect(({ body }) => expect(body.data).not.toHaveLength(0)) .expect(({ body }: { body: PaginatedTransactions }) => { body.data.forEach((transaction) => { expect(transaction.accountId).toBe(account.body[0].id); }); }); }); it("can filter transactions by type", async () => { const auth = await request(app).post("/login").send(testUser); const transactionType = await request(app) .get("/transaction-types") .set("Authorization", `Bearer ${auth.body.accessToken}`) .expect(200) .expect(({ body }) => expect(body).not.toHaveLength(0)); await request(app) .get(`/transactions?type=${transactionType.body[0].name}`) .set("Authorization", `Bearer ${auth.body.accessToken}`) .expect(200) .expect(({ body }) => expect(body.data).not.toHaveLength(0)) .expect(({ body }) => { body.data.forEach(({ type }: Transaction) => { expect(type).toBe(transactionType.body[0].name); }); }); }); });