Skip to content

Instantly share code, notes, and snippets.

@ConxiCR
Last active March 28, 2022 11:45
Show Gist options
  • Save ConxiCR/d2715c7ed990778c902e65df912581b3 to your computer and use it in GitHub Desktop.
Save ConxiCR/d2715c7ed990778c902e65df912581b3 to your computer and use it in GitHub Desktop.
testing with Jasmine and Karma
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
});
//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();
})
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