Skip to content

Commit

Permalink
feat: Make auto-register support same injectable being exported multi…
Browse files Browse the repository at this point in the history
…ple times
  • Loading branch information
Iku-turso committed Sep 6, 2023
1 parent 33990e7 commit a1527e3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import { flatMap, forEach, tap } from 'lodash/fp';
import {
flatMap,
forEach,
tap,
uniq,
values,
filter,
overSome,
} from 'lodash/fp';

import { pipeline } from '@ogre-tools/fp';

import {
isInjectable,
isInjectableBunch,
toFlatInjectables,
} from '@ogre-tools/injectable';

import requireContextFake from './requireContextFake';

const toInjectables = module =>
toFlatInjectables(
Object.values(module).filter(
exported => isInjectable(exported) || isInjectableBunch(exported),
),
pipeline(
module,
values,
filter(overSome([isInjectable, isInjectableBunch])),
toFlatInjectables,
uniq,
);

const getFileNameAndModule = requireContext =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,40 @@ describe('autoRegister', () => {
expect(actual).toBe('some-injected-instance');
});

it('given file with same injectable exported multiple times, and auto-registered, when the injectable is injected, does so', () => {
const injectableStub = getInjectable({
id: 'irrelevant',
instantiate: () => 'some-injected-instance',
});

const requireStub = {
context: getSafeFrom({
'some-directory/': getRequireContextStub({
default: injectableStub,
someNamedExport: injectableStub,
}),
}),
};

const di = createContainer('some-container');

autoRegister({
di,

targetModule: {
require: requireStub,
},

getRequireContexts: () => [
requireStub.context('some-directory/', true, /\.injectable\.js$/),
],
});

const actual = di.inject(injectableStub);

expect(actual).toBe('some-injected-instance');
});

it('given file with injectable bunch as export and auto-registered, when an injectable in bunch is injected, does so', () => {
const someInjectableBunch = getInjectableBunch({
someInjectable: {
Expand Down

0 comments on commit a1527e3

Please sign in to comment.