Home Manual Reference Source Test Repository

test/di_test.js

'use strict';

import {Configuration} from '@cedx/coveralls';
import {expect} from 'chai';
import {describe, it, Runnable} from 'mocha';

import {createObject, registerType, setAlias} from '../src/index';
import {SampleClass} from './fixtures/sample_class';

/**
 * @test {createObject}
 */
describe('createObject()', () => {
  it('should throw an error if the type is not resolvable', () => {
    expect(() => createObject(123)).to.throw(TypeError);
    expect(() => createObject('')).to.throw(TypeError);

    setAlias('@test', __dirname);
    expect(() => createObject('@test/FooBar')).to.throw(TypeError);
    expect(() => createObject({type: '@test/FooBar'})).to.throw(TypeError);
  });

  it('should return instances of any class from NPM packages', () => {
    expect(createObject('@npm/mocha/Runnable')).to.be.instanceOf(Runnable);
    expect(createObject('@npm/@cedx/coveralls/Configuration')).to.be.instanceOf(Configuration);
    expect(createObject({class: '@npm/@cedx/coveralls/Configuration'})).to.be.instanceOf(Configuration);
  });

  it('should return instances of any class from local modules', () => {
    setAlias('@test', __dirname);
    expect(createObject('@test/fixtures/sample_class/SampleClass')).to.be.instanceOf(SampleClass);
    expect(createObject({class: '@test/fixtures/sample_class/SampleClass'})).to.be.instanceOf(SampleClass);
  });

  it('should handle the constructor parameters', () => {
    setAlias('@test', __dirname);

    let instance = createObject('@test/fixtures/sample_class/SampleClass', 'foo');
    expect(instance.sampleProperty).to.equal('foo');

    instance = createObject({class: '@test/fixtures/sample_class/SampleClass'}, 'bar');
    expect(instance.sampleProperty).to.equal('bar');
  });

  it('should handle the property initialization', () => {
    setAlias('@test', __dirname);

    let instance = createObject({class: '@test/fixtures/sample_class/SampleClass', sampleProperty: 'foo'});
    expect(instance.sampleProperty).to.equal('foo');

    instance = createObject({class: '@test/fixtures/sample_class/SampleClass', sampleProperty: 'bar'});
    expect(instance.sampleProperty).to.equal('bar');

    instance = createObject({class: '@test/../src/http_error/HTTPError', expose: false, message: 'FooBar', status: 400});
    expect(instance.expose).to.be.false;
    expect(instance.message).to.equal('FooBar');
    expect(instance.status).to.equal(400);
  });
});

/**
 * @test {registerType}
 */
describe('registerType()', () => {
  it('should throw an error if the class alias is invalid', () => {
    expect(() => registerType('')).to.throw(TypeError);
    expect(() => registerType('@fooBarBaz123456')).to.throw(TypeError);

    setAlias('@test', __dirname);
    expect(() => registerType('@test/FooBar')).to.throw(TypeError);
  });

  it('should return the constructor of any class from NPM packages', () => {
    expect(registerType('@npm/mocha/Runnable')).to.equal(Runnable);
    expect(registerType('@npm/@cedx/coveralls/Configuration')).to.equal(Configuration);
  });

  it('should return the constructor of any class from local modules', () => {
    setAlias('@test', __dirname);
    expect(registerType('@test/fixtures/sample_class/SampleClass')).to.equal(SampleClass);
  });
});