-
-
Notifications
You must be signed in to change notification settings - Fork 86
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
22 changed files
with
1,326 additions
and
293 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,183 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { | ||
Component, | ||
Directive, | ||
EventEmitter, | ||
Inject, | ||
Injectable, | ||
InjectionToken, | ||
Input, | ||
NgModule, | ||
NO_ERRORS_SCHEMA, | ||
OnDestroy, | ||
Output, | ||
Pipe, | ||
PipeTransform, | ||
} from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { MockDirective, MockModule, MockPipe, MockService, ngMocks } from 'ng-mocks'; | ||
|
||
const TARGET1 = new InjectionToken('TARGET1'); | ||
|
||
@Injectable() | ||
class Target1Service { | ||
public callback: () => void = () => undefined; | ||
|
||
public touch(): void { | ||
this.callback(); | ||
} | ||
} | ||
|
||
@Pipe({ | ||
name: 'target1', | ||
}) | ||
class Target1Pipe implements PipeTransform { | ||
protected readonly name = 'pipe1'; | ||
public transform(value: string): string { | ||
return `${this.name}:${value}`; | ||
} | ||
} | ||
|
||
@Component({ | ||
selector: 'target2', | ||
template: `<ng-content></ng-content>`, | ||
}) | ||
class Target2Component {} | ||
|
||
@Component({ | ||
selector: 'target1', | ||
template: `<div (target1)="update.emit()"> | ||
{{ greeting }} {{ greeting | target1 }} <target2>{{ target }}</target2> | ||
</div>`, | ||
}) | ||
class Target1Component { | ||
@Input() public greeting: string | null = null; | ||
public readonly target: string; | ||
@Output() public readonly update: EventEmitter<void> = new EventEmitter(); | ||
|
||
constructor(@Inject(TARGET1) target: string) { | ||
this.target = target; | ||
} | ||
} | ||
|
||
@Directive({ | ||
selector: '[target1]', | ||
}) | ||
class Target1Directive implements OnDestroy { | ||
public readonly service: Target1Service; | ||
@Output() public readonly target1: EventEmitter<void> = new EventEmitter(); | ||
|
||
constructor(service: Target1Service) { | ||
this.service = service; | ||
this.service.callback = () => this.target1.emit(); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.service.callback = () => undefined; | ||
} | ||
} | ||
|
||
@NgModule({ | ||
declarations: [Target2Component], | ||
exports: [Target2Component], | ||
providers: [ | ||
{ | ||
provide: TARGET1, | ||
useValue: 'target1', | ||
}, | ||
], | ||
}) | ||
class Target2Module {} | ||
|
||
@NgModule({ | ||
declarations: [Target1Pipe, Target1Component, Target1Directive], | ||
imports: [CommonModule, Target2Module], | ||
providers: [ | ||
Target1Service, | ||
{ | ||
provide: TARGET1, | ||
useValue: 'target1', | ||
}, | ||
], | ||
}) | ||
class Target1Module {} | ||
|
||
describe('ngMocks.guts:NO_ERRORS_SCHEMA', () => { | ||
let fixture: ComponentFixture<Target1Component>; | ||
let component: Target1Component; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [Target1Component, Target1Pipe], | ||
imports: [CommonModule], | ||
providers: [ | ||
{ | ||
provide: TARGET1, | ||
useValue: 'target1', | ||
}, | ||
], | ||
schemas: [NO_ERRORS_SCHEMA], | ||
}); | ||
fixture = TestBed.createComponent(Target1Component); | ||
component = fixture.componentInstance; | ||
}); | ||
|
||
it('creates component', () => { | ||
expect(component).toEqual(jasmine.any(Target1Component)); | ||
expect(fixture.nativeElement.innerHTML).toEqual('<div><target2></target2></div>'); | ||
component.greeting = 'hello'; | ||
fixture.detectChanges(); | ||
expect(fixture.nativeElement.innerHTML).toContain('hello'); | ||
}); | ||
}); | ||
|
||
describe('ngMocks.guts:legacy', () => { | ||
let fixture: ComponentFixture<Target1Component>; | ||
let component: Target1Component; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [MockPipe(Target1Pipe), MockDirective(Target1Directive), Target1Component], | ||
imports: [CommonModule, MockModule(Target2Module)], | ||
providers: [ | ||
{ | ||
provide: Target1Service, | ||
useValue: MockService(Target1Service), | ||
}, | ||
{ | ||
provide: TARGET1, | ||
useValue: undefined, | ||
}, | ||
], | ||
}); | ||
fixture = TestBed.createComponent(Target1Component); | ||
component = fixture.componentInstance; | ||
}); | ||
|
||
it('creates component', () => { | ||
expect(component).toEqual(jasmine.any(Target1Component)); | ||
expect(fixture.nativeElement.innerHTML).toEqual('<div><target2></target2></div>'); | ||
component.greeting = 'hello'; | ||
fixture.detectChanges(); | ||
expect(fixture.nativeElement.innerHTML).toContain('hello'); | ||
}); | ||
}); | ||
|
||
describe('ngMocks.guts:normal', () => { | ||
let fixture: ComponentFixture<Target1Component>; | ||
let component: Target1Component; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule(ngMocks.guts(Target1Component, Target1Module)); | ||
fixture = TestBed.createComponent(Target1Component); | ||
component = fixture.componentInstance; | ||
}); | ||
|
||
it('creates component', () => { | ||
expect(component).toEqual(jasmine.any(Target1Component)); | ||
expect(fixture.nativeElement.innerHTML).toEqual('<div><target2></target2></div>'); | ||
component.greeting = 'hello'; | ||
fixture.detectChanges(); | ||
expect(fixture.nativeElement.innerHTML).toContain('hello'); | ||
}); | ||
}); |
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
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,30 @@ | ||
// tslint:disable:no-default-export no-default-import | ||
|
||
import { getTestBed, TestBed } from '@angular/core/testing'; | ||
|
||
import { ngMocksUniverse } from '../common/ng-mocks-universe'; | ||
|
||
import flushTestBed from './mock-helper.flushTestBed'; | ||
|
||
export default () => { | ||
beforeAll(() => { | ||
if (ngMocksUniverse.global.has('bullet:customized')) { | ||
TestBed.resetTestingModule(); | ||
} | ||
ngMocksUniverse.global.set('bullet', true); | ||
}); | ||
|
||
afterEach(() => { | ||
flushTestBed(); | ||
for (const fixture of (getTestBed() as any)._activeFixtures || /* istanbul ignore next */ []) { | ||
fixture.destroy(); | ||
} | ||
}); | ||
|
||
afterAll(() => { | ||
ngMocksUniverse.global.delete('bullet'); | ||
if (ngMocksUniverse.global.has('bullet:reset')) { | ||
TestBed.resetTestingModule(); | ||
} | ||
}); | ||
}; |
Oops, something went wrong.