correct load of categorypage on tag click

improved failing tests
This commit is contained in:
Lukas Heiligenbrunner 2020-06-24 21:47:22 +02:00
parent e640b36ce4
commit 753ea99693
8 changed files with 64 additions and 116 deletions

View File

@ -1,13 +1,11 @@
import React from "react"; import React from "react";
import "./Preview.css"; import "./Preview.css";
import Player from "../../pages/Player/Player"; import Player from "../../pages/Player/Player";
import VideoContainer from "../VideoContainer/VideoContainer";
import {Spinner} from "react-bootstrap"; import {Spinner} from "react-bootstrap";
class Preview extends React.Component { class Preview extends React.Component {
constructor(props, context) { constructor(props, context) {
super(props, context); super(props, context);
this.props = props;
this.state = { this.state = {
previewpicture: null, previewpicture: null,
@ -68,37 +66,6 @@ class Preview extends React.Component {
} }
export class TagPreview extends React.Component { export class TagPreview extends React.Component {
constructor(props, context) {
super(props, context);
this.props = props;
}
fetchVideoData(tag) {
console.log(tag);
const updateRequest = new FormData();
updateRequest.append('action', 'getMovies');
updateRequest.append('tag', tag);
console.log("fetching data");
// fetch all videos available
fetch('/api/videoload.php', {method: 'POST', body: updateRequest})
.then((response) => response.json()
.then((result) => {
console.log(result);
this.props.categorybinding(
<VideoContainer
data={result}
viewbinding={this.props.viewbinding}/>, tag
);
}))
.catch(() => {
console.log("no connection to backend");
});
}
render() { render() {
return ( return (
<div className='videopreview tagpreview' onClick={() => this.itemClick()}> <div className='videopreview tagpreview' onClick={() => this.itemClick()}>
@ -110,7 +77,7 @@ export class TagPreview extends React.Component {
} }
itemClick() { itemClick() {
this.fetchVideoData(this.props.name); this.props.categorybinding(this.props.name);
} }
} }

View File

@ -78,14 +78,7 @@ describe('<TagPreview/>', function () {
}); });
it('click event triggered', done => { it('click event triggered', function () {
const mockSuccessResponse = {};
const mockJsonPromise = Promise.resolve(mockSuccessResponse);
const mockFetchPromise = Promise.resolve({
json: () => mockJsonPromise,
});
global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
const func = jest.fn(); const func = jest.fn();
const wrapper = shallow(<TagPreview/>); const wrapper = shallow(<TagPreview/>);
@ -96,19 +89,11 @@ describe('<TagPreview/>', function () {
}); });
// first call of fetch is getting of available tags // first call of fetch is getting of available tags
expect(global.fetch).toHaveBeenCalledTimes(0); expect(func).toHaveBeenCalledTimes(0);
wrapper.find('.videopreview').simulate('click'); wrapper.find('.videopreview').simulate('click');
// now called 1 times // now called 1 times
expect(global.fetch).toHaveBeenCalledTimes(1); expect(func).toHaveBeenCalledTimes(1);
process.nextTick(() => {
//callback to close window should have called
expect(func).toHaveBeenCalledTimes(1);
global.fetch.mockClear();
done();
});
}); });
}); });

View File

