test files
Carttest.js
import React from "react";
import Cart from "../components/cart";
import { shallow } from "enzyme";
import configureStore from "redux-mock-store";
import Phone from "../images/smartphone.jpg";
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
configure({ adapter: new Adapter() });
const mockStore = configureStore([]);
describe("cart component", () => {
let store;
const mockfn1 = jest.fn();
window.alert = jest.fn();
const historyMock = { push: jest.fn() };
let component;
let empty;
beforeEach(() => {
store = mockStore({
addedItems: [
{id: 1,src: Phone,name: "1Plus7t",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB"},
{id: 2,src: Phone,name: "samsung",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB"}],
total: 0,
shippingPrice: 50,
myOrder: [],
view: [],
Amount: [],
price: 0
});
component = shallow(
<Cart
store={store}
onOp1={mockfn1}
onOp2={mockfn1}
onInc={mockfn1}
history={historyMock}
/>
)
.childAt(0)
.dive();
empty = mockStore({
addedItems: []
});
});
it("cart items", () => {
const spy =jest.spyOn(component.instance(),"handleOrder");
const shipping = component.find("p").at(1);
expect(shipping.render().text()).toContain("Choose your shipping");
const Place_Order = component.find("#place_order");
expect(Place_Order.render().text()).toContain("Place Order");
Place_Order.simulate("click");
expect(spy).toHaveBeenCalledTimes(1);
});
it("cart list", () => {
const opt1 = component.find(".Card");
expect(opt1.length).toBe(2);
const items1 = opt1.at(0);
const item1 = items1.find("table tbody tr").map(child => child.text());
expect(item1).toEqual(["1Plus7tRs.30000+-Delete"]);
const items2 = opt1.at(1);
const item2 = items2.find("table tbody tr").map(child => child.text());
expect(item2).toEqual(["samsungRs.23000+-Delete"]);
});
it("button click", () => {
const spy =jest.spyOn(component.instance(),"handleIncr");
const opt1 = component.find(".Card").at(0);
const item1 = opt1.find("table tbody tr td").at(3);
const btn = item1.find("#increment");
btn.simulate("click");
expect(spy).toHaveBeenCalledTimes(1);
});
it("onchange shipping price", () => {
const spy1 =jest.spyOn(component.instance(),"onOption1");
const spy2 =jest.spyOn(component.instance(),"onOption2");
const option1 = component.find("#option1");
expect(option1.length).toBe(1);
option1.simulate("change" );
expect(spy1).toHaveBeenCalledTimes(1);
const option2 = component.find("#option2");
option2.simulate("change");
expect(spy2).toHaveBeenCalledTimes(1);
});
it("empty cart check", () => {
var data = [];
const wrapper = shallow(<Cart store={empty} />)
.childAt(0)
.dive();
const nav = wrapper.find("Navbar");
expect(nav.length).toBe(1);
const cart_msg = wrapper.find("p");
expect(cart_msg.text()).toBe("Your cart is empty");
});
});
cartReducer.test.js
import cartReducer from "../components/reducer/cartReducer";
import Phone from "../images/smartphone.jpg";
describe("testing cart reducer", () => {
const initState = {
items: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue", RAM: "8GB",Storage: "128GB"},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB"},
{id: 3,src: Phone,name: "MI A7",price: 19000,color: "Frosted Silver",RAM: "6GB",Storage: "64GB"},
{id: 4,src: Phone,name: "REAL ME",price: 20000,color: "Nebula Blue",RAM: "12GB",Storage: "32GB"},
{id: 5,src: Phone,name: "MOTO",price: 23000,color: "Glacier Blue",RAM: "4GB",Storage: "32GB"},
{id: 6,src: Phone,name: "VIVO",price: 19000,color: "Frosted Silver",RAM: "6GB",Storage: "64GB"},
{id: 7,src: Phone,name: "OPPO",price: 20000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB"},
{id: 8,src: Phone,name: "REDMI",price: 20000,color: "Nebula Blue",RAM: "4GB",Storage: "64GB"}
],
addedItems: [],
total: 0,
shippingPrice: 0,
myOrder: [],
view: [],
Amount: [],
price: 0
};
it("to check option1", () => {
expect(cartReducer({}, { type: "on_Op1" })).toEqual({
shippingPrice: 50
});
});
it("to check option2", () => {
expect(cartReducer({}, { type: "on_Op2" })).toEqual({
shippingPrice: 100
});
});
it("handling Add to cart", () => {
const state = {
view: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB"},
],
addedItems: [],
total: 0,
};
expect(cartReducer(state, { type: "on_Add_To_Cart", id: 1 })).toEqual({
addedItems: [{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB",quantity:1}],
total: 30000,
view:state.view,
shippingPrice:0
});
});
it("delete from cart", () => {
const state = {
addedItems: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB",quantity: 1},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB",quantity: 1}
],
total: 53000
};
expect(cartReducer(state, { type: "on_Delete", id: 2 })).toEqual({
addedItems: [{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB",quantity: 1}],
total: 30000
});
});
it("cancel order", () => {
const state={
myOrder:[
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB"},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB"}
]
}
expect(cartReducer({}, { type: "Cancel_Order" })).toEqual({
myOrder: [],
total:0
});
});
it("view check", () => {
const state = {
items: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB"},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB"}
]
};
expect(cartReducer(state, { type: "View", id: 1 })).toEqual({
view: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB"}
],
items: state.items
});
});
it("check place order", () => {
const state = {
addedItems: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB",quantity:1},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB",quantity:1}
],
total: 53000,
shippingPrice: 50,
Amount: 0,
myOrder: [],
price: 0
};
expect(cartReducer(state, { type: "Place_Order" })).toEqual({
myOrder: state.addedItems,
Amount: state.total,
price: state.shippingPrice,
addedItems: [],
total: 0,
shippingPrice: 0
});
});
it("increment check", () => {
const state = {
addedItems: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB",quantity: 2},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB",quantity: 1}
],
total: 83000
};
expect(cartReducer(state, { type: "on_Inc", id: 2 })).toEqual({
addedItems: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB",quantity: 2},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB",quantity: 2}
],
total: 106000
});
});
it("decrement check", () => {
const state = {
addedItems: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB",quantity: 2},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB",quantity: 1}],
total: 83000
};
expect(cartReducer(state, { type: "on_Decr", id: 1 })).toEqual({
addedItems: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB",quantity: 1},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB",quantity: 1}
],
total: 53000
});
});
});
Nav.test.js
import React from "react";
import Navbar from "../components/nav";
import { shallow } from "enzyme";
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
configure({ adapter: new Adapter() });
describe("testing NavBar", () => {
it("navbar_content", () => {
const component = shallow(<Navbar />);
const div = component.find("div").at(0);
expect(div.length).toBe(1);
const nav = component.find("nav").at(0);
expect(nav.length).toBe(1);
const Cart=(component.find("Link")).at(1);
expect(Cart.props().to).toContain("/cart");
const Order=(component.find("Link")).at(2);
expect(Order.props().to).toContain("/myorder");
});
});
Order.test.js
import React from "react";
import Order from "../components/order";
import { shallow } from "enzyme";
import configureStore from "redux-mock-store";
import Phone from "../images/smartphone.jpg";
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
configure({ adapter: new Adapter() });
const mockStore = configureStore([]);
describe("order component", () => {
let store;
let component;
let empty;
const mockLoginfn1 = jest.fn();
window.alert = jest.fn();
beforeEach(() => {
store = mockStore({
addedItems: [],
total: 0,
shippingPrice: 0,
myOrder: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB"},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB"}
],
view: [],
Amount: [],
price: 0
});
empty = mockStore({
myOrder: []
});
component = shallow(<Order store={store} onCancel={mockLoginfn1} />)
.childAt(0)
.dive();
});
it("your order recipt", () => {
const spy =jest.spyOn(component.instance(),"handleCancel");
const firstRow = component.find("h1");
expect(firstRow.render().text()).toContain("Your Order");
const btn_cancel = component.find("button");
expect(btn_cancel.render().text()).toContain("Cancel Order");
btn_cancel.simulate("click");
expect(spy).toHaveBeenCalledTimes(1);
expect(window.alert).toHaveBeenCalledWith('Your order has been cancelled');
});
it("product ordered", () => {
const Card = component.find(".Order_card");
const pro1 = Card.at(0);
const products = pro1.find("ul").map(child => child.text());
expect(products).toEqual([
"Product Name:ONEPLUSQuantity:Price:Rs.30000",
"Product Name:SAMSUNGQuantity:Price:Rs.23000"
]);
});
it("empty order check", () => {
const wrapper = shallow(<Order store={empty} />)
.childAt(0)
.dive();
const nav = wrapper.find("Navbar");
expect(nav.length).toBe(1);
const cart_msg = wrapper.find("p");
expect(cart_msg.text()).toBe("No orders yet");
});
});
Product.test.js
import React from "react";
import Product from "../components/Product";
import { shallow } from "enzyme";
import configureStore from "redux-mock-store";
import Phone from "../images/smartphone.jpg";
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import { equal } from "assert";
configure({ adapter: new Adapter() });
const mockStore = configureStore([]);
describe("Product component", () => {
const historyMock = { push: jest.fn() };
const mockfn1 = jest.fn();
let store;
let component;
beforeEach(() => {
store = mockStore({
items: [
{id: 1,src: Phone,name: "ONEPLUS",price: 30000,color: "Nebula Blue", RAM: "8GB",Storage: "128GB"},
{id: 2,src: Phone,name: "SAMSUNG",price: 23000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB"},
{id: 3,src: Phone,name: "MI A7",price: 19000,color: "Frosted Silver",RAM: "6GB",Storage: "64GB"},
{id: 4,src: Phone,name: "REAL ME",price: 20000,color: "Nebula Blue",RAM: "12GB",Storage: "32GB"},
{id: 5,src: Phone,name: "MOTO",price: 23000,color: "Glacier Blue",RAM: "4GB",Storage: "32GB"},
{id: 6,src: Phone,name: "VIVO",price: 19000,color: "Frosted Silver",RAM: "6GB",Storage: "64GB"},
{id: 7,src: Phone,name: "OPPO",price: 20000,color: "Glacier Blue",RAM: "8GB",Storage: "128GB"},
{id: 8,src: Phone,name: "REDMI",price: 20000,color: "Nebula Blue",RAM: "4GB",Storage: "64GB"}
],
addedItems: [],
total: 0,
shippingPrice: 0,
myOrder: [],
view: [],
Amount: [],
price: 0
});
component = shallow(
<Product store={store} onView={mockfn1} history={historyMock} />
) .childAt(0)
.dive();
});
it("product content", () => {
expect(component.find("Navbar").length).toBe(1);
expect(component.find(".card").length).toBe(8);
});
it("product card", () => {
const card = component.find(".card").at(0);
const Product = card.find("ul").map(child => child.text());
expect(Product).toEqual(["ONEPLUSRs.30000View Product"]);
});
it("view button click", () => {
const card = component.find(".card").at(0);
const spy =jest.spyOn(component.instance(),"handleView")
const viewPro = card
.find("ul")
.find("li")
.at("3");
const btn = viewPro.find("button");
expect(btn.length).toBe(1);
btn.simulate("click");
expect(spy).toHaveBeenCalledTimes(1);
});
});
View.test.js
import React from "react";
import View from "../components/view";
import { shallow } from "enzyme";
import configureStore from "redux-mock-store";
import Phone from "../images/smartphone.jpg";
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
configure({ adapter: new Adapter() });
const mockStore = configureStore([]);
describe("view component", () => {
const historyMock = { push: jest.fn() };
let store;
let component;
const mockfn1 = jest.fn();
beforeEach(() => {
store = mockStore({
view: [
{id: 1,src: Phone,name: "1Plus7t",price: 30000,color: "Nebula Blue",RAM: "8GB",Storage: "128GB"}]
});
component = shallow(
<View store={store} onAdd={mockfn1} history={historyMock} />
)
.childAt(0)
.dive();
});
it("view content", () => {
expect(component.find("Navbar").length).toBe(1);
});
it("view list", () => {
const productName = component.find(".view_card");
expect(productName.length).toEqual(1);
const item1 = productName
.at(0)
.find("tr")
.map(child => child.text());
expect(item1).toEqual([
"1Plus7tColor:Nebula BlueRAM:8GBStorage:128GBPrice:Rs.30000Add To Cart"
]);
});
it("view button click", () => {
const spy =jest.spyOn(component.instance(),"handleClick");
const productName = component.find(".view_card");
expect(productName.length).toEqual(1);
const item1 = (productName.at(0).find("table")).find("tbody");
const item2 = (item1.find("tr").at(0)).find("td").at(1);
const Add_to_cart = item2.find("#viewProduct");
expect(Add_to_cart.length).toBe(1);
Add_to_cart.simulate("click");
expect(spy).toHaveBeenCalled();
});
});