File tree Expand file tree Collapse file tree 2 files changed +37
-8
lines changed Expand file tree Collapse file tree 2 files changed +37
-8
lines changed Original file line number Diff line number Diff line change 1+ import crypto from "crypto" ;
2+
13export 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}
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments