-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMenu.test.js
90 lines (85 loc) · 2.76 KB
/
Menu.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
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
/**
* **** Integration Testing ****
*
* Testing Cart items Functionality:
* Step1. Load RestaurantDetail Component
* Step2. write Test Cases:
* - Shimmer should load on restaurant-detail page
* - Menu should load on restaurant-detail page
* - Add items to the cart on restaurant-detail page
*/
import "@testing-library/jest-dom"
import { render, act, waitFor, fireEvent } from "@testing-library/react"
import { StaticRouter } from "react-router-dom/server"
import { Provider } from "react-redux"
import store from "../../utils/store"
import RestaurantDetail from "../RestaurantDetail"
import Header from "../Header"
import { MENU_DATA } from "../../mocks/data"
// Creating a fetch() mock function with jest.fn().
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => {
return Promise.resolve(MENU_DATA)
}
})
})
test("Shimmer should load on restaurant-detail page", async () => {
// Load RestaurantDetail Component
let restaurantDetail
await waitFor(() => {
restaurantDetail = render(
<StaticRouter>
<Provider store={store}>
<RestaurantDetail />
</Provider>
</StaticRouter>
)
})
// Shimmer should load on restaurant-detail page
const shimmer = await restaurantDetail.getByTestId("shimmer")
/* Instead of wrapping render() in waitFor() directly use findByTestId() */
// const shimmer = await restaurantDetail.findByTestId("shimmer")
expect(shimmer.children.length).toBe(10)
})
test("Menu should load on restaurant-detail page", async () => {
// Load RestaurantDetail Component
let restaurantDetail
await waitFor(() => {
restaurantDetail = render(
<StaticRouter>
<Provider store={store}>
<RestaurantDetail />
</Provider>
</StaticRouter>
)
})
// Shimmer should load on restaurant-detail page
await waitFor(() => expect(restaurantDetail.getByTestId("menu")))
const restaurantMenu = restaurantDetail.getByTestId("menu")
expect(restaurantMenu.children.length).toBe(62)
})
test("Add menu to cart", async () => {
// load restaurant-detail & header
let body
await waitFor(() => {
body = render(
<StaticRouter>
<Provider store={store}>
<Header />
<RestaurantDetail />
</Provider>
</StaticRouter>
)
})
// check the total cart items on button click
await waitFor(() => expect(body.getByTestId("menu")))
// fire click thrice to add 3 menu itemsw
const allAddBtn = body.getAllByTestId("add-btn") // Note: getAllByTestId() vs getByTestId()
fireEvent.click(allAddBtn[0])
fireEvent.click(allAddBtn[1])
fireEvent.click(allAddBtn[2])
// check whether 3-items has been added to the cart
const cart = body.getByTestId("cart-items")
expect(cart.innerHTML).toBe("3")
})