-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.test.js
62 lines (61 loc) · 2.13 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const ShopCart = require('./index');
const Item = require('./Item');
describe('ES6 Recap Home Work', () => {
test('We can add item to cart', () => {
const cart = new ShopCart();
const Foo = new Item(1, 'Foo');
const Bar = new Item(2, 'Bar');
cart.add(Foo, 3);
cart.add(Bar, 2);
expect(cart.items.length).toBe(2);
cart.add(Foo, 7);
expect(cart.items[0].count).toBe(10);
expect(cart.items[1].count).toBe(2);
});
test('We can remove item from cart', () => {
const cart = new ShopCart();
const Foo = new Item(1, 'Foo');
const Bar = new Item(2, 'Bar');
cart.add(Foo, 3);
cart.add(Bar, 2);
cart.remove(Foo);
expect(cart.items.length).toBe(1);
});
test('We can clear cart', () => {
const cart = new ShopCart();
const Foo = new Item(1, 'Foo');
const Bar = new Item(2, 'Bar');
cart.add(Foo, 3);
cart.add(Bar, 2);
cart.clear();
expect(cart.items.length).toBe(0);
});
test('We can change quantity items in cart', () => {
const cart = new ShopCart();
const Foo = new Item(1, 'Foo');
const Bar = new Item(2, 'Bar');
cart.add(Foo, 3);
cart.add(Bar, 2);
cart.setCount(Bar, 20);
expect(cart.items.length).toBe(2);
expect(cart.items[0].count).toBe(3);
expect(cart.items[1].count).toBe(20);
});
test('We can`t set negative amount', () => {
const cart = new ShopCart();
const Foo = new Item(1, 'Foo');
const Bar = new Item(2, 'Bar');
cart.add(Foo, 3);
cart.add(Bar, 2);
expect(() => cart.setCount(Bar, 0)).toThrow('Count should be greater than zero');
expect(() => cart.setCount(Bar, -2)).toThrow('Count should be greater than zero');
});
test('We can convert Cart Object to String', () => {
const cart = new ShopCart();
const Foo = new Item(1, 'Foo');
const Bar = new Item(2, 'Bar');
cart.add(Foo, 10);
cart.add(Bar, 22);
expect(cart.toString()).toBe('In your cart 32 items');
});
});