-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
292 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,85 @@ | ||
// import { ComponentFixture, TestBed } from '@angular/core/testing' | ||
import { ComponentFixture, TestBed } from '@angular/core/testing' | ||
|
||
// import { NuevaTareaComponent } from './nueva-tarea.component' | ||
import { NuevaTareaComponent } from './nueva-tarea.component' | ||
import { Router } from '@angular/router' | ||
import { HttpClient } from '@angular/common/http' | ||
import { getHttpClientSpy } from 'services/httpClientSpy' | ||
import { Usuario } from 'domain/usuario' | ||
|
||
// describe('NuevaTareaComponent', () => { | ||
// let component: NuevaTareaComponent | ||
// let fixture: ComponentFixture<NuevaTareaComponent> | ||
describe('NuevaTareaComponent', () => { | ||
let component: NuevaTareaComponent | ||
let fixture: ComponentFixture<NuevaTareaComponent> | ||
let routerSpy: jasmine.SpyObj<Router> | ||
let httpClientSpy: jasmine.SpyObj<HttpClient> | ||
|
||
// beforeEach(async () => { | ||
// await TestBed.configureTestingModule({ | ||
// imports: [NuevaTareaComponent] | ||
// }) | ||
// .compileComponents() | ||
beforeEach(async () => { | ||
routerSpy = jasmine.createSpyObj('Router', ['navigate', 'navigateByUrl']) | ||
httpClientSpy = getHttpClientSpy() | ||
|
||
// fixture = TestBed.createComponent(NuevaTareaComponent) | ||
// component = fixture.componentInstance | ||
// fixture.detectChanges() | ||
// }) | ||
await TestBed.configureTestingModule({ | ||
imports: [NuevaTareaComponent], | ||
providers: [ | ||
{ provide: HttpClient, useValue: httpClientSpy }, | ||
{ provide: Router, useValue: routerSpy } | ||
] | ||
}).compileComponents() | ||
|
||
// it('should create', () => { | ||
// expect(component).toBeTruthy() | ||
// }) | ||
// }) | ||
fixture = TestBed.createComponent(NuevaTareaComponent) | ||
component = fixture.componentInstance | ||
fixture.detectChanges() | ||
}) | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy() | ||
}) | ||
|
||
it('should create a new task', async () => { | ||
// Arrange | ||
await sendInput('descripcion', 'Aprender Angular') | ||
await sendInput('iteracion', 'Iteracion 1') | ||
// TODO: lograr que funcione para input type date y drop down | ||
component.tarea.fecha = new Date() | ||
component.asignatario = new Usuario('Nahuel Palumbo') | ||
await sendInput('porcentaje-cumplimiento', '20') | ||
|
||
// Act | ||
getByTestId('guardar').click() | ||
fixture.detectChanges() | ||
await fixture.whenStable() | ||
|
||
// Assert | ||
const route = routerSpy.navigateByUrl.calls.first().args[0] | ||
expect(route).toBe('/') | ||
}) | ||
|
||
it('an invalid task cannot be created', async () => { | ||
await sendInput('porcentaje-cumplimiento', '101') | ||
getByTestId('guardar').click() | ||
fixture.detectChanges() | ||
await fixture.whenStable() | ||
expect(routerSpy.navigateByUrl).toHaveBeenCalledTimes(0) | ||
expect(component.tarea.hasErrors).toBeTruthy() | ||
expect(component.tarea.errors.length).toBe(4) | ||
validateErrorField('descripcion') | ||
validateErrorField('iteracion') | ||
validateErrorField('fecha') | ||
validateErrorField('porcentajeCumplimiento') | ||
}) | ||
|
||
function getByTestId(testId: string) { | ||
const resultHtml = fixture.debugElement.nativeElement | ||
return resultHtml.querySelector(`[data-testid="${testId}"`) | ||
} | ||
|
||
async function sendInput(testId: string, text: string | Date) { | ||
const inputElement = getByTestId(testId) | ||
inputElement.value = text | ||
inputElement.dispatchEvent(new Event('input')) | ||
fixture.detectChanges() | ||
return fixture.whenStable() | ||
} | ||
|
||
function validateErrorField(field: string) { | ||
expect(getByTestId(`error-message-${field}`).innerHTML).toBeTruthy() | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,146 @@ | ||
// import { ComponentFixture, TestBed } from '@angular/core/testing' | ||
import { | ||
ComponentFixture, | ||
TestBed, | ||
fakeAsync, | ||
flush, | ||
tick | ||
} from '@angular/core/testing' | ||
|
||
// import { TareasComponent } from './tareas.component' | ||
import { TareasComponent } from './tareas.component' | ||
import { getHttpClientSpy } from 'services/httpClientSpy' | ||
import { HttpClient } from '@angular/common/http' | ||
import { Router } from '@angular/router' | ||
import { registerLocaleData } from '@angular/common' | ||
import localeEs from '@angular/common/locales/es' | ||
import { throwError } from 'rxjs' | ||
|
||
// describe('TareasComponent', () => { | ||
// let component: TareasComponent | ||
// let fixture: ComponentFixture<TareasComponent> | ||
// | ||
/** Registramos el locale ES para formatear números */ | ||
// Font Awesome para los íconos | ||
// | ||
// routing | ||
// componentes propios | ||
|
||
// beforeEach(async () => { | ||
// await TestBed.configureTestingModule({ | ||
// imports: [TareasComponent] | ||
// }) | ||
// .compileComponents() | ||
registerLocaleData(localeEs) | ||
|
||
// fixture = TestBed.createComponent(TareasComponent) | ||
// component = fixture.componentInstance | ||
// fixture.detectChanges() | ||
// }) | ||
describe('TareasComponent', () => { | ||
let component: TareasComponent | ||
let fixture: ComponentFixture<TareasComponent> | ||
let routerSpy: jasmine.SpyObj<Router> | ||
let httpClientSpy: jasmine.SpyObj<HttpClient> | ||
|
||
// it('should create', () => { | ||
// expect(component).toBeTruthy() | ||
// }) | ||
// }) | ||
beforeEach(async () => { | ||
routerSpy = jasmine.createSpyObj('Router', ['navigate', 'navigateByUrl']) | ||
httpClientSpy = getHttpClientSpy() | ||
|
||
await TestBed.configureTestingModule({ | ||
imports: [TareasComponent], | ||
providers: [ | ||
{ provide: HttpClient, useValue: httpClientSpy }, | ||
{ provide: Router, useValue: routerSpy } | ||
] | ||
}).compileComponents() | ||
|
||
fixture = TestBed.createComponent(TareasComponent) | ||
component = fixture.componentInstance | ||
fixture.detectChanges() | ||
|
||
await fixture.whenStable() | ||
fixture.detectChanges() | ||
}) | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy() | ||
}) | ||
|
||
it('should initially show 2 pending tasks', fakeAsync(() => { | ||
expect(2).toBe(component.tareas.length) | ||
})) | ||
|
||
it('first task can be marked as done', () => { | ||
expect(getByTestId('cumplir_1')).toBeTruthy() | ||
}) | ||
|
||
it('when a task is done, it has 100% of completion', () => { | ||
getByTestId('cumplir_1').click() | ||
fixture.detectChanges() | ||
// Chequeamos que el objeto de dominio tarea responde correctamente, | ||
// pero también el binding entre componente y vista | ||
expect(getByTestId('porcentaje_1').textContent).toBe('100,00') | ||
// https://daveceddia.com/jasmine-2-spy-cheat-sheet/ | ||
// Chequeamos que se haya enviado la información correctamente al backend | ||
const tareaActualizada = httpClientSpy.put.calls.mostRecent().args[1] | ||
expect(tareaActualizada.porcentajeCumplimiento).toBe(100) | ||
}) | ||
|
||
it('unassign first task', async () => { | ||
getByTestId('desasignar_1').click() | ||
fixture.detectChanges() | ||
expect(getByTestId('asignatario_1').textContent).toBe('') | ||
}) | ||
|
||
it('searching for second task should have one tr in tasks list', () => { | ||
component.tareaBuscada = 'e2e' | ||
fixture.detectChanges() | ||
const resultHtml = fixture.debugElement.nativeElement | ||
expect( | ||
resultHtml.querySelectorAll('[data-testid="fila-tarea"]').length | ||
).toBe(1) | ||
}) | ||
|
||
it('unassign - should catch error gracefully', fakeAsync(() => { | ||
httpClientSpy.put.and.returnValue(throwError(() => new Error('Fake error'))) | ||
|
||
getByTestId('desasignar_1').click() | ||
tick(1000) | ||
fixture.detectChanges() | ||
|
||
expect(getByTestId('error-message')?.innerHTML).toBeTruthy() | ||
// Importante para que no falle con "Error: 1 timer(s) still in the queue" | ||
// tick(2000) | ||
// o mejor... | ||
flush() | ||
|
||
httpClientSpy.put.calls.reset() | ||
})) | ||
|
||
it('finish - should catch error gracefully', fakeAsync(() => { | ||
httpClientSpy.put.and.returnValue(throwError(() => new Error('Fake error'))) | ||
|
||
getByTestId('cumplir_1').click() | ||
tick(1000) | ||
fixture.detectChanges() | ||
expect(getByTestId('error-message')?.innerHTML).toBeTruthy() | ||
flush() | ||
|
||
httpClientSpy.put.calls.reset() | ||
})) | ||
|
||
it('create new task should navigate', async () => { | ||
getByTestId('nueva-tarea').click() | ||
|
||
fixture.detectChanges() | ||
|
||
const route = routerSpy.navigateByUrl.calls.first().args[0] | ||
expect(route).toBe('/nuevaTarea') | ||
}) | ||
|
||
it('assign task should navigate', () => { | ||
getByTestId('asignar_2').click() | ||
|
||
fixture.detectChanges() | ||
|
||
// const route = routerSpy.navigate.calls.first().args[0] | ||
// expect(route).toEqual(['/asignarTarea', 2]) | ||
|
||
// Con destructuring | ||
const [url, tareaId] = routerSpy.navigate.calls.first().args[0] | ||
expect(url).toBe('/asignarTarea') | ||
expect(tareaId).toBe(2) | ||
}) | ||
|
||
function getByTestId(testId: string) { | ||
const resultHtml = fixture.debugElement.nativeElement | ||
return resultHtml.querySelector(`[data-testid="${testId}"]`) | ||
} | ||
}) |
Oops, something went wrong.