Last active
March 28, 2022 11:45
-
-
Save ConxiCR/d2715c7ed990778c902e65df912581b3 to your computer and use it in GitHub Desktop.
testing with Jasmine and Karma
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
import { CartComponent } from './cart.component'; | |
import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
import { HttpClientTestingModule } from '@angular/common/http/testing' | |
import { BookService } from '../../services/book.service'; | |
import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core'; | |
describe('cart.component', () => { | |
let component: CartComponent;//declaramos el componente de tipo que queremos testear y lo importamos | |
let fixture: ComponentFixture<CartComponent>;//declaramos una variable para poder extraer el servicio que requiere el tipo que va a tener. | |
beforeEach(() => { | |
TestBed.configureTestingModule({ | |
imports:[ | |
HttpClientTestingModule | |
], | |
declarations:[ | |
CartComponent | |
], | |
providers:[ | |
BookService | |
], | |
schemas:[CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA] | |
}).compileComponents(); | |
}); | |
beforeEach(() => {//to instantiate the component | |
fixture = TestBed.createComponent(CartComponent); | |
component = fixture.componentInstance; | |
fixture.detectChanges();//el component entrara por el método onInit | |
}); | |
it('should create', () => { | |
//esperamos a que ocurra algo con expect. Que el componet este instanciado correctamente | |
expect(component).toBeTruthy(); | |
//expect(component).toBeFalse(); | |
}); | |
//provamos a lanzar el test | |
}); |
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
//Method to test comes from cart.component.ts | |
/*public getTotalPrice(listCartBook: Book[]): number { | |
let totalPrice = 0; | |
listCartBook.forEach((book: Book) => { | |
totalPrice += book.amount * book.price; | |
}); | |
return totalPrice; | |
}*/ | |
//to test this method we create a const of the array | |
const listBook: Book[] = [ | |
{ | |
name: '', | |
author: '', | |
isbn: '', | |
price: 15, | |
amount: 2 | |
}, | |
{ | |
name: '', | |
author: '', | |
isbn: '', | |
price: 30, | |
amount: 5 | |
}, | |
{ | |
name: '', | |
author: '', | |
isbn: '', | |
price: 9, | |
amount: 3 | |
} | |
]; | |
it('getTotalPrice return an amount', () => { | |
const totalPrice = component.getTotalPrice(listBook); | |
//this method returned a value greater than 0. | |
// expect(totalPrice).toBeGreaterThan(0); | |
//with this method you will check that it is not 0. You can expect several options. | |
expect(totalPrice).not.toBe(0); | |
//expect(totalPrice).toBeNull(); | |
}) |
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
import { Component, OnInit } from '@angular/core'; | |
import { BookService } from '../../services/book.service'; | |
import { Book } from '../../models/book.model'; | |
@Component({ | |
selector: 'app-cart', | |
templateUrl: './cart.component.html', | |
styleUrls: ['./cart.component.scss'] | |
}) | |
export class CartComponent implements OnInit { | |
public listCartBook: Book[] = []; | |
public totalPrice = 0; | |
public Math = Math; | |
constructor( | |
private readonly _bookService: BookService | |
) { } | |
ngOnInit(): void { | |
this.listCartBook = this._bookService.getBooksFromCart(); | |
this.totalPrice = this.getTotalPrice(this.listCartBook); | |
} | |
public getTotalPrice(listCartBook: Book[]): number { | |
let totalPrice = 0; | |
listCartBook.forEach((book: Book) => { | |
totalPrice += book.amount * book.price; | |
}); | |
return totalPrice; | |
} | |
public onInputNumberChange(action: string, book: Book): void { | |
const amount = action === 'plus' ? book.amount + 1 : book.amount - 1; | |
book.amount = Number(amount); | |
this.listCartBook = this._bookService.updateAmountBook(book); | |
this.totalPrice = this.getTotalPrice(this.listCartBook); | |
} | |
public onClearBooks(): void { | |
if (this.listCartBook && this.listCartBook.length > 0) { | |
this._clearListCartBook(); | |
} else { | |
console.log("No books available"); | |
} | |
} | |
private _clearListCartBook() { | |
this.listCartBook = []; | |
this._bookService.removeBooksFromCart(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment