Skip to content

Commit f715903

Browse files
committed
Enhance Carrito class: implement unique item identification in addItem and update removeItem logic; improve tests for item management
1 parent 2faa7bc commit f715903

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

examples/Carrito.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import crypto from "crypto";
2+
13
export class Carrito {
24
// items = 0;
35

@@ -26,7 +28,23 @@ export class Carrito {
2628
// throw new Error('Item must have price and name');
2729
// }
2830
this.checkItem(item);
29-
this.items.push(item);
31+
32+
// Aproximación con cantidad
33+
// if (this.items.some(i => i.name === item.name)) {
34+
// const findItem = this.items.find(i => i.name === item.name);
35+
// findItem.qt += 1;
36+
// } else {
37+
// this.items.push({
38+
// ...item,
39+
// qt: 1
40+
// });
41+
// }
42+
43+
this.items.push({
44+
...item,
45+
id: crypto.randomUUID(),
46+
});
47+
// this.items.push(item);
3048
this.totalCheckout += item.price;
3149
}
3250

@@ -38,7 +56,22 @@ export class Carrito {
3856
}
3957

4058
removeItem(item) {
41-
this.items = this.items.filter((i) => i.name !== item.name);
59+
// this.items = this.items.filter( i => i.name !== item.name );
60+
// return this.items;
61+
62+
// Aproximación con cantidad
63+
// const findItem = this.items.find(i => i.name === item.name);
64+
// if (findItem.qt === 1) {
65+
// // Elimino el elemento
66+
// this.items = this.items.filter( i => i.name !== item.name );
67+
// } else {
68+
// // Reducimos qt
69+
// findItem.qt -= 1;
70+
// }
71+
72+
// Aproximación con ID
73+
const findItem = this.items.find((i) => i.name === item.name);
74+
this.items = this.items.filter((i) => i.id != findItem.id);
4275
return this.items;
4376
}
4477
}

examples/Carrito.spect.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,12 @@ describe("Testing de la clase Carrito", () => {
136136
expect(carrito.items).toHaveLength(1);
137137
});
138138

139-
/* it.todo(
140-
"Carrito.items debe ser un array con DOS elementos si añadimos dos sushiItem y un waterItem y eliminamos un sushiItem"
141-
);
142-
143-
it("", () => {
139+
it("Carrito.items debe ser un array con DOS elementos si añadimos dos sushiItem y un waterItem y eliminamos un sushiItem", () => {
144140
carrito.addItem(sushiItem);
145141
carrito.addItem(sushiItem);
146142
carrito.addItem(waterItem);
147143
carrito.removeItem(sushiItem);
148144
expect(carrito.items).toHaveLength(2);
149-
}); */
145+
});
150146
});
151147
});

0 commit comments

Comments
 (0)