use enzyme library for testing updated some tests
new test for newtag component
This commit is contained in:
		@@ -1,28 +1,20 @@
 | 
			
		||||
import React from 'react';
 | 
			
		||||
import { render } from '@testing-library/react';
 | 
			
		||||
import App from './App';
 | 
			
		||||
import ReactDom from "react-dom";
 | 
			
		||||
import {shallow} from 'enzyme'
 | 
			
		||||
 | 
			
		||||
describe('<App/>', function () {
 | 
			
		||||
  it('renders without crashing ', function () {
 | 
			
		||||
    const div = document.createElement("div");
 | 
			
		||||
    ReactDom.render(<App/>,div);
 | 
			
		||||
    ReactDom.unmountComponentAtNode(div);
 | 
			
		||||
    const wrapper = shallow(<App/>);
 | 
			
		||||
    wrapper.unmount();
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('renders title', () => {
 | 
			
		||||
    const { getByText } = render(<App />);
 | 
			
		||||
    const linkElement = getByText(/Home Page/i);
 | 
			
		||||
    expect(linkElement).toBeInTheDocument();
 | 
			
		||||
    const wrapper = shallow(<App/>);
 | 
			
		||||
    expect(wrapper.find('.navbar-brand').text()).toBe('OpenMediaCenter');
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('are navlinks correct', function () {
 | 
			
		||||
    const { getByText } = render(<App />);
 | 
			
		||||
    const randomElement = getByText(/Random Video/i);
 | 
			
		||||
    const categoryElement = getByText(/Categories/i);
 | 
			
		||||
    const settingsElement = getByText(/Settings/i);
 | 
			
		||||
    expect(randomElement).toBeInTheDocument();
 | 
			
		||||
    expect(categoryElement).toBeInTheDocument();
 | 
			
		||||
    expect(settingsElement).toBeInTheDocument();
 | 
			
		||||
    const wrapper = shallow(<App/>);
 | 
			
		||||
    expect(wrapper.find('nav').find('li')).toHaveLength(4);
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
                }));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +1,17 @@
 | 
			
		||||
import React from "react";
 | 
			
		||||
import ReactDom from 'react-dom'
 | 
			
		||||
import SideBar from "./SideBar";
 | 
			
		||||
 | 
			
		||||
import {render} from '@testing-library/react'
 | 
			
		||||
import "@testing-library/jest-dom"
 | 
			
		||||
import {shallow} from "enzyme";
 | 
			
		||||
 | 
			
		||||
describe('<SideBar/>', function () {
 | 
			
		||||
    it('renders without crashing ', function () {
 | 
			
		||||
        const div = document.createElement("div");
 | 
			
		||||
        ReactDom.render(<SideBar/>,div);
 | 
			
		||||
        ReactDom.unmountComponentAtNode(div);
 | 
			
		||||
        const wrapper = shallow(<SideBar/>);
 | 
			
		||||
        wrapper.unmount();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('renders childs correctly', function () {
 | 
			
		||||
        const {getByText} = render(<SideBar>test</SideBar>);
 | 
			
		||||
        const randomElement = getByText(/test/i);
 | 
			
		||||
        expect(randomElement).toBeInTheDocument();
 | 
			
		||||
        const wrapper = shallow(<SideBar>test</SideBar>);
 | 
			
		||||
        expect(wrapper.children().text()).toBe("test");
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,6 @@ import React from "react";
 | 
			
		||||
import "./Tag.css"
 | 
			
		||||
 | 
			
		||||
class Tag extends React.Component {
 | 
			
		||||
 | 
			
		||||
    constructor(props, context) {
 | 
			
		||||
        super(props, context);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,19 +1,17 @@
 | 
			
		||||
import React from "react";
 | 
			
		||||
import ReactDom from 'react-dom'
 | 
			
		||||
import Tag from './Tag'
 | 
			
		||||
 | 
			
		||||
import {render} from '@testing-library/react'
 | 
			
		||||
import "@testing-library/jest-dom"
 | 
			
		||||
import {shallow} from 'enzyme'
 | 
			
		||||
 | 
			
		||||
describe('<Tag/>', function () {
 | 
			
		||||
    it('renders without crashing ', function () {
 | 
			
		||||
        const div = document.createElement("div");
 | 
			
		||||
        ReactDom.render(<Tag/>,div);
 | 
			
		||||
        ReactDom.unmountComponentAtNode(div);
 | 
			
		||||
        const wrapper = shallow(<Tag>test</Tag>);
 | 
			
		||||
        wrapper.unmount();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it('renders childs correctly', function () {
 | 
			
		||||
        const {getByTestId} = render(<Tag>test</Tag>);
 | 
			
		||||
        expect(getByTestId("Test-Tag")).toHaveTextContent("test");
 | 
			
		||||
        const wrapper = shallow(<Tag>test</Tag>);
 | 
			
		||||
        expect(wrapper.children().text()).toBe("test");
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -3,3 +3,8 @@
 | 
			
		||||
// expect(element).toHaveTextContent(/react/i)
 | 
			
		||||
// learn more: https://github.com/testing-library/jest-dom
 | 
			
		||||
import '@testing-library/jest-dom/extend-expect';
 | 
			
		||||
 | 
			
		||||
import { configure } from 'enzyme';
 | 
			
		||||
import Adapter from 'enzyme-adapter-react-16';
 | 
			
		||||
 | 
			
		||||
configure({ adapter: new Adapter() });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user