Newer
Older
// Load the implementations that should be tested
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, fakeAsync, inject, TestBed, tick, } from '@angular/core/testing';
import 'rxjs/add/observable/of';
import { Chips } from './models/chips.model';
import { UploaderService } from '../uploader/uploader.service';
import { ChipsComponent } from './chips.component';
import { NgbModule, NgbTooltip } from '@ng-bootstrap/ng-bootstrap';
import { By } from '@angular/platform-browser';
import { PaginationComponent } from '../pagination/pagination.component';
import { TruncatableComponent } from '../truncatable/truncatable.component';
function createTestComponent<T>(html: string, type: { new(...args: any[]): T }): ComponentFixture<T> {
TestBed.overrideComponent(type, {
set: {template: html}
});
const fixture = TestBed.createComponent(type);
fixture.detectChanges();
return fixture as ComponentFixture<T>;
}
// async beforeEach
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NgbModule.forRoot(),
SortablejsModule.forRoot({ animation: 150 }),
],
declarations: [
ChipsComponent,
TestComponent,
], // declare the test component
providers: [
ChangeDetectorRef,
ChipsComponent,
UploaderService
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
});
}));
// synchronous beforeEach
beforeEach(() => {
html = `
<ds-chips
*ngIf="chips.hasItems()"
[chips]="chips"
(selected)="onChipSelected($event)"></ds-chips>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
it('should create Chips Component', inject([ChipsComponent], (app: ChipsComponent) => {
expect(app).toBeDefined();
}));
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
beforeEach(() => {
chips = new Chips(['a', 'b', 'c']);
chipsFixture = TestBed.createComponent(ChipsComponent);
chipsComp = chipsFixture.componentInstance; // TruncatableComponent test instance
chipsComp.editable = true;
chipsComp.chips = chips;
chipsFixture.detectChanges();
});
afterEach(() => {
chipsFixture.destroy();
chipsComp = null;
});
it('should set edit mode when a chip item is selected', fakeAsync(() => {
spyOn(chipsComp.selected, 'emit');
chipsComp.chipsSelected(new Event('click'), 1);
chipsFixture.detectChanges();
tick();
const item = chipsComp.chips.getChipByIndex(1);
expect(item.editMode).toBe(true);
expect(chipsComp.selected.emit).toHaveBeenCalledWith(1);
}));
it('should not set edit mode when a chip item is selected and editable is false', fakeAsync(() => {
chipsComp.editable = false;
spyOn(chipsComp.selected, 'emit');
chipsComp.chipsSelected(new Event('click'), 1);
chipsFixture.detectChanges();
tick();
const item = chipsComp.chips.getChipByIndex(1);
expect(item.editMode).toBe(false);
expect(chipsComp.selected.emit).not.toHaveBeenCalledWith(1);
}));
it('should emit when a chip item is removed and editable is true', fakeAsync(() => {
spyOn(chipsComp.chips, 'remove');
const item = chipsComp.chips.getChipByIndex(1);
chipsComp.removeChips(new Event('click'), 1);
chipsFixture.detectChanges();
tick();
expect(chipsComp.chips.remove).toHaveBeenCalledWith(item);
}));
it('should save chips item index when drag and drop start', fakeAsync(() => {
const de = chipsFixture.debugElement.query(By.css('li.nav-item'));
de.triggerEventHandler('dragstart', null);
expect(chipsComp.dragged).toBe(0)
}));
it('should update chips item order when drag and drop end', fakeAsync(() => {
spyOn(chipsComp.chips, 'updateOrder');
const de = chipsFixture.debugElement.query(By.css('li.nav-item'));
de.triggerEventHandler('dragend', null);
expect(chipsComp.dragged).toBe(-1)
expect(chipsComp.chips.updateOrder).toHaveBeenCalled();
}));
it('should show item tooltip on mouse over', fakeAsync(() => {
const de = chipsFixture.debugElement.query(By.css('li.nav-item'));
de.triggerEventHandler('mouseover', null);
expect(chipsComp.tipText).toBe('a')
}));
});
// declare a test component
@Component({
selector: 'ds-test-cmp',
template: ``
})
class TestComponent {
public chips = new Chips(['a', 'b', 'c']);
public editable = true;