From 537d869338e70a8844e26a3ee2d8854da420b3ee Mon Sep 17 00:00:00 2001 From: Lukas Heiligenbrunner Date: Mon, 13 Jul 2020 00:44:16 +0200 Subject: [PATCH] added several tests to generalsettings view --- src/pages/CategoryPage/CategoryPage.test.js | 3 +- src/pages/SettingsPage/GeneralSettings.js | 11 ++-- .../SettingsPage/GeneralSettings.module.css | 2 +- .../SettingsPage/GeneralSettings.test.js | 66 +++++++++++++++++-- src/pages/SettingsPage/SettingsPage.test.js | 6 +- 5 files changed, 72 insertions(+), 16 deletions(-) diff --git a/src/pages/CategoryPage/CategoryPage.test.js b/src/pages/CategoryPage/CategoryPage.test.js index 1821c1e..a96cc9e 100644 --- a/src/pages/CategoryPage/CategoryPage.test.js +++ b/src/pages/CategoryPage/CategoryPage.test.js @@ -1,7 +1,6 @@ -import {shallow, mount} from "enzyme"; +import {mount, shallow} from "enzyme"; import React from "react"; import CategoryPage from "./CategoryPage"; -import VideoContainer from "../../elements/VideoContainer/VideoContainer"; function prepareFetchApi(response) { const mockJsonPromise = Promise.resolve(response); diff --git a/src/pages/SettingsPage/GeneralSettings.js b/src/pages/SettingsPage/GeneralSettings.js index 3f5b067..f1d020b 100644 --- a/src/pages/SettingsPage/GeneralSettings.js +++ b/src/pages/SettingsPage/GeneralSettings.js @@ -1,5 +1,5 @@ import React from "react"; -import {Form, Col, Button} from "react-bootstrap"; +import {Button, Col, Form} from "react-bootstrap"; import style from "./GeneralSettings.module.css" class GeneralSettings extends React.Component { @@ -42,13 +42,13 @@ class GeneralSettings extends React.Component { this.saveSettings(); }}> - + Video Path this.setState({videopath: ee.target.value})}/> - + TV Show Path { @@ -67,14 +68,14 @@ class GeneralSettings extends React.Component { /> {this.state.passwordsupport ? - + Password this.setState({password: e.target.value})}/> : null } - + The name of the Mediacenter this.setState({mediacentername: e.target.value})}/> diff --git a/src/pages/SettingsPage/GeneralSettings.module.css b/src/pages/SettingsPage/GeneralSettings.module.css index b6c8eb3..2f9cb81 100644 --- a/src/pages/SettingsPage/GeneralSettings.module.css +++ b/src/pages/SettingsPage/GeneralSettings.module.css @@ -2,7 +2,7 @@ width: 60%; } -.mediacenternameform{ +.mediacenternameform { margin-top: 25px; width: 40%; } diff --git a/src/pages/SettingsPage/GeneralSettings.test.js b/src/pages/SettingsPage/GeneralSettings.test.js index 27873f7..00ec02b 100644 --- a/src/pages/SettingsPage/GeneralSettings.test.js +++ b/src/pages/SettingsPage/GeneralSettings.test.js @@ -19,10 +19,10 @@ describe('', function () { it('test password hide/show switchbutton', function () { const wrapper = shallow(); - expect(wrapper.find("FormGroup").findWhere(it => it.props().controlId === "passwordfield")).toHaveLength(0); + expect(wrapper.find("[data-testid='passwordfield']")).toHaveLength(0); wrapper.find("FormCheck").findWhere(it => it.props().label === "Enable Password support").simulate("change"); - expect(wrapper.find("FormGroup").findWhere(it => it.props().controlId === "passwordfield")).toHaveLength(1); + expect(wrapper.find("[data-testid='passwordfield']")).toHaveLength(1); }); it('test savesettings', done => { @@ -31,15 +31,71 @@ describe('', function () { global.fetch = prepareFetchApi({success: true}); expect(global.fetch).toBeCalledTimes(0); - wrapper.find("[data-testid='mainformsettings']").simulate("submit"); + const fakeEvent = {preventDefault: () => console.log('preventDefault')}; + wrapper.find("[data-testid='mainformsettings']").simulate("submit", fakeEvent); expect(global.fetch).toBeCalledTimes(1); process.nextTick(() => { - // expect callback to have loaded correct tag - expect(wrapper.state().selected).toBe("testname"); + // todo 2020-07-13: test popup of error success here global.fetch.mockClear(); done(); }); }); + + it('test failing savesettings', done => { + const wrapper = shallow(); + + global.fetch = prepareFetchApi({success: false}); + + expect(global.fetch).toBeCalledTimes(0); + const fakeEvent = {preventDefault: () => console.log('preventDefault')}; + wrapper.find("[data-testid='mainformsettings']").simulate("submit", fakeEvent); + expect(global.fetch).toBeCalledTimes(1); + + process.nextTick(() => { + // todo 2020-07-13: test error popup here! + + global.fetch.mockClear(); + done(); + }); + }); + + it('test videopath change event', function () { + const wrapper = shallow(); + + expect(wrapper.state().videopath).not.toBe("test"); + + const event = {target: {name: "pollName", value: "test"}}; + wrapper.find("[data-testid='videpathform']").find("FormControl").simulate("change", event); + expect(wrapper.state().videopath).toBe("test"); + }); + + it('test tvshowpath change event', function () { + const wrapper = shallow(); + + const event = {target: {name: "pollName", value: "test"}}; + expect(wrapper.state().tvshowpath).not.toBe("test"); + wrapper.find("[data-testid='tvshowpath']").find("FormControl").simulate("change", event); + expect(wrapper.state().tvshowpath).toBe("test"); + }); + + it('test mediacentername-form change event', function () { + const wrapper = shallow(); + + const event = {target: {name: "pollName", value: "test"}}; + expect(wrapper.state().mediacentername).not.toBe("test"); + wrapper.find("[data-testid='nameform']").find("FormControl").simulate("change", event); + expect(wrapper.state().mediacentername).toBe("test"); + }); + + it('test password-form change event', function () { + const wrapper = shallow(); + wrapper.setState({passwordsupport: true}); + + const event = {target: {name: "pollName", value: "test"}}; + expect(wrapper.state().password).not.toBe("test"); + wrapper.find("[data-testid='passwordfield']").find("FormControl").simulate("change", event); + expect(wrapper.state().password).toBe("test"); + }); }); diff --git a/src/pages/SettingsPage/SettingsPage.test.js b/src/pages/SettingsPage/SettingsPage.test.js index 5cac3a6..735cea9 100644 --- a/src/pages/SettingsPage/SettingsPage.test.js +++ b/src/pages/SettingsPage/SettingsPage.test.js @@ -19,15 +19,15 @@ describe('', function () { it('simulate topic clicka', function () { const wrapper = shallow(); - simulateSideBarClick("General",wrapper); + simulateSideBarClick("General", wrapper); expect(wrapper.state().currentpage).toBe("general"); expect(wrapper.find(".SettingsContent").find("GeneralSettings")).toHaveLength(1); - simulateSideBarClick("Movies",wrapper); + simulateSideBarClick("Movies", wrapper); expect(wrapper.state().currentpage).toBe("movies"); expect(wrapper.find(".SettingsContent").find("MovieSettings")).toHaveLength(1); - simulateSideBarClick("TV Shows",wrapper); + simulateSideBarClick("TV Shows", wrapper); expect(wrapper.state().currentpage).toBe("tv"); expect(wrapper.find(".SettingsContent").find("span")).toHaveLength(1); });