-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathproducts.test.ts
More file actions
92 lines (86 loc) · 2.51 KB
/
products.test.ts
File metadata and controls
92 lines (86 loc) · 2.51 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import mockAxios from 'axios'
import { Tokens, Products, Product } from '../src'
import { makeToken } from './utils'
const apiUrl = 'https://api.ordercloud.io/v1'
const validToken = makeToken()
beforeEach(() => {
jest.clearAllMocks() // cleans up any tracked calls before the next test
Tokens.RemoveAccessToken()
})
test('can create product', async () => {
Tokens.SetAccessToken(validToken)
const product: Product = {
Name: 'Tennis Balls',
ID: 'TB2038',
}
await Products.Create(product)
expect(mockAxios.post).toHaveBeenCalledTimes(1)
expect(mockAxios.post).toHaveBeenCalledWith(`${apiUrl}/products`, product, {
paramsSerializer: expect.any(Object),
timeout: 60000,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${validToken}`,
},
})
})
test('can patch product', async () => {
Tokens.SetAccessToken(validToken)
const productID = 'mockproductid'
const partialProduct: Partial<Product> = {
Description: 'This product is pretty sweet, trust me',
}
await Products.Patch(productID, partialProduct)
expect(mockAxios.patch).toHaveBeenCalledTimes(1)
expect(mockAxios.patch).toHaveBeenCalledWith(
`${apiUrl}/products/${productID}`,
partialProduct,
{
paramsSerializer: expect.any(Object),
timeout: 60000,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${validToken}`,
},
}
)
})
test('can update product', async () => {
Tokens.SetAccessToken(validToken)
const productID = 'mockproductid'
const product: Product = {
Name: 'Tennis Balls',
Description: 'This product is pretty sweet, trust me',
}
await Products.Save(productID, product)
expect(mockAxios.put).toHaveBeenCalledTimes(1)
expect(mockAxios.put).toHaveBeenCalledWith(
`${apiUrl}/products/${productID}`,
product,
{
paramsSerializer: expect.any(Object),
timeout: 60000,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${validToken}`,
},
}
)
})
test('can delete product', async () => {
Tokens.SetAccessToken(validToken)
const productID = 'mockproductid'
await Products.Delete(productID)
expect(mockAxios.delete).toHaveBeenCalledTimes(1)
expect(mockAxios.delete).toHaveBeenCalledWith(
`${apiUrl}/products/${productID}`,
{
paramsSerializer: expect.any(Object),
timeout: 60000,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${validToken}`,
},
}
)
})