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
+
1
3
export class Carrito {
2
4
// items = 0;
3
5
@@ -26,7 +28,23 @@ export class Carrito {
26
28
// throw new Error('Item must have price and name');
27
29
// }
28
30
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);
30
48
this . totalCheckout += item . price ;
31
49
}
32
50
@@ -38,7 +56,22 @@ export class Carrito {
38
56
}
39
57
40
58
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 ) ;
42
75
return this . items ;
43
76
}
44
77
}
Original file line number Diff line number Diff line change @@ -136,16 +136,12 @@ describe("Testing de la clase Carrito", () => {
136
136
expect ( carrito . items ) . toHaveLength ( 1 ) ;
137
137
} ) ;
138
138
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" , ( ) => {
144
140
carrito . addItem ( sushiItem ) ;
145
141
carrito . addItem ( sushiItem ) ;
146
142
carrito . addItem ( waterItem ) ;
147
143
carrito . removeItem ( sushiItem ) ;
148
144
expect ( carrito . items ) . toHaveLength ( 2 ) ;
149
- }); */
145
+ } ) ;
150
146
} ) ;
151
147
} ) ;
You can’t perform that action at this time.
0 commit comments