@ -1,15 +1,9 @@
import React from "react"; import React from "react";
import "./Tag.css" import "./Tag.css"
import VideoContainer from "../VideoContainer/VideoContainer"; import CategoryPage from "../../pages/CategoryPage/CategoryPage";
class Tag extends React.Component { class Tag extends React.Component {
constructor(props, context) {
super(props, context);
this.props = props;
}
render() { render() {
return ( return (
<button className='tagbtn' onClick={() => this.TagClick()} <button className='tagbtn' onClick={() => this.TagClick()}
@ -20,23 +14,10 @@ class Tag extends React.Component {
TagClick() { TagClick() {
const tag = this.props.children.toString().toLowerCase(); const tag = this.props.children.toString().toLowerCase();
const updateRequest = new FormData(); this.props.viewbinding.changeRootElement(
updateRequest.append('action', 'getMovies'); <CategoryPage
updateRequest.append('tag', tag); category={tag}
viewbinding={this.props.viewbinding}/>);
// fetch all videos available
fetch('/api/videoload.php', {method: 'POST', body: updateRequest})
.then((response) => response.json()
.then((result) => {
this.props.contentbinding(
<VideoContainer
data={result}
viewbinding={this.props.viewbinding}/>, tag
);
}))
.catch(() => {
console.log("no connection to backend");
});
} }
} }

View File

@ -23,21 +23,20 @@ describe('<Tag/>', function () {
expect(wrapper.children().text()).toBe("test"); expect(wrapper.children().text()).toBe("test");
}); });
it('click event triggered and setvideo callback called', done => { it('click event triggered and setvideo callback called', function () {
global.fetch = prepareFetchApi({}); global.fetch = prepareFetchApi({});
const func = jest.fn(); const func = jest.fn();
const elem = {
changeRootElement: () => func()
};
const wrapper = shallow(<Tag const wrapper = shallow(<Tag
contentbinding={func}>test</Tag>); viewbinding={elem}>test</Tag>);
expect(func).toBeCalledTimes(0);
wrapper.simulate("click"); wrapper.simulate("click");
process.nextTick(() => { expect(func).toBeCalledTimes(1);
// state to be set correctly with response
expect(func).toBeCalledTimes(1);
global.fetch.mockClear();
done();
});
}); });
}); });

View File

