diff --git a/test/api.test.js b/test/api.test.js index 9c96137..c845592 100644 --- a/test/api.test.js +++ b/test/api.test.js @@ -1,83 +1,37 @@ -var sinon = require('sinon') -var request = require('supertest') -var createApi = require('../src/api') -var error = require('../src/error') +const sinon = require('sinon'); +const request = require('supertest'); +const createApi = require('../src/api'); -describe('api: POST /', function () { - var api - beforeEach(function(){ - api = createApi(function(){}, { - token: '1234' - }) - }) +describe('api: POST /', () => { + let api; - it('should return 401 if no token is given', function(done) { - request(api) - .post('/') - .expect(401, done) - }) + beforeEach(() => { + api = createApi(() => {}, { token: '1234' }); + }); - it('should return 401 if invalid token is give', function (done) { - request(api) - .post('/') - .set('Authorization', 'Bearer test') - .expect(401, done) - }) + it('should return 401 if no token is given', async () => { + await request(api).post('/').expect(401); + }); - it('should return 422 on errorneous responses', function(done) { - queue = function () { - return { - addToQueue: function() { - return new Promise(function (resolve) { - resolve({ - code: '001', - error: true - }) - }) - }, - close: function(){} - } - } - var api = createApi(queue, { - token: '1234' - }) + it('should return 401 if invalid token is given', async () => { + await request(api).post('/').set('Authorization', 'Bearer test').expect(401); + }); - request(api) - .post('/') - .set('Authorization', 'Bearer 1234') - .send({}) - .expect(422, done) - }) + it('should return 422 on erroneous responses', async () => { + const queue = { addToQueue: () => Promise.resolve({ code: '001', error: true }), close: () => {} }; + const api = createApi(queue, { token: '1234' }); - it('should run the queue with the correct params', function (done) { - var meta = {id: 1} + await request(api).post('/').set('Authorization', 'Bearer 1234').send({}).expect(422); + }); - var addToQueue = sinon.stub() - addToQueue.onCall(0).returns(new Promise(function (resolve) { resolve({ id: '1234' }) })) + it('should run the queue with the correct params', async () => { + const meta = { id: 1 }; + const addToQueue = sinon.stub().resolves({ id: '1234' }); + const queue = { addToQueue, close: () => {} }; + const api = createApi(queue, { token: '1234' }); - var queue = function() { - return { - addToQueue: addToQueue, - close: function(){} - } - } - var api = createApi(queue, { - token: '1234' - }) + await request(api).post('/').set('Authorization', 'Bearer 1234').send({ url: 'https://google.com', meta: meta }).expect(201); - request(api) - .post('/') - .set('Authorization', 'Bearer 1234') - .send({ url: 'https://google.com', meta: meta }) - .expect(201) - .end(function (err, res) { - if (err) return done(err) - - if (!addToQueue.calledWith({ url: 'https://google.com', meta: meta })) { - throw new Error('Queue was not called with correct url') - } - - done() - }) - }) -}) + sinon.assert.calledWith(addToQueue, { url: 'https://google.com', meta: meta }); + }); +});