import React from
'react'
;
import Adapter from
'enzyme-adapter-react-16'
;
import { expect } from
'chai'
;
import { shallow, mount, configure } from
'enzyme'
;
import TestUtils from
'react-dom/test-utils'
;
import App from
'./app/components/App'
;
import jsdom from
'jsdom'
;
import { findDOMNode } from
'react-dom'
;
configure({ adapter:
new
Adapter() });
beforeAll(() => {
global.fetch = jest.fn();
});
let wrapper;
beforeEach(() => {
wrapper = shallow(< App />, { disableLifecycleMethods:
true
});
});
afterEach(() => {
wrapper.unmount();
});
if
(
typeof
document ===
'undefined'
) {
global.document = jsdom.jsdom(
'<!doctype html><html><body></body></html>'
);
global.window = document.defaultView;
global.navigator = global.window.navigator;
}
describe(
'DOM Rendering'
,
function
() {
it(
'Add functionality to add new products
by clicking add'
,
function
() {
const app = TestUtils.renderIntoDocument(<App />);
const appDOM = findDOMNode(app);
let productItemsLength =
appDOM.querySelectorAll(
'.todo-text'
).length;
let addInput = appDOM.querySelector(
'input'
);
addInput.value =
'Add item'
;
let addButton = appDOM.querySelector(
'.add-todo button'
);
TestUtils.Simulate.click(addButton);
console.log(appDOM.querySelectorAll(
'.todo-text'
).length);
expect(appDOM.querySelectorAll(
'.todo-text'
)
.length).to.be.equal(productItemsLength + 3);
});
});
describe(
'DOM Rendering'
,
function
() {
it(
'On deleting, the item should get deleted'
,
function
() {
const app = TestUtils.renderIntoDocument(<App />);
let productItems = TestUtils
.scryRenderedDOMComponentsWithTag(app,
'li'
);
let productLength = productItems.length;
let deleteButton = productItems[0]
.querySelector(
'button'
);
TestUtils.Simulate.click(deleteButton);
let productItemsAfterClick = TestUtils
.scryRenderedDOMComponentsWithTag(app,
'li'
);
expect(productItemsAfterClick.length)
.to.equal(productLength - 1);
});
});
describe(
'Enzyme Shallow'
,
function
() {
it(
'App\'s title should be Available Products'
,
function
() {
let app = shallow(<App />);
expect(app.find(
'h1'
).text())
.to.equal(
'Available Products'
);
});
});
describe(
'Enzyme Mount'
,
function
() {
it(
'Delete An Item'
,
function
() {
let app = mount(<App />);
let itemLength = app.find(
'li'
).length;
app.find(
'button.delete'
).at(0).simulate(
'click'
);
expect(app.find(
'li'
).length).to.equal(itemLength - 1);
});
});