Home Manual Reference Source Test Repository

test/application_test.js

'use strict';

import {expect} from 'chai';
import {describe, it} from 'mocha';
import {Application, app} from '../src/index';

/**
 * @test {Application}
 */
describe('Application', () => {

  /**
   * @test {Application#address}
   */
  describe('#address', () => {
    it('should have an "any IPv4" address as the default address', () => {
      expect((new Application).address).to.equal(Application.DEFAULT_ADDRESS);
    });

    it('should be the value of the underlying "address" setting', () => {
      let app = new Application;
      app._options.set('address', '192.168.0.1');
      expect(app.address).to.equal('192.168.0.1');
    });
  });

  /**
   * @test {Application#debug}
   */
  describe('#debug', () => {
    it('should be `false` in production environment', () => {
      process.env.NODE_ENV = 'production';
      expect((new Application).debug).to.be.false;
    });

    it('should be `true` in development environment', () => {
      process.env.NODE_ENV = 'development';
      expect((new Application).debug).to.be.true;
    });
  });

  /**
   * @test {Application#env}
   */
  describe('#env', () => {
    it('should be "development" if the `NODE_ENV` environment variable is not set', () => {
      delete process.env.NODE_ENV;
      expect((new Application).env).to.equal('development');
    });

    it('should equal the value of `NODE_ENV` environment variable when it is set', () => {
      process.env.NODE_ENV = 'production';
      expect((new Application).env).to.equal('production');
    });
  });

  /**
   * @test {Application#listening}
   */
  describe('#listening', () => {
    it('should return whether the application is listening', async () => {
      let app = new Application;
      app.init({address: '127.0.0.1'});

      expect(app.listening).to.be.false;
      await app.listen();
      expect(app.listening).to.be.true;
      await app.close();
      expect(app.listening).to.be.false;
    });
  });

  /**
   * @test {Application#name}
   */
  describe('#name', () => {
    it('should be "My Application" by default', () => {
      expect((new Application).name).to.equal('My Application');
    });

    it('should be the value of the underlying "name" setting', () => {
      let app = new Application;
      app._options.set('name', 'FooBar');
      expect(app.name).to.equal('FooBar');
    });
  });

  /**
   * @test {Application#port}
   */
  describe('#port', () => {
    it('should be `3000` by default', () => {
      expect((new Application).port).to.equal(Application.DEFAULT_PORT);
    });

    it('should be the value of the underlying "port" setting', () => {
      let app = new Application;
      app._options.set('port', 9000);
      expect(app.port).to.equal(9000);
    });
  });
});

/**
 * @test {app}
 */
describe('app()', () => {
  it('should return a `null` reference by default', () => {
    delete global._app;
    expect(app()).to.be.null;
  });

  it('should return a singleton instance when the application is started', () => {
    let application = new Application;
    expect(app()).to.equal(application);
    expect(app()).to.equal(application);
  });
});