-
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.
Merge pull request #172 from inversify/refactor/update-instance-resol…
…ution-with-post-construct-checks Update instance resolution with post construct checks
- Loading branch information
Showing
6 changed files
with
755 additions
and
22 deletions.
There are no files selected for viewing
263 changes: 263 additions & 0 deletions
263
packages/container/libraries/core/src/resolution/actions/resolveBindingActivations.spec.ts
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,263 @@ | ||
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals'; | ||
|
||
import { ServiceIdentifier } from '@inversifyjs/common'; | ||
|
||
import { BindingActivation } from '../../binding/models/BindingActivation'; | ||
import { ResolutionParams } from '../models/ResolutionParams'; | ||
import { resolveBindingActivations } from './resolveBindingActivations'; | ||
|
||
describe(resolveBindingActivations.name, () => { | ||
describe('having a non promise value', () => { | ||
let paramsMock: jest.Mocked<ResolutionParams>; | ||
let serviceIdentifierFixture: ServiceIdentifier; | ||
let valueFixture: unknown; | ||
|
||
beforeAll(() => { | ||
paramsMock = { | ||
getActivations: jest.fn(), | ||
} as Partial< | ||
jest.Mocked<ResolutionParams> | ||
> as jest.Mocked<ResolutionParams>; | ||
serviceIdentifierFixture = 'service-id'; | ||
valueFixture = Symbol(); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns undefined', () => { | ||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
result = resolveBindingActivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
valueFixture, | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getActivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getActivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should return value', () => { | ||
expect(result).toBe(valueFixture); | ||
}); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns sync activations', () => { | ||
let activationMock: jest.Mock<BindingActivation>; | ||
let activationResult: unknown; | ||
|
||
let result: unknown; | ||
|
||
beforeAll(() => { | ||
activationResult = Symbol('activation-result'); | ||
|
||
activationMock = jest.fn().mockReturnValueOnce(activationResult); | ||
|
||
paramsMock.getActivations.mockReturnValueOnce([activationMock]); | ||
|
||
result = resolveBindingActivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
valueFixture, | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getActivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getActivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should call activation', () => { | ||
expect(activationMock).toHaveBeenCalledTimes(1); | ||
expect(activationMock).toHaveBeenCalledWith(valueFixture); | ||
}); | ||
|
||
it('should return value', () => { | ||
expect(result).toBe(activationResult); | ||
}); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns async activations', () => { | ||
let activationMock: jest.Mock<BindingActivation>; | ||
let activationResult: unknown; | ||
|
||
let result: unknown; | ||
|
||
beforeAll(async () => { | ||
activationResult = Symbol('activation-result'); | ||
|
||
activationMock = jest | ||
.fn() | ||
.mockReturnValueOnce(Promise.resolve(activationResult)); | ||
|
||
paramsMock.getActivations.mockReturnValueOnce([activationMock]); | ||
|
||
result = await resolveBindingActivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
valueFixture, | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getActivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getActivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should call activation', () => { | ||
expect(activationMock).toHaveBeenCalledTimes(1); | ||
expect(activationMock).toHaveBeenCalledWith(valueFixture); | ||
}); | ||
|
||
it('should return value', () => { | ||
expect(result).toBe(activationResult); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('having a promise value', () => { | ||
let paramsMock: jest.Mocked<ResolutionParams>; | ||
let serviceIdentifierFixture: ServiceIdentifier; | ||
let valueFixture: unknown; | ||
|
||
beforeAll(() => { | ||
paramsMock = { | ||
getActivations: jest.fn(), | ||
} as Partial< | ||
jest.Mocked<ResolutionParams> | ||
> as jest.Mocked<ResolutionParams>; | ||
serviceIdentifierFixture = 'service-id'; | ||
valueFixture = Symbol(); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns undefined', () => { | ||
let result: unknown; | ||
|
||
beforeAll(async () => { | ||
result = await resolveBindingActivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
Promise.resolve(valueFixture), | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getActivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getActivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should return value', () => { | ||
expect(result).toBe(valueFixture); | ||
}); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns sync activations', () => { | ||
let activationMock: jest.Mock<BindingActivation>; | ||
let activationResult: unknown; | ||
|
||
let result: unknown; | ||
|
||
beforeAll(async () => { | ||
activationResult = Symbol('activation-result'); | ||
|
||
activationMock = jest.fn().mockReturnValueOnce(activationResult); | ||
|
||
paramsMock.getActivations.mockReturnValueOnce([activationMock]); | ||
|
||
result = await resolveBindingActivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
Promise.resolve(valueFixture), | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getActivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getActivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should call activation', () => { | ||
expect(activationMock).toHaveBeenCalledTimes(1); | ||
expect(activationMock).toHaveBeenCalledWith(valueFixture); | ||
}); | ||
|
||
it('should return value', () => { | ||
expect(result).toBe(activationResult); | ||
}); | ||
}); | ||
|
||
describe('when called, and params.getActivations() returns async activations', () => { | ||
let activationMock: jest.Mock<BindingActivation>; | ||
let activationResult: unknown; | ||
|
||
let result: unknown; | ||
|
||
beforeAll(async () => { | ||
activationResult = Symbol('activation-result'); | ||
|
||
activationMock = jest | ||
.fn() | ||
.mockReturnValueOnce(Promise.resolve(activationResult)); | ||
|
||
paramsMock.getActivations.mockReturnValueOnce([activationMock]); | ||
|
||
result = await resolveBindingActivations( | ||
paramsMock, | ||
serviceIdentifierFixture, | ||
Promise.resolve(valueFixture), | ||
); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call params.getActivations', () => { | ||
expect(paramsMock.getActivations).toHaveBeenCalledTimes(1); | ||
expect(paramsMock.getActivations).toHaveBeenCalledWith( | ||
serviceIdentifierFixture, | ||
); | ||
}); | ||
|
||
it('should call activation', () => { | ||
expect(activationMock).toHaveBeenCalledTimes(1); | ||
expect(activationMock).toHaveBeenCalledWith(valueFixture); | ||
}); | ||
|
||
it('should return value', () => { | ||
expect(result).toBe(activationResult); | ||
}); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.