-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductItem.spec.js
70 lines (58 loc) · 1.66 KB
/
ProductItem.spec.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
63
64
65
66
67
68
69
70
import React from 'react'
import { shallow } from 'enzyme'
import Product from './Product'
import ProductItem from './ProductItem'
const setup = product => {
const actions = {
onAddToCartClicked: jest.fn()
}
const component = shallow(
<ProductItem product={product} {...actions} />
)
return {
component: component,
actions: actions,
button: component.find('button'),
product: component.find(Product)
}
}
let productProps
describe('ProductItem component', () => {
beforeEach(() => {
productProps = {
title: 'Product 1',
price: 9.99,
inventory: 6
}
})
it('should render product', () => {
const { product } = setup(productProps)
expect(product.props()).toEqual({ title: 'Product 1', price: 9.99, quantity: 6 })
})
it('should render Add To Cart message', () => {
const { button } = setup(productProps)
expect(button.text()).toMatch(/^Add to cart/)
})
it('should not disable button', () => {
const { button } = setup(productProps)
expect(button.prop('disabled')).toEqual('')
})
it('should call action on button click', () => {
const { button, actions } = setup(productProps)
button.simulate('click')
expect(actions.onAddToCartClicked).toBeCalled()
})
describe('when product inventory is 0', () => {
beforeEach(() => {
productProps.inventory = 0
})
it('should render Sold Out message', () => {
const { button } = setup(productProps)
expect(button.text()).toMatch(/^Sold Out/)
})
it('should disable button', () => {
const { button } = setup(productProps)
expect(button.prop('disabled')).toEqual('disabled')
})
})
})