import { async, ComponentFixtureAutoDetect, TestBed } from '@angular/core/testing';
describe('NguiVirtualListComponent', () => {
...
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
{ provide: ComponentFixtureAutoDetect, useValue: true },
...
Last active
May 4, 2020 19:42
-
-
Save allenhwkim/a281649182702cc20d2b8b1c56d1d06e to your computer and use it in GitHub Desktop.
Angular5+ Unit Tests
Error case:
- No provider for ElementRef!
- No provider for XXXXService!
constructor(
public renderer: Renderer2,
public element: ElementRef, // <-- mock required
public dynamicComponentService: DynamicComponentService, // <-- mock required
public cdr: ChangeDetectorRef,
@Inject(PLATFORM_ID) private platformId: any
) {}
import { ChangeDetectorRef, ElementRef, Injectable, Renderer2 } from '@angular/core';
import { DynamicComponentService } from '../../utils';
class MockElementRef extends ElementRef {
constructor() { super(undefined); }
// nativeElement = {};
}
class MockDynamicComponentService extends DynamicComponentService {
}
describe('NguiVirtualListComponent', () => {
let fixture;
let component;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
NguiVirtualListComponent,
MockComponent('ngui-inview')
],
providers: [
Renderer2,
ChangeDetectorRef,
{ provide: ComponentFixtureAutoDetect, useValue: true },
{ provide: ElementRef, useClass: MockElementRef },
{ provide: DynamicComponentService, useClass: MockDynamicComponentService },
{ provide: PLATFORM_ID, useValue: 'browser'}
]
// schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));
it('should create a component', async(() => {
fixture = TestBed.createComponent(NguiVirtualListComponent);
component = fixture.debugElement.componentInstance;
expect(component).toBeTruthy();
}));
...
component.myFunc = jest.fn().mockReturnValue(true)
component.ngOnInit();
expect(component.myFunc).toHaveBeenCalled();
Object.defineProperty(myObj, 'prop', {
get: jest.fn(() => 'bar'),
set: jest.fn()
});
Ref. https://facebook.github.io/jest/docs/en/tutorial-jquery.html
document.body.innerHTML = '<input id="my-input" />;
Error Case
- ReferenceError: IntersectionObserver is not defined
ngOnInit(): void {
if (isPlatformBrowser(this.platformId)) {
this.observer = new IntersectionObserver(this.handleIntersect.bind(this), this.options);
this.observer.observe(this.element.nativeElement);
}
}
(<any>window).IntersectionObserver = jest.fn();
IntersectionObserver.prototype.observe = jest.fn();
describe('MyComponent', () => {
it('should run #ngOnInit', async(() => {
component.ngOnInit();
expect(component.observer).toBeTruthy();
}));
});
foo(): void {
console.error('<ngui-virtual-list> requires [template] and {bottomInview)');
}
it('should test console.error', async(() => {
const spyError = jest.spyOn(console, 'error');
component.foo();
expect(spyError).toHaveBeenCalled();
}));
function foo() {
throw 'error';
}
expect(() => {
foo();
}).toThrow();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment