67 lines
2.0 KiB
TypeScript
Executable File
67 lines
2.0 KiB
TypeScript
Executable File
import request from 'supertest';
|
|
import app from '../../src/app';
|
|
import { pool } from '../../src/db';
|
|
import { resetDb, closeDb } from './setup/testDb';
|
|
|
|
beforeEach(async () => {
|
|
await resetDb();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await pool.end();
|
|
await closeDb();
|
|
});
|
|
|
|
describe('GET /api/items', () => {
|
|
it('returns an empty array when there are no items', async () => {
|
|
const res = await request(app).get('/api/items');
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toEqual([]);
|
|
});
|
|
|
|
it('returns items with an empty images array when none are attached', async () => {
|
|
await pool.query(
|
|
`INSERT INTO items (name, description, price_cents) VALUES ('Vintage Lamp', 'A one-of-a-kind lamp.', 4500)`
|
|
);
|
|
const res = await request(app).get('/api/items');
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toHaveLength(1);
|
|
expect(res.body[0]).toMatchObject({
|
|
name: 'Vintage Lamp',
|
|
price_cents: 4500,
|
|
status: 'available',
|
|
images: []
|
|
});
|
|
});
|
|
|
|
it('returns 404 for a nonexistent item', async () => {
|
|
const res = await request(app).get('/api/items/99999');
|
|
expect(res.status).toBe(404);
|
|
});
|
|
});
|
|
|
|
describe('demo checkout', () => {
|
|
it('marks an available item as sold', async () => {
|
|
const { rows } = await pool.query(
|
|
`INSERT INTO items (name, price_cents) VALUES ('Test Item', 1000) RETURNING id`
|
|
);
|
|
const itemId = rows[0].id;
|
|
|
|
const res = await request(app).post(`/api/checkout/demo/${itemId}/purchase`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.status).toBe('sold');
|
|
|
|
const check = await request(app).get(`/api/items/${itemId}`);
|
|
expect(check.body.status).toBe('sold');
|
|
});
|
|
|
|
it('refuses to sell an item that is already sold', async () => {
|
|
const { rows } = await pool.query(
|
|
`INSERT INTO items (name, price_cents, status) VALUES ('Already Sold', 1000, 'sold') RETURNING id`
|
|
);
|
|
const itemId = rows[0].id;
|
|
const res = await request(app).post(`/api/checkout/demo/${itemId}/purchase`);
|
|
expect(res.status).toBe(409);
|
|
});
|
|
});
|