import mongoose from 'mongoose' import request from 'supertest' import { getTokenCookie } from '@dss-dev-mathis/dss-core-common' import app from '../../app' import Sheet from '../../models/sheet' import { createSheet } from '../../test/utils' describe('[Create New Sheet] POST /api/sheets', () => { it('should be a valid route', async () => { const response = await request(app).post('/api/sheets').send({}) expect(response.status).not.toEqual(404) }) it('should throw UnauthorizedError if unauthenticated', async () => { const response = await request(app).post('/api/sheets').send({}) expect(response.status).toEqual(401) }) it('should not get UnauthorizedError if authenticated', async () => { const cookie = await getTokenCookie({ id: new mongoose.Types.ObjectId().toHexString() }) const response = await request(app) .post('/api/sheets') .set('Cookie', [cookie]) .send({}) expect(response.status).not.toEqual(401) }) it('should throw error if new sheet has invalid title', async () => { const cookie = await getTokenCookie({ id: new mongoose.Types.ObjectId().toHexString() }) await request(app) .post('/api/sheets') .set('Cookie', [cookie]) .send({ title: '', price: 10 }) .expect(400) }) it('should create a new sheet', async () => { let sheets = await Sheet.find({}) const cookie = await getTokenCookie({ id: new mongoose.Types.ObjectId().toHexString() }) await request(app) .post('/api/sheets') .set('Cookie', [cookie]) .send({ title: 'Test Event', price: 20000 }) sheets = await Sheet.find({}) expect(sheets.length).toEqual(1) expect(sheets[0].title).toEqual('Test Event') expect(sheets[0].price).toEqual(20000) }) it('should publish a sheet:created event', async () => { const cookie = await getTokenCookie({ id: new mongoose.Types.ObjectId().toHexString() }) await createSheet(app, cookie) // TODO: Migrate to rabbitmq // expect(natsClient.client.publish).toHaveBeenCalled() }) })