-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...s/injectable/src/dependency-injection-container/createContainer.late-registration.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import getDi from '../test-utils/getDiForUnitTesting'; | ||
import getInjectable from '../getInjectable/getInjectable'; | ||
|
||
describe('createContainer.late-registration', () => { | ||
it('given injectable registered by another injectable during injection, when thus registered injectable is injected, injects', async () => { | ||
const someInjectableForlateRegistration = getInjectable({ | ||
id: 'some-injectable-for-late-registration', | ||
|
||
instantiate: di => { | ||
di.register(someInjectableToBeRegisteredLate); | ||
}, | ||
}); | ||
|
||
const someInjectableToBeRegisteredLate = getInjectable({ | ||
id: 'some-injectable-to-be-registered-late', | ||
|
||
instantiate: () => 'some-instance', | ||
}); | ||
|
||
const di = getDi( | ||
someInjectableForlateRegistration, | ||
// Notice: the injectable is not registered before late. | ||
// someInjectableToBeRegisteredLate, | ||
); | ||
|
||
di.inject(someInjectableForlateRegistration); | ||
|
||
const actual = di.inject(someInjectableToBeRegisteredLate); | ||
|
||
expect(actual).toBe('some-instance'); | ||
}); | ||
|
||
it('given injectable registered by another injectable during setup, when thus registered injectable is injected, injects', async () => { | ||
const someInjectableForlateRegistration = getInjectable({ | ||
id: 'some-injectable-for-late-registration', | ||
|
||
setup: di => { | ||
di.register(someInjectableToBeRegisteredLate); | ||
}, | ||
}); | ||
|
||
const someInjectableToBeRegisteredLate = getInjectable({ | ||
id: 'some-injectable-to-be-registered-late', | ||
|
||
instantiate: () => 'some-instance', | ||
}); | ||
|
||
const di = getDi( | ||
someInjectableForlateRegistration, | ||
// Notice: the injectable is not registered before late. | ||
// someInjectableToBeRegisteredLate, | ||
); | ||
|
||
await di.runSetups(); | ||
|
||
const actual = di.inject(someInjectableToBeRegisteredLate); | ||
|
||
expect(actual).toBe('some-instance'); | ||
}); | ||
|
||
it('given setups are already ran, when registering a setuppable, throws', async () => { | ||
const someSetuppableToBeRegisteredLate = getInjectable({ | ||
id: 'some-setuppable-to-be-registered-late', | ||
|
||
setup: () => {}, | ||
}); | ||
|
||
const di = getDi(); | ||
|
||
await di.runSetups(); | ||
|
||
expect(() => { | ||
di.register(someSetuppableToBeRegisteredLate); | ||
}).toThrow( | ||
'Tried to register setuppable "some-setuppable-to-be-registered-late" after setups have already ran.', | ||
); | ||
}); | ||
|
||
it('given setups are being ran, when a setuppable registers another setuppable, throws', async () => { | ||
const someSetuppable = getInjectable({ | ||
id: 'some-setuppable', | ||
|
||
setup: di => { | ||
di.register(someSetuppableToBeRegisteredLate); | ||
}, | ||
}); | ||
|
||
const someSetuppableToBeRegisteredLate = getInjectable({ | ||
id: 'some-setuppable-to-be-registered-late', | ||
|
||
setup: () => {}, | ||
}); | ||
|
||
const di = getDi(someSetuppable); | ||
|
||
return expect(di.runSetups()).rejects.toThrow( | ||
'Tried to register setuppable "some-setuppable-to-be-registered-late" during setup.', | ||
); | ||
}); | ||
}); |