const chai = require('chai'); const chaiHttp = require('chai-http'); const express = require('express'); const models = require('../../../models'); import { SetupApp } from '../../../app'; import { expect } from 'chai'; import * as helpers from './helpers'; async function init() { chai.use(chaiHttp); const app = express(); SetupApp(app); await models.sequelize.sync({ force: true }); return app; } describe('POST /song with no title', () => { it('should fail', done => { init().then((app) => { chai .request(app) .post('/song') .send({}) .then((res) => { expect(res).to.have.status(400); done(); }); }) }); }); describe('POST /song with only a title', () => { it('should return the first available id', done => { init().then(async(app) => { chai .request(app) .post('/song') .send({ title: "MySong" }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ id: 1 }); done(); }); }) }); }); describe('POST /song with a nonexistent artist Id', () => { it('should fail', done => { init().then(async (app) => { chai .request(app) .post('/song') .send({ title: "MySong", artistIds: [1] }) .then((res) => { expect(res).to.have.status(400); done(); }); }) }); }); describe('POST /song with an existing artist Id', () => { it('should succeed', done => { init().then((app) => { var req = chai.request(app).keepOpen(); helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 }) .then(() => helpers.createSong(req, { title: "MySong", artistIds: [ 1 ] }, 200, { id: 1 }) ) .then(req.close) .then(done); }); }); }); describe('POST /song with two existing artist Ids', () => { it('should succeed', done => { init().then((app) => { var req = chai.request(app).keepOpen(); helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) .then(() => helpers.createArtist(req, { name: "Artist2" }, 200, { id: 2 }) ) .then(() => helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 200, { id: 1 }) ) .then(req.close) .then(done); }); }); }); describe('POST /song with an existent and a nonexistent artist Id', () => { it('should fail', done => { init().then((app) => { var req = chai.request(app).keepOpen(); helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) .then(() => helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 400) ) .then(req.close) .then(done); }); }); }); export { };