reactant-di
Version: 
A dependency injection lib for Reactant
79 lines (65 loc) • 1.55 kB
text/typescript
import { injectable, createContainer, multiInject } from '../..';
describe('multiInject', () => {
  test('explicit identifier', () => {
    
    class Foo {
      public get test() {
        return 'test';
      }
    }
    
    class Bar {
      constructor( public foos: Foo[]) {}
      public get length() {
        return this.foos.length;
      }
    }
    const bar = createContainer({
      ServiceIdentifiers: new Map(),
      modules: [Foo, Foo],
    }).get(Bar);
    expect(bar.length).toBe(2);
  });
  test('string identifier', () => {
    
    class Foo {
      public get test() {
        return 'test';
      }
    }
    
    class Bar {
      constructor( public foos: Foo[]) {}
      public get length() {
        return this.foos.length;
      }
    }
    const bar = createContainer({
      ServiceIdentifiers: new Map(),
      modules: [
        { provide: 'Foo', useClass: Foo },
        { provide: 'Foo', useValue: 'test' },
      ],
    }).get(Bar);
    expect(bar.length).toBe(2);
    expect(bar.foos[1]).toBe('test');
  });
  test('Unexpected multi-Inject', () => {
    
    class Foo {
      public get test() {
        return 'test';
      }
    }
    
    class Bar {
      constructor(public foos: Foo) {}
    }
    expect(() => {
      createContainer({
        ServiceIdentifiers: new Map(),
        modules: [Foo, Foo],
      }).get(Bar);
    }).toThrowErrorMatchingSnapshot();
  });
});