-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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 #9915 from thiagomini/refactor/instance-wrapper
refactor(core): extract instance wrapper merge logic
- Loading branch information
Showing
4 changed files
with
235 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
ClassProvider, | ||
FactoryProvider, | ||
Provider, | ||
ValueProvider, | ||
} from '@nestjs/common'; | ||
import { isUndefined } from '@nestjs/common/utils/shared.utils'; | ||
|
||
export function isClassProvider<T = any>( | ||
provider: Provider, | ||
): provider is ClassProvider<T> { | ||
return Boolean((provider as ClassProvider<T>)?.useClass); | ||
} | ||
|
||
export function isValueProvider<T = any>( | ||
provider: Provider, | ||
): provider is ValueProvider<T> { | ||
const providerValue = (provider as ValueProvider)?.useValue; | ||
return !isUndefined(providerValue); | ||
} | ||
|
||
export function isFactoryProvider<T = any>( | ||
provider: Provider, | ||
): provider is FactoryProvider<T> { | ||
return Boolean((provider as FactoryProvider).useFactory); | ||
} |
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
132 changes: 132 additions & 0 deletions
132
packages/core/test/injector/helpers/provider-classifier.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,132 @@ | ||
import { ClassProvider, FactoryProvider, ValueProvider } from '@nestjs/common'; | ||
import { expect } from 'chai'; | ||
import { | ||
isClassProvider, | ||
isFactoryProvider, | ||
isValueProvider, | ||
} from '../../../injector/helpers/provider-classifier'; | ||
|
||
describe('provider classifier', () => { | ||
describe('isClassProvider', () => { | ||
it('should return true if useClass is present', () => { | ||
const classProvider: ClassProvider = { | ||
useClass: class TestClass {}, | ||
provide: 'token', | ||
}; | ||
|
||
expect(isClassProvider(classProvider)).to.be.true; | ||
}); | ||
|
||
it('should return false if useClass is undefined', () => { | ||
const classProvider: ClassProvider = { | ||
useClass: undefined, | ||
provide: 'token', | ||
}; | ||
|
||
expect(isClassProvider(classProvider)).to.be.false; | ||
}); | ||
|
||
it('should return false if useClass is not present', () => { | ||
const classProvider = { | ||
provide: 'token', | ||
}; | ||
|
||
expect(isClassProvider(classProvider as ClassProvider)).to.be.false; | ||
}); | ||
|
||
it('should return false if provider is undefined', () => { | ||
const classProvider = undefined; | ||
|
||
expect(isClassProvider(classProvider as ClassProvider)).to.be.false; | ||
}); | ||
}); | ||
|
||
describe('isValueProvider', () => { | ||
it('should return true if useValue is not undefined', () => { | ||
const valueProvider: ValueProvider = { | ||
useValue: 'value', | ||
provide: 'token', | ||
}; | ||
|
||
expect(isValueProvider(valueProvider)).to.be.true; | ||
}); | ||
|
||
it('should return true if useValue is "false"', () => { | ||
const valueProvider: ValueProvider = { | ||
useValue: false, | ||
provide: 'token', | ||
}; | ||
|
||
expect(isValueProvider(valueProvider)).to.be.true; | ||
}); | ||
|
||
it('should return true if useValue is "null"', () => { | ||
const valueProvider: ValueProvider = { | ||
useValue: null, | ||
provide: 'token', | ||
}; | ||
|
||
expect(isValueProvider(valueProvider)).to.be.true; | ||
}); | ||
|
||
it('should return true if useValue is an empty string', () => { | ||
const valueProvider: ValueProvider = { | ||
useValue: null, | ||
provide: '', | ||
}; | ||
|
||
expect(isValueProvider(valueProvider)).to.be.true; | ||
}); | ||
|
||
it('should return false if useValue is undefined', () => { | ||
const valueProvider: ValueProvider = { | ||
useValue: undefined, | ||
provide: 'token', | ||
}; | ||
|
||
expect(isValueProvider(valueProvider)).to.be.false; | ||
}); | ||
|
||
it('should return false if useValue is not present', () => { | ||
const valueProvider = { | ||
provide: 'token', | ||
}; | ||
|
||
expect(isValueProvider(valueProvider as ValueProvider)).to.be.false; | ||
}); | ||
|
||
it('should return false if provider is undefined', () => { | ||
const valueProvider = undefined; | ||
|
||
expect(isValueProvider(valueProvider as ValueProvider)).to.be.false; | ||
}); | ||
}); | ||
|
||
describe('isFactoryProvider', () => { | ||
it('should return true if useFactory is present', () => { | ||
const factoryProvider: FactoryProvider = { | ||
provide: 'token', | ||
useFactory: () => {}, | ||
}; | ||
|
||
expect(isFactoryProvider(factoryProvider)).to.be.true; | ||
}); | ||
|
||
it('should return false if useFactory is not present', () => { | ||
const factoryProvider = { | ||
provide: 'token', | ||
}; | ||
|
||
expect(isFactoryProvider(factoryProvider as FactoryProvider)).to.be.false; | ||
}); | ||
|
||
it('should return false if useFactory is undefined', () => { | ||
const factoryProvider: FactoryProvider = { | ||
provide: 'token', | ||
useFactory: undefined, | ||
}; | ||
|
||
expect(isFactoryProvider(factoryProvider as FactoryProvider)).to.be.false; | ||
}); | ||
}); | ||
}); |
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