Skip to content

Commit

Permalink
feat(inversify-code-examples): add isBound examples
Browse files Browse the repository at this point in the history
  • Loading branch information
notaphplover committed Dec 31, 2024
1 parent db161bb commit 3b92b7a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, it } from '@jest/globals';

import {
isKatanaBound,
isKatanaSymbolBound,
isNinjaBound,
isWarriorSymbolBound,
} from './containerApiIsBound';

describe('Container API (isBound)', () => {
it('should detect bound and not bound services', async () => {
expect(isKatanaBound).toBe(false);
expect(isKatanaSymbolBound).toBe(false);
expect(isNinjaBound).toBe(true);
expect(isWarriorSymbolBound).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Container, injectable } from 'inversify';

// Begin-example
interface Warrior {
kind: string;
}

const katanaSymbol: symbol = Symbol.for('Katana');
const warriorSymbol: symbol = Symbol.for('Warrior');

@injectable()
class Ninja implements Warrior {
public readonly kind: string = 'ninja';
}

@injectable()
class Katana {}

const container: Container = new Container();
container.bind<Warrior>(Ninja).to(Ninja);
container.bind<Warrior>(warriorSymbol).to(Ninja);

// returns true
const isNinjaBound: boolean = container.isBound(Ninja);
// returns true
const isWarriorSymbolBound: boolean = container.isBound(warriorSymbol);
// returns false
const isKatanaBound: boolean = container.isBound(Katana);
// returns false
const isKatanaSymbolBound: boolean = container.isBound(katanaSymbol);

// End-example

export {
isKatanaBound,
isKatanaSymbolBound,
isNinjaBound,
isWarriorSymbolBound,
};

0 comments on commit 3b92b7a

Please sign in to comment.