refactor: handle save contact, add contact list, update template
This commit is contained in:
@@ -1,4 +1,14 @@
|
|||||||
<app-card
|
<app-main-header [title]="strings.contactList"/>
|
||||||
bgColor="var(--secondary)"
|
<app-card bgColor="var(--secondary)">
|
||||||
><app-contact-form class="form"></app-contact-form>
|
<app-contact-form class="form" (contact)="save($event)">
|
||||||
|
<app-form-header
|
||||||
|
[subtitle]="strings.allFieldRequired|upperfirst"
|
||||||
|
[title]="strings.addContact|upperfirst"
|
||||||
|
slot="header"
|
||||||
|
/>
|
||||||
|
</app-contact-form>
|
||||||
</app-card>
|
</app-card>
|
||||||
|
<app-card bgColor="#fff">
|
||||||
|
<app-contact-list/>
|
||||||
|
</app-card>
|
||||||
|
<footer></footer>
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
app-card {
|
app-card {
|
||||||
|
&:first-of-type {
|
||||||
margin-top: 2rem;
|
margin-top: 2rem;
|
||||||
|
}
|
||||||
|
margin-top: 3rem;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -11,3 +14,7 @@ app-contact-form {
|
|||||||
display: inline-block;
|
display: inline-block;
|
||||||
padding: 3rem;
|
padding: 3rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
footer {
|
||||||
|
margin-top: 60px;
|
||||||
|
}
|
||||||
@@ -3,15 +3,27 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|||||||
import { Main } from './main';
|
import { Main } from './main';
|
||||||
import { STRINGS_INJECTOR } from '../../app.config';
|
import { STRINGS_INJECTOR } from '../../app.config';
|
||||||
import { strings } from '../../strings';
|
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', () => {
|
describe('Main', () => {
|
||||||
let component: Main;
|
let component: Main;
|
||||||
let fixture: ComponentFixture<Main>;
|
let fixture: ComponentFixture<Main>;
|
||||||
|
let contactService: jasmine.SpyObj<ContactService>;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
contactService = jasmine.createSpyObj(ContactList.name, ['getAll', 'save']);
|
||||||
|
contactService.getAll.and.returnValue(of([]));
|
||||||
await TestBed.configureTestingModule({
|
await TestBed.configureTestingModule({
|
||||||
imports: [Main],
|
imports: [Main],
|
||||||
providers: [{ provide: STRINGS_INJECTOR, useValue: strings }],
|
providers: [
|
||||||
|
{ provide: STRINGS_INJECTOR, useValue: strings },
|
||||||
|
{ provide: ContactService, useValue: contactService }
|
||||||
|
],
|
||||||
}).compileComponents();
|
}).compileComponents();
|
||||||
fixture = TestBed.createComponent(Main);
|
fixture = TestBed.createComponent(Main);
|
||||||
component = fixture.componentInstance;
|
component = fixture.componentInstance;
|
||||||
@@ -21,4 +33,13 @@ describe('Main', () => {
|
|||||||
it('should create', () => {
|
it('should create', () => {
|
||||||
expect(component).toBeTruthy();
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,11 +1,25 @@
|
|||||||
import { Component } from '@angular/core';
|
import { Component, inject } from '@angular/core';
|
||||||
import { Card } from '../../components/card/card';
|
import { Card } from '../../components/card/card';
|
||||||
import { ContactForm } from '../../components/contact-form/contact-form';
|
import { ContactForm } from '../../components/contact-form/contact-form';
|
||||||
|
import { ContactList } from '../../components/contact-list/contact-list';
|
||||||
|
import { ContactService } from './contact.service';
|
||||||
|
import { ContactDTO } from '../../models/ContactDTO';
|
||||||
|
import { FormHeader } from '../../components/form-header/form-header';
|
||||||
|
import { STRINGS_INJECTOR } from '../../app.config';
|
||||||
|
import { UpperfirstPipe } from '../../pipes/upperfirst-pipe';
|
||||||
|
import { MainHeader } from '../../components/main-header/main-header';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-main',
|
selector: 'app-main',
|
||||||
imports: [Card, ContactForm],
|
imports: [Card, ContactForm, ContactList, FormHeader, MainHeader, UpperfirstPipe],
|
||||||
templateUrl: './main.html',
|
templateUrl: './main.html',
|
||||||
styleUrl: './main.scss',
|
styleUrl: './main.scss',
|
||||||
})
|
})
|
||||||
export class Main {}
|
export class Main {
|
||||||
|
private readonly contactService = inject(ContactService);
|
||||||
|
protected readonly strings = inject(STRINGS_INJECTOR);
|
||||||
|
|
||||||
|
save(contactDTO: ContactDTO) {
|
||||||
|
this.contactService.save(contactDTO).subscribe();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user