use enzyme library for testing updated some tests

new test for newtag component
This commit is contained in:
2020-06-10 15:41:41 +02:00
parent bb18a62a3d
commit 751e09f54b
8 changed files with 86 additions and 51 deletions

View File

@ -75,14 +75,14 @@ class AddTagPopup extends React.Component {
updateRequest.append('movieid', this.props.movie_id);
fetch('/api/videoload.php', {method: 'POST', body: updateRequest})
.then((response) => response.json())
.then((result) => {
if (result.result !== "success") {
console.log("error occured while writing to db -- todo error handling");
console.log(result.result);
}
this.props.onHide();
});
.then((response) => response.json()
.then((result) => {
if (result.result !== "success") {
console.log("error occured while writing to db -- todo error handling");
console.log(result.result);
}
this.props.onHide();
}));
}
}

View File

@ -1,17 +1,60 @@
import React from "react";
import ReactDom from 'react-dom'
import {render, cleanup} from '@testing-library/react'
import {shallow} from 'enzyme'
import "@testing-library/jest-dom"
import AddTagPopup from "./AddTagPopup";
afterEach(cleanup);
describe('<AddTagPopup/>', function () {
it('renders without crashing ', function () {
const div = document.createElement("div");
ReactDom.render(<AddTagPopup/>,div);
ReactDom.unmountComponentAtNode(div);
const wrapper = shallow(<AddTagPopup/>);
wrapper.unmount();
});
it('test dropdown insertion', function () {
const wrapper = shallow(<AddTagPopup/>);
wrapper.setState({items: ["test1", "test2", "test3"]});
expect(wrapper.find('DropdownItem')).toHaveLength(3);
});
it('test storeseletion click event', done => {
const mockSuccessResponse = {};
const mockJsonPromise = Promise.resolve(mockSuccessResponse);
const mockFetchPromise = Promise.resolve({
json: () => mockJsonPromise,
});
global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
const func = jest.fn();
const wrapper = shallow(<AddTagPopup/>);
wrapper.setProps({
onHide: () => {
func()
}
});
wrapper.setState({
items: ["test1", "test2", "test3"],
selection: {
name: "test1",
id: 42
}
});
// first call of fetch is getting of available tags
expect(global.fetch).toHaveBeenCalledTimes(1);
wrapper.find('ModalFooter').find('button').simulate('click');
// now called 2 times
expect(global.fetch).toHaveBeenCalledTimes(2);
process.nextTick(() => {
//callback to close window should have called
expect(func).toHaveBeenCalledTimes(1);
global.fetch.mockClear();
done();
});
});
});