Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject empty array with @injectAll() in case of unregistered string or symbol token #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ class Foo {
Parameter decorator for array parameters where the array contents will come from the container.
It will inject an array using the specified injection token to resolve the values.

If nothing is registered for the specified token and the token is a string or a symbol, it will inject an empty array.
If nothing is registered for the specified token and the token is a class constructor, it will inject an array
with a single instance of the class.

#### Usage

```typescript
Expand Down Expand Up @@ -368,7 +372,7 @@ const myFoo = container.resolve(Foo);
const myBar = container.resolve<Bar>("Bar");
```

You can also resolve all instances registered against a given token with `resolveAll()`.
You can also resolve all instances registered against a given token with `**`resolveAll(`**)`.

```typescript
const myBars = container.resolveAll<Bar>("Bar"); // myBars type is Bar[]
Expand Down
17 changes: 13 additions & 4 deletions src/__tests__/global-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,19 @@ test("resolves anonymous classes separately", () => {

// --- resolveAll() ---

test("fails to resolveAll unregistered dependency by name", () => {
expect(() => {
globalContainer.resolveAll("NotRegistered");
}).toThrow();
test("resolves an empty array in case of unregistered dependency by name", () => {
const array = globalContainer.resolveAll("NotRegistered");
expect(Array.isArray(array)).toBeTruthy();
expect(array.length).toBe(0);
});

test("resolves an array of a single instance in case of unregistered dependency by class constructor", () => {
class Foo {}

const fooArray = globalContainer.resolveAll<Foo>(Foo);
expect(Array.isArray(fooArray)).toBeTruthy();
expect(fooArray.length).toBe(1);
expect(fooArray[0]).toBeInstanceOf(Foo);
});

test("resolves an array of transient instances bound to a single interface", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/dependency-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class InternalDependencyContainer implements DependencyContainer {
const registrations = this.getAllRegistrations(token);

if (!registrations && isNormalToken(token)) {
throw `Attempted to resolve unregistered dependency token: ${token.toString()}`;
return [];
}

if (registrations) {
Expand Down