All files / test api.spec.js

100% Statements 23/23
100% Branches 0/0
100% Functions 14/14
100% Lines 23/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56    1x 1x   1x 1x   1x 4x                   1x 4x     1x 1x 1x 1x   1x       1x 1x   1x         1x 1x 1x 1x   1x       1x 1x          
'use strict';
 
const rp = require('request-promise-native');
const Mockiji = require('../src/index');
 
describe('API', function() {
  let server = null;
 
  beforeEach(function() {
    server = new Mockiji({
      configuration: {
        api_base_path: 'test/data/api',
        port: this.getServerPort(),
        logs: [],
        silent: true,
      }
    });
  });
 
  afterEach(function(done) {
    server.stop().then(done, done);
  });
 
  describe('start method', function() {
    it('should start the server if called', function(done) {
      rp(`http://127.0.0.1:${this.getServerPort()}/api/`).
      then(done.fail, () => server.start()).
      catch(done.fail).
      then(() => rp(`http://127.0.0.1:${this.getServerPort()}/api/`)).
      then(done, done.fail);
    });
 
    it('should reject the promise if the server is already running', function(done) {
      server.start().
      catch(done.fail).
      then(() => server.start()).
      then(done.fail, done);
    });
  });
 
  describe('stop method', function() {
    it('should stop the server if called', function(done) {
      server.start().
      then(() => server.stop()).
      catch(done.fail).
      then(() => rp(`http://127.0.0.1:${this.getServerPort()}/api/`)).
      then(done.fail, done);
    });
 
    it('should reject the promise if the server is not running', function(done) {
      server.stop().
      then(done.fail, done);
    });
  });
});