@ -5,13 +5,12 @@ import Tag from "../../elements/Tag/Tag";
import {TagPreview} from "../../elements/Preview/Preview"; import {TagPreview} from "../../elements/Preview/Preview";
import NewTagPopup from "../../elements/NewTagPopup/NewTagPopup"; import NewTagPopup from "../../elements/NewTagPopup/NewTagPopup";
import PageTitle from "../../elements/PageTitle/PageTitle"; import PageTitle from "../../elements/PageTitle/PageTitle";
import VideoContainer from "../../elements/VideoContainer/VideoContainer";
class CategoryPage extends React.Component { class CategoryPage extends React.Component {
constructor(props, context) { constructor(props, context) {
super(props, context); super(props, context);
this.props = props;
this.state = { this.state = {
loadedtags: [], loadedtags: [],
selected: null selected: null
@ -19,7 +18,12 @@ class CategoryPage extends React.Component {
} }
componentDidMount() { componentDidMount() {
this.loadTags(); // check if predefined category is set
if (this.props.category) {
this.fetchVideoData(this.props.category);
} else {
this.loadTags();
}
} }
render() { render() {
@ -31,10 +35,10 @@ class CategoryPage extends React.Component {
<SideBar> <SideBar>
<div className='sidebartitle'>Default Tags:</div> <div className='sidebartitle'>Default Tags:</div>
<Tag viewbinding={this.props.viewbinding} contentbinding={this.setPage}>All</Tag> <Tag viewbinding={this.props.viewbinding} contentbinding={this.loadTag}>All</Tag>
<Tag viewbinding={this.props.viewbinding} contentbinding={this.setPage}>FullHd</Tag> <Tag viewbinding={this.props.viewbinding} contentbinding={this.loadTag}>FullHd</Tag>
<Tag viewbinding={this.props.viewbinding} contentbinding={this.setPage}>LowQuality</Tag> <Tag viewbinding={this.props.viewbinding} contentbinding={this.loadTag}>LowQuality</Tag>
<Tag viewbinding={this.props.viewbinding} contentbinding={this.setPage}>HD</Tag> <Tag viewbinding={this.props.viewbinding} contentbinding={this.loadTag}>HD</Tag>
<hr/> <hr/>
<button data-testid='btnaddtag' className='btn btn-success' onClick={() => { <button data-testid='btnaddtag' className='btn btn-success' onClick={() => {
this.setState({popupvisible: true}) this.setState({popupvisible: true})
@ -51,12 +55,12 @@ class CategoryPage extends React.Component {
name={m.tag_name} name={m.tag_name}
tag_id={m.tag_id} tag_id={m.tag_id}
viewbinding={this.props.viewbinding} viewbinding={this.props.viewbinding}
categorybinding={this.setPage}/> categorybinding={this.loadTag}/>
)) : )) :
"loading"} "loading"}
</div>) : </div>) :
<> <>
{this.selectionelements} {this.selectionelements ? this.selectionelements : null}
<button data-testid='backbtn' className="btn btn-success" <button data-testid='backbtn' className="btn btn-success"
onClick={this.loadCategoryPageDefault}>Back onClick={this.loadCategoryPageDefault}>Back
</button> </button>
@ -76,12 +80,35 @@ class CategoryPage extends React.Component {
); );
} }
setPage = (element, tagname) => { loadTag = (tagname) => {
this.selectionelements = element; // this.selectionelements = element;
this.setState({selected: null}); // todo save this change trigger better // this.setState({selected: null}); // todo save this change trigger better
this.setState({selected: tagname}); this.fetchVideoData(tagname);
}; };
fetchVideoData(tag) {
console.log(tag);
const updateRequest = new FormData();
updateRequest.append('action', 'getMovies');
updateRequest.append('tag', tag);
console.log("fetching data");
// fetch all videos available
fetch('/api/videoload.php', {method: 'POST', body: updateRequest})
.then((response) => response.json()
.then((result) => {
this.selectionelements =
<VideoContainer
data={result}
viewbinding={this.props.viewbinding}/>;
this.setState({selected: tag});
}))
.catch(() => {
console.log("no connection to backend");
});
}
loadCategoryPageDefault = () => { loadCategoryPageDefault = () => {
this.setState({selected: null}); this.setState({selected: null});
}; };

View File

@ -1,4 +1,4 @@
import {shallow, mount} from "enzyme"; import {shallow} from "enzyme";
import React from "react"; import React from "react";
import HomePage from "./HomePage"; import HomePage from "./HomePage";
import VideoContainer from "../../elements/VideoContainer/VideoContainer"; import VideoContainer from "../../elements/VideoContainer/VideoContainer";
@ -22,19 +22,6 @@ describe('<HomePage/>', function () {
wrapper.unmount(); wrapper.unmount();
}); });
it('ckeck default tag click events', function () {
// todo mount bad here maybe delete this test
const wrapper = mount(<HomePage/>);
global.fetch = prepareFetchApi({});
expect(global.fetch).toBeCalledTimes(0);
// click every tag button
wrapper.find("Tag").map((i) => {
i.simulate("click");
});
expect(global.fetch).toBeCalledTimes(4);
});
it('test data insertion', function () { it('test data insertion', function () {
const wrapper = shallow(<HomePage/>); const wrapper = shallow(<HomePage/>);

View File

@ -21,8 +21,6 @@ class Player extends React.Component {
tags: [], tags: [],
popupvisible: false popupvisible: false
}; };
this.props = props;
} }
options = { options = {
@ -66,7 +64,9 @@ class Player extends React.Component {
<hr/> <hr/>
<div className='sidebartitle'>Tags:</div> <div className='sidebartitle'>Tags:</div>
{this.state.tags.map((m) => ( {this.state.tags.map((m) => (
<Tag key={m.tag_name}>{m.tag_name}</Tag> <Tag
key={m.tag_name}
viewbinding={this.props.viewbinding}>{m.tag_name}</Tag>
))} ))}
</SideBar> </SideBar>

View File

@ -29,7 +29,9 @@ class RandomPage extends React.Component {
<SideBar> <SideBar>
<div className='sidebartitle'>Visible Tags:</div> <div className='sidebartitle'>Visible Tags:</div>
{this.state.tags.map((m) => ( {this.state.tags.map((m) => (
<Tag>{m.tag_name}</Tag> <Tag
key={m.tag_name}
viewbinding={this.props.viewbinding}>{m.tag_name}</Tag>
))} ))}
</SideBar> </SideBar>