84 lines
3.2 KiB
TypeScript
84 lines
3.2 KiB
TypeScript
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
|
|
import { ContactListTable } from './contact-list-table';
|
|
import { strings } from '../../strings';
|
|
import { ContactService } from '../../services/contact.service';
|
|
import { ContactDTO } from '../../models/ContactDTO';
|
|
import { By } from '@angular/platform-browser';
|
|
import { of } from 'rxjs';
|
|
import { Router } from '@angular/router';
|
|
import { LanguageManager } from '../../services/language-manager';
|
|
|
|
describe('ContactListTable', () => {
|
|
let component: ContactListTable;
|
|
let fixture: ComponentFixture<ContactListTable>;
|
|
|
|
let contactService: jasmine.SpyObj<ContactService>;
|
|
let languageManager: jasmine.SpyObj<LanguageManager>;
|
|
let router: jasmine.SpyObj<Router>;
|
|
let CONTACT_LIST_MOCK: ContactDTO[];
|
|
|
|
beforeEach(async () => {
|
|
contactService = jasmine.createSpyObj(ContactService.name, ['delete']);
|
|
languageManager = jasmine.createSpyObj(LanguageManager.name, [], { strings: strings.en });
|
|
router = jasmine.createSpyObj(Router.name, ['navigate']);
|
|
CONTACT_LIST_MOCK = [new ContactDTO(1, 'MOCK', 'MOCK', '5491122222222')];
|
|
await TestBed.configureTestingModule({
|
|
imports: [ContactListTable],
|
|
providers: [
|
|
{ provide: ContactService, useValue: contactService },
|
|
{ provide: LanguageManager, useValue: languageManager },
|
|
{ provide: Router, useValue: router },
|
|
],
|
|
}).compileComponents();
|
|
contactService.delete.and.returnValue(of([]));
|
|
fixture = TestBed.createComponent(ContactListTable);
|
|
component = fixture.componentInstance;
|
|
fixture.componentRef.setInput('contactList', CONTACT_LIST_MOCK);
|
|
fixture.detectChanges();
|
|
});
|
|
|
|
it('should create', () => {
|
|
expect(component).toBeTruthy();
|
|
});
|
|
|
|
describe('delete', () => {
|
|
it('should call delete for valid ID', () => {
|
|
const deleteSpy = spyOn(component, 'delete').and.callThrough();
|
|
const deleteButton = fixture.debugElement
|
|
.query(By.css('app-contact-actions-bar'))
|
|
.query(By.css('.btn--delete'));
|
|
deleteButton.triggerEventHandler('click');
|
|
expect(deleteSpy).toHaveBeenCalledWith(CONTACT_LIST_MOCK[0].id);
|
|
});
|
|
|
|
it("shouldn't call delete method if ID is falsy", () => {
|
|
CONTACT_LIST_MOCK[0].id = undefined;
|
|
const deleteButton = fixture.debugElement
|
|
.query(By.css('app-contact-actions-bar'))
|
|
.query(By.css('.btn--delete'));
|
|
deleteButton.triggerEventHandler('click');
|
|
expect(contactService.delete).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('edit', () => {
|
|
it("shouldn't navigate if ID is falsy", () => {
|
|
CONTACT_LIST_MOCK[0].id = undefined;
|
|
const editButton = fixture.debugElement
|
|
.query(By.css('app-contact-actions-bar'))
|
|
.query(By.css('.btn--edit'));
|
|
editButton.triggerEventHandler('click');
|
|
expect(router.navigate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should call navigate for valid ID', () => {
|
|
const editButton = fixture.debugElement
|
|
.query(By.css('app-contact-actions-bar'))
|
|
.query(By.css('.btn--edit'));
|
|
editButton.triggerEventHandler('click');
|
|
expect(router.navigate).toHaveBeenCalledOnceWith(['edit', CONTACT_LIST_MOCK[0].id]);
|
|
});
|
|
});
|
|
});
|