-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.spec.ts
78 lines (67 loc) · 1.94 KB
/
store.spec.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {} from 'jest';
import {
defaultState,
actionTypes,
reducer,
initStore,
addToCart,
removeFromCart,
ActionInterface,
} from './store';
const productId: string = '1';
const addToCartAction: ActionInterface = {
type: actionTypes.ADD_TO_CART,
payload: productId,
};
const removeFromCartAction: ActionInterface = {
type: actionTypes.REMOVE_FROM_CART,
payload: productId,
};
describe('defaultState', () => {
it('has products', () => {
expect(defaultState).toHaveProperty('products');
});
it('has cart', () => {
expect(defaultState).toHaveProperty('cart');
});
});
describe('reducer', () => {
it('returns default state', () => {
const state = reducer(undefined, { type: null });
expect(state).toEqual(defaultState);
});
it('adds a product to the cart', () => {
const state = reducer(undefined, addToCartAction);
const cartProduct = {
productId,
quantity: 1,
};
expect(state.cart).toHaveProperty(productId);
expect(state.cart[productId]).toEqual(cartProduct);
});
it('increases the quantity if the item is already in the cart', () => {
const state = reducer(undefined, addToCartAction);
const state2 = reducer(state, addToCartAction);
const cartProduct = {
productId,
quantity: 2,
};
expect(state2.cart[productId]).toEqual(cartProduct);
});
it('removes a product from the cart', () => {
const stateWithProductInCart = reducer(undefined, addToCartAction);
const stateWithoutProductInCart = reducer(
stateWithProductInCart,
removeFromCartAction,
);
expect(stateWithoutProductInCart.cart[productId]).toBe(undefined);
});
});
describe('actions', () => {
it('creates action to add a product to the cart', () => {
expect(addToCart(productId)).toEqual(addToCartAction);
});
it('creates and action to remove a product from the cart', () => {
expect(removeFromCart(productId)).toEqual(removeFromCartAction);
});
});