-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#377): respect of providedIn in Injectable
- Loading branch information
Showing
7 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
libs/ng-mocks/src/lib/common/core.reflect.provided-in.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,18 @@ | ||
import coreReflectProvidedIn from './core.reflect.provided-in'; | ||
|
||
describe('core.reflect.provided-in', () => { | ||
it('covers ngInjectableDef', () => { | ||
expect( | ||
coreReflectProvidedIn({ | ||
ngInjectableDef: {}, | ||
}), | ||
).toEqual(undefined); | ||
expect( | ||
coreReflectProvidedIn({ | ||
ngInjectableDef: { | ||
providedIn: 'root', | ||
}, | ||
}), | ||
).toEqual('root'); | ||
}); | ||
}); |
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,5 @@ | ||
import { AnyType } from './core.types'; | ||
|
||
export default (declaration: any): undefined | AnyType<any> | string => { | ||
return declaration?.ɵprov?.providedIn ?? declaration?.ngInjectableDef?.providedIn; | ||
}; |
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
17 changes: 17 additions & 0 deletions
17
libs/ng-mocks/src/lib/mock-builder/promise/handle-provided-in-dependency.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,17 @@ | ||
import coreReflectProvidedIn from '../../common/core.reflect.provided-in'; | ||
import ngMocksUniverse from '../../common/ng-mocks-universe'; | ||
|
||
export default (provide: any): void => { | ||
if (ngMocksUniverse.touches.has(provide)) { | ||
return; | ||
} | ||
|
||
const providedIn = coreReflectProvidedIn(provide); | ||
if (!providedIn) { | ||
return; | ||
} | ||
|
||
if (ngMocksUniverse.config.get('ngMocksDepsSkip').has(providedIn)) { | ||
ngMocksUniverse.config.get('ngMocksDepsSkip').add(provide); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
Component, | ||
Inject, | ||
Injectable as InjectableSource, | ||
InjectionToken, | ||
NgModule, | ||
VERSION, | ||
} from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
// Because of A5 we need to cast Injectable to any type. | ||
// But because of A10+ we need to do it via a middle function. | ||
function Injectable(...args: any[]): any { | ||
return InjectableSource(...args); | ||
} | ||
|
||
@NgModule({}) | ||
class TargetModule {} | ||
|
||
@Injectable({ | ||
providedIn: TargetModule, | ||
} as any) | ||
class TargetService { | ||
private readonly name = 'target'; | ||
|
||
public echo(): string { | ||
return this.name; | ||
} | ||
} | ||
|
||
// TODO Remove any with A5 | ||
const TOKEN = new (InjectionToken as any)('TOKEN', { | ||
factory: () => 'TOKEN', | ||
providedIn: TargetModule, | ||
}); | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: `service:{{ service.echo() }} token:{{ token }}`, | ||
}) | ||
class TargetComponent { | ||
public constructor( | ||
public readonly service: TargetService, | ||
@Inject(TOKEN) public readonly token: string, | ||
) {} | ||
} | ||
|
||
describe('issue-377', () => { | ||
beforeEach(() => { | ||
if (parseInt(VERSION.major, 10) <= 5) { | ||
pending('Need Angular > 5'); | ||
} | ||
}); | ||
|
||
describe('expected', () => { | ||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
declarations: [TargetComponent], | ||
imports: [TargetModule], | ||
}).compileComponents(), | ||
); | ||
|
||
it('sets TestBed correctly', () => { | ||
const fixture = MockRender(TargetComponent); | ||
expect(ngMocks.formatText(fixture)).toEqual( | ||
'service:target token:TOKEN', | ||
); | ||
}); | ||
}); | ||
|
||
describe('keep', () => { | ||
beforeEach(() => MockBuilder(TargetComponent).keep(TargetModule)); | ||
|
||
it('sets TestBed correctly', () => { | ||
const fixture = MockRender(TargetComponent); | ||
expect(ngMocks.formatText(fixture)).toEqual( | ||
'service:target token:TOKEN', | ||
); | ||
}); | ||
}); | ||
|
||
describe('mock', () => { | ||
beforeEach(() => MockBuilder(TargetComponent).mock(TargetModule)); | ||
|
||
it('sets TestBed correctly', () => { | ||
const fixture = MockRender(TargetComponent); | ||
expect(ngMocks.formatText(fixture)).toEqual('service: token:'); | ||
}); | ||
}); | ||
}); |
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,43 @@ | ||
import { Component } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; | ||
import { MockBuilder, MockRender } from 'ng-mocks'; | ||
|
||
@Component({ | ||
selector: 'app-form', | ||
template: ` | ||
<form [formGroup]="form"> | ||
<input type="text" formGroupName="name" /> | ||
</form> | ||
`, | ||
}) | ||
class FormComponent { | ||
public form = this.fb.group({ | ||
name: [], | ||
}); | ||
|
||
public constructor(private readonly fb: FormBuilder) {} | ||
} | ||
|
||
describe('issue-377:classic', () => { | ||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
declarations: [FormComponent], | ||
imports: [ReactiveFormsModule], | ||
}).compileComponents(), | ||
); | ||
|
||
it('sets TestBed correctly', () => { | ||
expect(() => MockRender(FormComponent)).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('issue-377:mock', () => { | ||
beforeEach(() => | ||
MockBuilder(FormComponent).keep(ReactiveFormsModule), | ||
); | ||
|
||
it('sets TestBed correctly', () => { | ||
expect(() => MockRender(FormComponent)).not.toThrow(); | ||
}); | ||
}); |