From 753ea99693bb46285cfa6be11a06852c925ee40e Mon Sep 17 00:00:00 2001 From: Lukas Heiligenbrunner Date: Wed, 24 Jun 2020 21:47:22 +0200 Subject: [PATCH] correct load of categorypage on tag click improved failing tests --- src/elements/Preview/Preview.js | 35 +---------------- src/elements/Preview/Previw.test.js | 21 ++-------- src/elements/Tag/Tag.js | 29 +++----------- src/elements/Tag/Tag.test.js | 17 ++++----- src/pages/CategoryPage/CategoryPage.js | 53 +++++++++++++++++++------- src/pages/HomePage/HomePage.test.js | 15 +------- src/pages/Player/Player.js | 6 +-- src/pages/RandomPage/RandomPage.js | 4 +- 8 files changed, 64 insertions(+), 116 deletions(-) diff --git a/src/elements/Preview/Preview.js b/src/elements/Preview/Preview.js index ea016db..c3a6810 100644 --- a/src/elements/Preview/Preview.js +++ b/src/elements/Preview/Preview.js @@ -1,13 +1,11 @@ import React from "react"; import "./Preview.css"; import Player from "../../pages/Player/Player"; -import VideoContainer from "../VideoContainer/VideoContainer"; import {Spinner} from "react-bootstrap"; class Preview extends React.Component { constructor(props, context) { super(props, context); - this.props = props; this.state = { previewpicture: null, @@ -68,37 +66,6 @@ class Preview 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( - , tag - ); - })) - .catch(() => { - console.log("no connection to backend"); - }); - } - - render() { return (
this.itemClick()}> @@ -110,7 +77,7 @@ export class TagPreview extends React.Component { } itemClick() { - this.fetchVideoData(this.props.name); + this.props.categorybinding(this.props.name); } } diff --git a/src/elements/Preview/Previw.test.js b/src/elements/Preview/Previw.test.js index 1395a65..e1f68ed 100644 --- a/src/elements/Preview/Previw.test.js +++ b/src/elements/Preview/Previw.test.js @@ -78,14 +78,7 @@ describe('', function () { }); - it('click event triggered', done => { - const mockSuccessResponse = {}; - const mockJsonPromise = Promise.resolve(mockSuccessResponse); - const mockFetchPromise = Promise.resolve({ - json: () => mockJsonPromise, - }); - global.fetch = jest.fn().mockImplementation(() => mockFetchPromise); - + it('click event triggered', function () { const func = jest.fn(); const wrapper = shallow(); @@ -96,19 +89,11 @@ describe('', function () { }); // first call of fetch is getting of available tags - expect(global.fetch).toHaveBeenCalledTimes(0); + expect(func).toHaveBeenCalledTimes(0); wrapper.find('.videopreview').simulate('click'); // now called 1 times - expect(global.fetch).toHaveBeenCalledTimes(1); - - process.nextTick(() => { - //callback to close window should have called - expect(func).toHaveBeenCalledTimes(1); - - global.fetch.mockClear(); - done(); - }); + expect(func).toHaveBeenCalledTimes(1); }); }); diff --git a/src/elements/Tag/Tag.js b/src/elements/Tag/Tag.js index b853838..db6a6b5 100644 --- a/src/elements/Tag/Tag.js +++ b/src/elements/Tag/Tag.js @@ -1,15 +1,9 @@ import React from "react"; import "./Tag.css" -import VideoContainer from "../VideoContainer/VideoContainer"; +import CategoryPage from "../../pages/CategoryPage/CategoryPage"; class Tag extends React.Component { - constructor(props, context) { - super(props, context); - - this.props = props; - } - render() { return (
) : <> - {this.selectionelements} + {this.selectionelements ? this.selectionelements : null} @@ -76,12 +80,35 @@ class CategoryPage extends React.Component { ); } - setPage = (element, tagname) => { - this.selectionelements = element; - this.setState({selected: null}); // todo save this change trigger better - this.setState({selected: tagname}); + loadTag = (tagname) => { + // this.selectionelements = element; + // this.setState({selected: null}); // todo save this change trigger better + 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 = + ; + this.setState({selected: tag}); + })) + .catch(() => { + console.log("no connection to backend"); + }); + } + loadCategoryPageDefault = () => { this.setState({selected: null}); }; diff --git a/src/pages/HomePage/HomePage.test.js b/src/pages/HomePage/HomePage.test.js index c963cd9..ebfac6b 100644 --- a/src/pages/HomePage/HomePage.test.js +++ b/src/pages/HomePage/HomePage.test.js @@ -1,4 +1,4 @@ -import {shallow, mount} from "enzyme"; +import {shallow} from "enzyme"; import React from "react"; import HomePage from "./HomePage"; import VideoContainer from "../../elements/VideoContainer/VideoContainer"; @@ -22,19 +22,6 @@ describe('', function () { wrapper.unmount(); }); - it('ckeck default tag click events', function () { - // todo mount bad here maybe delete this test - const wrapper = mount(); - 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 () { const wrapper = shallow(); diff --git a/src/pages/Player/Player.js b/src/pages/Player/Player.js index c8d5b3f..1883160 100644 --- a/src/pages/Player/Player.js +++ b/src/pages/Player/Player.js @@ -21,8 +21,6 @@ class Player extends React.Component { tags: [], popupvisible: false }; - - this.props = props; } options = { @@ -66,7 +64,9 @@ class Player extends React.Component {
Tags:
{this.state.tags.map((m) => ( - {m.tag_name} + {m.tag_name} ))} diff --git a/src/pages/RandomPage/RandomPage.js b/src/pages/RandomPage/RandomPage.js index 27eaa0c..3261aa5 100644 --- a/src/pages/RandomPage/RandomPage.js +++ b/src/pages/RandomPage/RandomPage.js @@ -29,7 +29,9 @@ class RandomPage extends React.Component {
Visible Tags:
{this.state.tags.map((m) => ( - {m.tag_name} + {m.tag_name} ))}