|
1 | 1 | import { it, describe, expect } from "vitest"; |
2 | 2 | import { TEST_SERVER_URL } from "./global.setup"; |
3 | 3 | import { resetDatabase } from "../database"; |
4 | | -import {PendingProposal, Proposal} from "../types/proposal"; |
| 4 | +import {Proposal} from "../types/proposal"; |
5 | 5 | import { seedDatabase } from "./helpers/seedDatabase"; |
6 | 6 | import { TEST_USER } from "../helpers"; |
7 | 7 | import assertDatabaseHas from './helpers/assertDatabaseHas'; |
8 | | -import ProposalsService from '../services/proposals'; |
| 8 | +import DraftsService from "../services/drafts"; |
9 | 9 |
|
10 | 10 | const seedDb = seedDatabase(); |
11 | 11 |
|
| 12 | + |
| 13 | +describe('test suite works', () => { |
| 14 | + it("should hit health check", async () => { |
| 15 | + const res = await fetch(`${TEST_SERVER_URL}/health`); |
| 16 | + expect(res.status).toEqual(200); |
| 17 | + }); |
| 18 | +}) |
| 19 | + |
| 20 | +/**************************************** |
| 21 | + * PROPOSALS |
| 22 | + ****************************************/ |
| 23 | + |
12 | 24 | describe("Proposals API", () => { |
13 | 25 | it("returns 404 on no proposals", async () => { |
14 | 26 | await resetDatabase(); |
@@ -145,3 +157,74 @@ describe("Proposals API", () => { |
145 | 157 | }); |
146 | 158 | }); |
147 | 159 | }); |
| 160 | + |
| 161 | +/**************************************** |
| 162 | + * DRAFTS |
| 163 | + ****************************************/ |
| 164 | + |
| 165 | +describe('smoke tests', () => { |
| 166 | + it('index route 404 on empty db', async () => { |
| 167 | + await resetDatabase(); |
| 168 | + const res = await fetch(`${TEST_SERVER_URL}/drafts`); |
| 169 | + const data = await res.json(); |
| 170 | + expect(res.status).toEqual(404); |
| 171 | + }) |
| 172 | + |
| 173 | + it('store route', async () => { |
| 174 | + const res = await fetch(`${TEST_SERVER_URL}/drafts/`, { |
| 175 | + method: 'POST', |
| 176 | + body: JSON.stringify({}) |
| 177 | + }); |
| 178 | + expect(res.status).toEqual(201); |
| 179 | + }) |
| 180 | +}) |
| 181 | + |
| 182 | +describe('factory and count services', () => { |
| 183 | + it('should create and count drafts', async () => { |
| 184 | + await resetDatabase() |
| 185 | + const email = 'test@example.com'; |
| 186 | + const customTitle = 'Custom Title'; |
| 187 | + const draftData = DraftsService.factory({ title: customTitle }); |
| 188 | + await DraftsService.store(draftData, email); |
| 189 | + const count = await DraftsService.count(); |
| 190 | + expect(count).toBeGreaterThan(0); |
| 191 | + await assertDatabaseHas("drafts", { title: customTitle }); |
| 192 | + }); |
| 193 | +}); |
| 194 | + |
| 195 | +/**************************************** |
| 196 | + * WINNER |
| 197 | + ****************************************/ |
| 198 | + |
| 199 | +describe("winner endpoint", () => { |
| 200 | + it("should return leading proposal and mark closed", async () => { |
| 201 | + await resetDatabase(); |
| 202 | + const proposals = await seedDb.addProposals(2); |
| 203 | + const winningProposal = proposals[0]; |
| 204 | + await seedDb.addUserVote(winningProposal.id, undefined, 2); |
| 205 | + await seedDb.addUserVote(proposals[1].id, undefined, -2) |
| 206 | + const res = await fetch(`${TEST_SERVER_URL}/winner`); |
| 207 | + expect(res.status).toBe(200); |
| 208 | + const data = await res.json(); |
| 209 | + expect(data.id).toEqual(winningProposal.id); |
| 210 | + expect(data.status).toBe('closed'); |
| 211 | + }) |
| 212 | +}) |
| 213 | + |
| 214 | +describe("winner endpoint - tied proposals", () => { |
| 215 | + it("should return a random winner in case of tie", async () => { |
| 216 | + await resetDatabase(); |
| 217 | + const proposals = await seedDb.addProposals(3); |
| 218 | + for (const proposal of proposals) { |
| 219 | + await seedDb.addUserVote(proposal.id, undefined, 2) |
| 220 | + } |
| 221 | + const res = await fetch(`${TEST_SERVER_URL}/winner`); |
| 222 | + expect(res.status).toBe(200); |
| 223 | + const data = await res.json(); |
| 224 | + expect(data.status).toBe('closed'); |
| 225 | + const remainingProposals = proposals.filter(p => p.id !== data.id); |
| 226 | + for (const proposal of remainingProposals) { |
| 227 | + expect(proposal.status).toBe('open'); |
| 228 | + } |
| 229 | + }) |
| 230 | +}) |
0 commit comments