-
-
Notifications
You must be signed in to change notification settings - Fork 87
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 #3691 from satanTime/issues/3391
docs(MockInstance): works with legacy tools too #3391
- Loading branch information
Showing
3 changed files
with
176 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { | ||
AfterViewInit, | ||
Component, | ||
Injector, | ||
ViewChild, | ||
} from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { Observable, Subject } from 'rxjs'; | ||
|
||
import { MockComponent, MockInstance } from 'ng-mocks'; | ||
|
||
// A copy of EMPTY, which does not exist in A5. | ||
const EMPTY = new Subject<any>(); | ||
EMPTY.complete(); | ||
|
||
// A child component that contains update$ the parent component wants to listen to. | ||
@Component({ | ||
selector: 'target', | ||
template: '{{ update$ | async }}', | ||
}) | ||
class ChildComponent { | ||
public readonly update$: Observable<void> = EMPTY; | ||
|
||
public constructor(public readonly injector: Injector) {} | ||
} | ||
|
||
// A parent component that uses @ViewChild to listen to update$ of its child component. | ||
@Component({ | ||
selector: 'real', | ||
template: '<target></target>', | ||
}) | ||
class RealComponent implements AfterViewInit { | ||
@ViewChild(ChildComponent, {} as any) | ||
protected child?: ChildComponent; | ||
|
||
public ngAfterViewInit() { | ||
if (this.child) { | ||
this.child.update$.subscribe(); | ||
} | ||
} | ||
} | ||
|
||
describe('MockInstance:component', () => { | ||
// Creates a scope to reset customizations automatically after this test. | ||
MockInstance.scope(); | ||
|
||
// Configuring TestBed with a mock for ChildComponent. | ||
beforeEach(() => { | ||
return TestBed.configureTestingModule({ | ||
imports: [CommonModule], | ||
declarations: [RealComponent, MockComponent(ChildComponent)], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
// Because ChildComponent is replaced with its mock object, | ||
// its update$ is undefined and ngAfterViewInit of the parent | ||
// component will fail on .subscribe(). | ||
// Let's fix it via defining customization for the mock object. | ||
MockInstance(ChildComponent, () => ({ | ||
// comment the next line to check the failure. | ||
update$: EMPTY, | ||
})); | ||
}); | ||
|
||
it('should render', () => { | ||
// Without the custom initialization rendering would fail here | ||
// with "Cannot read property 'subscribe' of undefined". | ||
expect(() => | ||
TestBed.createComponent(RealComponent).detectChanges(), | ||
).not.toThrow(); | ||
}); | ||
}); |
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