Skip to content

Commit

Permalink
Add tests for makeOrDefault()
Browse files Browse the repository at this point in the history
  • Loading branch information
aedart committed Mar 26, 2024
1 parent d782a67 commit cdf5822
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/browser/packages/support/container/container/make.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,68 @@ describe('@aedart/support/container', () => {
.toThrowError(NotFoundError);
});
});

describe('makeOrDefault', () => {

it('resolves when binding exists', () => {
const container = new Container();

container.bind('a', () => 'b');

// -------------------------------------------------------------------- //

const result = container.makeOrDefault('a');

expect(result)
.toBe('b');
});

it('resolves buildable identifier, when binding does not exist', () => {
const container = new Container();

class Bar {}

// -------------------------------------------------------------------- //

const result = container.makeOrDefault(Bar);

expect(result)
.toBeInstanceOf(Bar);
});

it('returns default value, when binding does not exist', () => {
const container = new Container();

// -------------------------------------------------------------------- //

const result = container.makeOrDefault('api', [], 'default');

expect(result)
.toBe('default');
});

it('invokes callback when given as default value', () => {
const container = new Container();

class Service {
name;

constructor(name) {
this.name = name;
}
}

// -------------------------------------------------------------------- //

const name = 'My Service';
const result = container.makeOrDefault('api', [ name ], (c, args) => {
return new Service(...args)
});

expect(result)
.toBeInstanceOf(Service);
expect(result.name)
.toBe(name);
});
});
});

0 comments on commit cdf5822

Please sign in to comment.