Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add new test for project managment section
  • Loading branch information
macrisguncay committed Mar 23, 2020
commit 2e07a113814e77e0a1de2347da37188692c571c2
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { async, ComponentFixture, TestBed, inject } from '@angular/core/testing';

import { ProjectManagementComponent } from './project-management.component';
import { Project } from '../../../interfaces/project';
import { ProjectService } from '../../../services/project.service';
import { of } from 'rxjs';
import { CreateProjectComponent } from '../../../components/shared/create-project/create-project.component';
import { ProjectListComponent } from '../../../components/shared/project-list/project-list.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

describe('ProjectManagementComponent', () => {
let component: ProjectManagementComponent;
Expand Down Expand Up @@ -42,8 +44,12 @@ describe('ProjectManagementComponent', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ProjectManagementComponent ],
providers: [ { provide: ProjectService, useValue: projectServiceStub }]
declarations: [ ProjectManagementComponent, CreateProjectComponent, ProjectListComponent ],
providers: [ { provide: ProjectService, useValue: projectServiceStub }],
imports: [
FormsModule,
ReactiveFormsModule
]
})
.compileComponents();
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export class ProjectManagementComponent implements OnInit {
};
this.projects = this.projects.concat(newProject);
}
console.log(this.projects);
}

editProject(projectId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { CreateProjectComponent } from './create-project.component';
import { Validators, FormBuilder } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

describe('CreateProjectComponent', () => {
let component: CreateProjectComponent;
Expand All @@ -10,9 +11,10 @@ describe('CreateProjectComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CreateProjectComponent ],
providers: [
FormBuilder
],
imports: [
FormsModule,
ReactiveFormsModule
]
})
.compileComponents();
}));
Expand All @@ -26,4 +28,74 @@ describe('CreateProjectComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('should send the form data on onSubmit buton #onSubmit', () => {
const project = {
name: 'app 4',
details: 'It is a good app',
status: 'inactive',
completed: true
};

spyOn(component.savedProject, 'emit');
component.onSubmit(project);
expect(component.savedProject.emit).toHaveBeenCalled();
});

it('should clean the form and send a cancelForm event #reset', () => {
const project = {
name: 'app 4',
details: 'It is a good app',
status: 'inactive',
completed: true
};

component.projectForm.setValue(project);
spyOn(component.cancelForm, 'emit');
component.reset();
expect(component.projectForm.value.name).toEqual(null);
expect(component.projectForm.value.details).toEqual(null);
expect(component.projectForm.value.status).toEqual(null);
expect(component.projectForm.value.completed).toEqual(null);

expect(component.cancelForm.emit).toHaveBeenCalled();
});

it('form invalid when empty', () => {
expect(component.projectForm.valid).toBeFalsy();
});

it('name field validity', () => {
const name = component.projectForm.controls.name;
expect(name.valid).toBeFalsy();

name.setValue('');
expect(name.hasError('required')).toBeTruthy();

name.setValue('A');
expect(name.hasError('required')).toBeFalsy();
});

it('details field validity', () => {
const details = component.projectForm.controls.details;
expect(details.valid).toBeFalsy();

details.setValue('');
expect(details.hasError('required')).toBeTruthy();

details.setValue('A');
expect(details.hasError('required')).toBeFalsy();
});

it('status field validity', () => {
const status = component.projectForm.controls.status;
expect(status.valid).toBeFalsy();

status.setValue('');
expect(status.hasError('required')).toBeTruthy();

status.setValue('A');
expect(status.hasError('required')).toBeFalsy();
});

});
18 changes: 18 additions & 0 deletions src/app/components/shared/modal/modal.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,22 @@ describe('ModalComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('should emit removeProject event #removedProject', () => {
const project = {
id: 1,
name: 'app 4',
details: 'It is a good app',
status: 'inactive',
completed: true
};

spyOn(component.removeProject, 'emit');
component.project = project;
fixture.detectChanges();
component.removedProject(project.id);
expect(component.removeProject.emit).toHaveBeenCalled();
component.cancelDeleteModal.nativeElement.click();
});
});

Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,37 @@ describe('ProjectListComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('should emit deleteProject event #removeProject', () => {
const project = {
id: 1,
name: 'app 4',
details: 'It is a good app',
status: 'inactive',
completed: true
};

component.projectToDelete = project;
spyOn(component.deleteProject, 'emit');
component.removeProject(project.id);
expect(component.deleteProject.emit).toHaveBeenCalled();
expect(component.projectToDelete).toBe(null);
});

it('should open delete modal #openModal', () => {
const project = {
id: 1,
name: 'app 4',
details: 'It is a good app',
status: 'inactive',
completed: true
};

component.openModal(project);
expect(component.projectToDelete.id).toBe(1);
expect(component.projectToDelete.name).toBe('app 4');
expect(component.projectToDelete.details).toBe('It is a good app');
expect(component.projectToDelete.status).toBe('inactive');
expect(component.projectToDelete.completed).toBe(true);
});
});
42 changes: 40 additions & 2 deletions src/app/services/project.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
import { TestBed, inject, async } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';

import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { Project } from '../interfaces/project';
import { ProjectService } from './project.service';

describe('ProjectService', () => {
let service: ProjectService;
let httpMock: HttpTestingController;

const projects: Project[] = [{
id: 1,
name: 'app 1',
details: 'It is a good app',
status: 'inactive',
completed: true
},
{
id: 2,
name: 'app 2',
details: 'It is a good app',
status: 'inactive',
completed: false
},
{
id: 3,
name: 'app 3',
details: 'It is a good app',
status: 'active',
completed: true
}
];

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule]
});
service = TestBed.inject(ProjectService);
httpMock = TestBed.inject(HttpTestingController);
});

afterEach (() => {
httpMock.verify ();
});

it('should create', inject([HttpClientTestingModule, ProjectService],
(httpClient: HttpClientTestingModule, apiService: ProjectService) => {
expect(apiService).toBeTruthy();
expect(httpClient).toBeTruthy();
}));

it('should get projects API/GET #getProjects', () => {
service.getProjects().subscribe(projects => {
expect(projects.length).toBe(3);
expect(projects).toEqual(projects);
});
const request = httpMock.expectOne('assets/project.json');
expect(request.request.method).toBe('GET');
request.flush(projects);
});

});