Skip to content

Instantly share code, notes, and snippets.

@karan-kang
Created August 4, 2017 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karan-kang/7e36e846facc983c45229a1e18d7ae9c to your computer and use it in GitHub Desktop.
Save karan-kang/7e36e846facc983c45229a1e18d7ae9c to your computer and use it in GitHub Desktop.
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import {
AppPanelService,
ExpandPanelComponent
} from './index';
describe('shared.ExpandPanelComponent', () => {
let fixture: ComponentFixture<ExpandPanelComponent>;
let component: ExpandPanelComponent;
let service: any;
let expanded = false;
beforeEach(async(() => {
// Mocked service
service = jasmine.createSpyObj('AppPanelServiceMock', [
'expandPanel', 'getPanelElement'
]);
service.expandPanel.and.callFake(() => {
return expanded;
});
service.getPanelElement.and.callFake(() => {
return {
panel: 'element'
};
});
TestBed.configureTestingModule({
declarations: [
ExpandPanelComponent
],
providers: [
{
provide: AppPanelService,
useValue: service
}
]
});
TestBed.compileComponents().then(() => {
fixture = TestBed.createComponent(ExpandPanelComponent);
component = fixture.componentInstance;
});
}));
it('should get instance', () => {
expect(component).toBeDefined();
});
it('should throw exception if no panel element', fakeAsync(() => {
service.getPanelElement.and.callFake(() => {
return null;
});
expect(component.ngOnInit()).toThrow();
}));
it('should call expand method', fakeAsync(() => {
expanded = true;
const anchor = fixture.debugElement.query(By.css('a'));
anchor.triggerEventHandler('click', null);
tick();
fixture.detectChanges();
expect(service.expandPanel).toHaveBeenCalled();
expect(component.expanded).toBe(expanded);
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment