2020-06-12 15:57:30 +00:00
|
|
|
import {shallow, mount} from "enzyme";
|
|
|
|
import React from "react";
|
|
|
|
import CategoryPage from "./CategoryPage";
|
|
|
|
import VideoContainer from "../../elements/VideoContainer/VideoContainer";
|
|
|
|
|
|
|
|
function prepareFetchApi(response) {
|
|
|
|
const mockJsonPromise = Promise.resolve(response);
|
|
|
|
const mockFetchPromise = Promise.resolve({
|
|
|
|
json: () => mockJsonPromise,
|
|
|
|
});
|
|
|
|
return (jest.fn().mockImplementation(() => mockFetchPromise));
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('<CategoryPage/>', function () {
|
|
|
|
it('renders without crashing ', function () {
|
|
|
|
const wrapper = shallow(<CategoryPage/>);
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test tag fetch call', done => {
|
|
|
|
global.fetch = prepareFetchApi(["first", "second"]);
|
|
|
|
|
|
|
|
const wrapper = shallow(<CategoryPage/>);
|
|
|
|
|
|
|
|
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
//callback to close window should have called
|
|
|
|
expect(wrapper.state().loadedtags.length).toBe(2);
|
|
|
|
|
|
|
|
global.fetch.mockClear();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test errored fetch call', done => {
|
|
|
|
global.fetch = prepareFetchApi({});
|
|
|
|
|
|
|
|
let message;
|
|
|
|
global.console.log = jest.fn((m) => {
|
|
|
|
message = m;
|
2020-06-17 19:51:02 +02:00
|
|
|
});
|
2020-06-12 15:57:30 +00:00
|
|
|
|
|
|
|
const wrapper = shallow(<CategoryPage/>);
|
|
|
|
|
|
|
|
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
//callback to close window should have called
|
|
|
|
expect(message).toBe("no connection to backend");
|
|
|
|
|
|
|
|
global.fetch.mockClear();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test new tag popup', function () {
|
|
|
|
const wrapper = mount(<CategoryPage/>);
|
|
|
|
|
|
|
|
expect(wrapper.find("NewTagPopup")).toHaveLength(0);
|
|
|
|
wrapper.find('[data-testid="btnaddtag"]').simulate('click');
|
|
|
|
// newtagpopup should be showing now
|
|
|
|
expect(wrapper.find("NewTagPopup")).toHaveLength(1);
|
|
|
|
|
|
|
|
// click close button in modal
|
|
|
|
wrapper.find(".modal-header").find("button").simulate("click");
|
|
|
|
expect(wrapper.find("NewTagPopup")).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test setpage callback', done => {
|
|
|
|
global.fetch = prepareFetchApi([{}, {}]);
|
|
|
|
|
|
|
|
const wrapper = mount(<CategoryPage/>);
|
|
|
|
|
|
|
|
wrapper.setState({
|
|
|
|
loadedtags: [
|
|
|
|
{
|
|
|
|
tag_name: "testname",
|
|
|
|
tag_id: 42
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
wrapper.find("TagPreview").find("div").first().simulate("click");
|
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
// expect callback to have loaded correct tag
|
|
|
|
expect(wrapper.state().selected).toBe("testname");
|
|
|
|
|
|
|
|
global.fetch.mockClear();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('test back to category view callback', function () {
|
|
|
|
const wrapper = shallow(<CategoryPage/>);
|
|
|
|
|
|
|
|
wrapper.setState({
|
|
|
|
selected: "test"
|
|
|
|
});
|
|
|
|
expect(wrapper.state().selected).not.toBeNull();
|
|
|
|
wrapper.find('[data-testid="backbtn"]').simulate("click");
|
|
|
|
expect(wrapper.state().selected).toBeNull();
|
|
|
|
});
|
2020-06-24 22:47:46 +02:00
|
|
|
|
|
|
|
it('load categorypage with predefined tag', function () {
|
|
|
|
const func = jest.fn();
|
|
|
|
CategoryPage.prototype.fetchVideoData = func;
|
|
|
|
|
|
|
|
const wrapper = shallow(<CategoryPage category="fullhd"/>);
|
|
|
|
|
|
|
|
expect(func).toBeCalledTimes(1);
|
|
|
|
});
|
2020-06-12 15:57:30 +00:00
|
|
|
});
|