refactor: handle save contact, add contact list, update template

This commit is contained in:
2025-12-14 21:07:15 -03:00
parent 7e349909b9
commit d25142c648
4 changed files with 62 additions and 10 deletions

View File

@@ -3,15 +3,27 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Main } from './main';
import { STRINGS_INJECTOR } from '../../app.config';
import { strings } from '../../strings';
import { ContactService } from './contact.service';
import { ContactList } from '../../components/contact-list/contact-list';
import { of } from 'rxjs';
import { ContactDTO } from '../../models/ContactDTO';
import { By } from '@angular/platform-browser';
import { ContactForm } from '../../components/contact-form/contact-form';
describe('Main', () => {
let component: Main;
let fixture: ComponentFixture<Main>;
let contactService: jasmine.SpyObj<ContactService>;
beforeEach(async () => {
contactService = jasmine.createSpyObj(ContactList.name, ['getAll', 'save']);
contactService.getAll.and.returnValue(of([]));
await TestBed.configureTestingModule({
imports: [Main],
providers: [{ provide: STRINGS_INJECTOR, useValue: strings }],
providers: [
{ provide: STRINGS_INJECTOR, useValue: strings },
{ provide: ContactService, useValue: contactService }
],
}).compileComponents();
fixture = TestBed.createComponent(Main);
component = fixture.componentInstance;
@@ -21,4 +33,13 @@ describe('Main', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
it('should call save on contact event', () => {
contactService.save.and.returnValue(of([]));
const saveSpy = spyOn(component, 'save').and.callThrough();
const CONTACT_MOCK = new ContactDTO()
const contactForm: ContactForm = fixture.debugElement.query(By.css('app-contact-form')).componentInstance;
contactForm.contact.emit(CONTACT_MOCK);
expect(saveSpy).toHaveBeenCalledOnceWith(CONTACT_MOCK);
});
});