Skip to content

Commit 599eb96

Browse files
committed
Refactor Carrito class and enhance tests for item management and checkout total
1 parent a3562b6 commit 599eb96

File tree

2 files changed

+96
-14
lines changed

2 files changed

+96
-14
lines changed

examples/Carrito.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
export class Carrito {
2-
items = 0;
2+
// items = 0;
3+
4+
items = [];
5+
6+
// Aproximación A
7+
totalCheckout = 0;
38

49
getTotalItems() {
5-
return this.items;
10+
return this.items.length;
611
}
712

8-
getTotalCheckout() {
9-
return 10;
13+
getTotatCheckout() {
14+
// return 10;
15+
// return this.items * 10;
16+
// return this.items == 1 ? 10 : 20;
17+
return this.totalCheckout;
1018
}
1119

1220
addItem(item) {
13-
if (!item.price || !item.name)
14-
throw new Error("Item must have price and name");
21+
// if ( typeof item !== 'object' ) throw new Error('Item must be an object');
22+
// if (
23+
// !item.price ||
24+
// !item.name
25+
// ) {
26+
// throw new Error('Item must have price and name');
27+
// }
28+
this.checkItem(item);
29+
this.items.push(item);
30+
this.totalCheckout += item.price;
31+
}
1532

16-
if (typeof item !== "object") {
17-
throw new Error("Item must be an object");
33+
checkItem(item) {
34+
if (typeof item !== "object") throw new Error("Item must be an object");
35+
if (!item.price || !item.name) {
36+
throw new Error("Item must have price and name");
1837
}
19-
this.items++;
38+
}
39+
40+
removeItem() {
41+
return [];
2042
}
2143
}

examples/Carrito.spect.js

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ describe("Testing de la clase Carrito", () => {
66
name: "sushiItem",
77
};
88

9+
const waterItem = {
10+
price: 1.5,
11+
name: "waterItem",
12+
};
13+
914
let carrito;
1015
beforeEach(() => {
1116
carrito = new Carrito();
@@ -59,12 +64,67 @@ describe("Testing de la clase Carrito", () => {
5964
});
6065

6166
describe("Testeando getTotalCheckout", () => {
67+
it("Carrito.getTotatCheckout debe devolver 10 despues de añadir 1 sushiItem", () => {
68+
carrito.addItem(sushiItem);
69+
expect(carrito.getTotatCheckout()).toEqual(10);
70+
});
71+
72+
it("Carrito.getTotatCheckout debe devolver 20 despues de añadir 2 sushiItem", () => {
73+
carrito.addItem(sushiItem);
74+
carrito.addItem(sushiItem);
75+
expect(carrito.getTotatCheckout()).toEqual(20);
76+
});
77+
78+
it("Carrito.getTotatCheckout debe devolver 11,5 despues de añadir 1 sushiItem i 1 waterItem", () => {
79+
carrito.addItem(sushiItem);
80+
carrito.addItem(waterItem);
81+
expect(carrito.getTotatCheckout()).toEqual(11.5);
82+
});
83+
84+
it("Debe devolver 0 si el carrito esta vacío", () => {
85+
expect(carrito.getTotatCheckout()).toEqual(0);
86+
});
87+
});
88+
89+
describe("Testeando addItem (detail)", () => {
90+
it("Debe contener el item añadido en la propiedad carrito.items", () => {
91+
carrito.addItem(sushiItem);
92+
expect(carrito.items).toPartiallyContain(sushiItem);
93+
});
94+
95+
it("Carrito.items debe ser un array vacío si no añadimos ningun elemento", () => {
96+
expect(carrito.items).toBeEmpty();
97+
});
98+
99+
it("Carrito debe llamar a una función checkItem antes de añadirlo al carrito.", () => {
100+
const spy = jest.spyOn(carrito, "checkItem");
101+
carrito.addItem(sushiItem);
102+
// expect(jest.spyOn(carrito, 'checkItem')).toHaveBeenCalled(); Es lo mismo que definir una variable.
103+
expect(spy).toHaveBeenCalled();
104+
});
105+
106+
it("Carrito debe llamar una única vez a checkItem cuando añadimos un elemento", () => {
107+
const spy = jest.spyOn(carrito, "checkItem");
108+
carrito.addItem(sushiItem);
109+
expect(spy).toHaveBeenCalledOnce();
110+
expect(spy).toHaveBeenCalledTimes(1);
111+
});
112+
113+
it("Carrito debe llamar a una función checkItem con el valor del item a añadir", () => {
114+
const spy = jest.spyOn(carrito, "checkItem");
115+
carrito.addItem(sushiItem);
116+
expect(spy).toHaveBeenCalledWith(sushiItem);
117+
});
118+
});
119+
120+
describe("Testeando removeItem", () => {
121+
it("Carrito.removeItem debe devolver un array vacio despues de añadir un elemento y eliminarlo", () => {
122+
carrito.addItem(waterItem);
123+
expect(carrito.removeItem(waterItem)).toHaveLength(0);
124+
});
125+
62126
it.todo(
63-
"Carrito.getTotatCheckout debe devolver 10 despues de añadir 1 sushiItem",
64-
() => {
65-
carrito.addItem(sushiItem);
66-
expect(carrito.getTotalCheckout()).toEqual(1);
67-
}
127+
"Carrito.removeItem debe devolver un array con un elemento cuando añadimos dos elementos distintos y eliminamos uno"
68128
);
69129
});
70130
});

0 commit comments

Comments
 (0)