You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

190 lines
5.5 KiB

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';
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({})
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(400);
done();
});
});
});
});
describe('POST /song with only a title', () => {
it('should return the first available id', done => {
init().then((app) => {
chai
.request(app)
.post('/song')
.send({
title: "MySong"
})
.end((err, res) => {
expect(err).to.be.null;
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((app) => {
chai
.request(app)
.post('/song')
.send({
title: "MySong",
artistIds: [1]
})
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(400);
done();
});
})
});
});
describe('POST /song with an existing artist Id', () => {
it('should succeed', done => {
init().then((app) => {
async function createArtist() {
await chai.request(app)
.post('/artist')
.send({
name: "MyArtist"
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
id: 1
});
});
}
async function createSong() {
chai.request(app)
.post('/song')
.send({
title: "MySong",
artistIds: [1]
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
id: 1
});
});
}
init()
.then(createArtist)
.then(createSong)
.then(done);
});
});
});
describe('POST /song with two existing artist Ids', () => {
it('should succeed', done => {
init().then((app) => {
async function createArtist(name, expectId) {
await chai.request(app)
.post('/artist')
.send({
name: name
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
id: expectId
});
});
}
async function createSong() {
chai.request(app)
.post('/song')
.send({
title: "MySong",
artistIds: [1, 2]
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
id: 1
});
});
}
init()
.then(() => { createArtist('Artist1', 1); })
.then(() => { createArtist('Artist2', 2); })
.then(createSong)
.then(done);
});
});
});
describe('POST /song with an existent and a nonexistent artist Id', () => {
it('should fail', done => {
init().then((app) => {
async function createArtist(name, expectId) {
await chai.request(app)
.post('/artist')
.send({
name: name
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
id: expectId
});
});
}
async function createSong() {
chai.request(app)
.post('/song')
.send({
title: "MySong",
artistIds: [1, 2]
})
.then((res) => {
expect(res).to.have.status(400);
});
}
init()
.then(() => { createArtist('Artist1', 1); })
.then(createSong)
.then(done);
});
});
});
export { };