From 76d10ba4317a86dc9b67282e718c24ddf11fef65 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sun, 12 Apr 2020 17:49:29 +0200 Subject: [PATCH] Fixed tests for redux --- README.md | 1 - smart-hut/src/App.test.js | 6 ++++- .../src/components/dashboard/DevicePanel.js | 4 +++- .../components/dashboard/devices/Device.js | 22 ++++++++++--------- .../components/dashboard/devices/Dimmer.js | 12 +++++----- .../src/components/dashboard/devices/Light.js | 8 +++---- .../components/dashboard/devices/SmartPlug.js | 14 +++++------- .../components/dashboard/devices/Switch.js | 13 +++++------ smart-hut/src/endpoint.js | 3 +-- smart-hut/src/remote.js | 9 ++++---- smart-hut/src/store.js | 13 ++++++----- smart-hut/src/views/Forgot-pass-reset.js | 7 ++++-- smart-hut/src/views/Forgot-password.js | 4 +++- smart-hut/src/views/Signup.js | 7 +++--- 14 files changed, 64 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 5aac65c..89ce48d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ # frontend - diff --git a/smart-hut/src/App.test.js b/smart-hut/src/App.test.js index 18413f8..135fe7a 100644 --- a/smart-hut/src/App.test.js +++ b/smart-hut/src/App.test.js @@ -3,12 +3,16 @@ import { render } from "@testing-library/react"; import { Router } from "react-router"; import { createMemoryHistory } from "history"; import App from "./App"; +import { Provider } from "react-redux"; +import smartHutStore from "./store"; test("redirects to homepage", () => { const history = createMemoryHistory(); render( - + + + ); expect(history.location.pathname).toBe("/"); diff --git a/smart-hut/src/components/dashboard/DevicePanel.js b/smart-hut/src/components/dashboard/DevicePanel.js index 28a9906..5e0fb17 100644 --- a/smart-hut/src/components/dashboard/DevicePanel.js +++ b/smart-hut/src/components/dashboard/DevicePanel.js @@ -48,7 +48,9 @@ const mapStateToProps = (state, _) => ({ if (state.active.activeRoom === -1) { return Object.values(state.devices); } else { - const deviceArray = [...state.rooms[state.active.activeRoom].devices].sort(); + const deviceArray = [ + ...state.rooms[state.active.activeRoom].devices, + ].sort(); return deviceArray.map((id) => state.devices[id]); } }, diff --git a/smart-hut/src/components/dashboard/devices/Device.js b/smart-hut/src/components/dashboard/devices/Device.js index ef202ac..6ad0ac4 100644 --- a/smart-hut/src/components/dashboard/devices/Device.js +++ b/smart-hut/src/components/dashboard/devices/Device.js @@ -56,19 +56,21 @@ class Device extends React.Component { return ( - - {this.renderDeviceComponent()} - - -
{this.props.device.name}
- {this.props.device.kind === "smartPlug" ? ( - ) : null} diff --git a/smart-hut/src/components/dashboard/devices/Dimmer.js b/smart-hut/src/components/dashboard/devices/Dimmer.js index d86b18e..2d9c105 100644 --- a/smart-hut/src/components/dashboard/devices/Dimmer.js +++ b/smart-hut/src/components/dashboard/devices/Dimmer.js @@ -65,7 +65,7 @@ export class KnobDimmerComponent extends Component { this.state = { intensity: this.props.device.intensity || 0, - timeout: null + timeout: null, }; this.saveIntensity = this.saveIntensity.bind(this); @@ -74,20 +74,20 @@ export class KnobDimmerComponent extends Component { setIntensity(intensity) { intensity *= 100; - + if (this.state.timeout) { clearTimeout(this.state.timeout); } - this.setState({ + this.setState({ intensity, timeout: setTimeout(() => { this.saveIntensity(); this.setState({ intensity: this.state.intensity, - timeout: null + timeout: null, }); - }, 100) + }, 100), }); } @@ -96,7 +96,7 @@ export class KnobDimmerComponent extends Component { this.props .knobDimmerDimTo(this.props.id, val) .catch((err) => console.error("knob dimmer set intensity error", err)); - }; + } render() { return ( diff --git a/smart-hut/src/components/dashboard/devices/Light.js b/smart-hut/src/components/dashboard/devices/Light.js index 8fa5327..9143ec3 100644 --- a/smart-hut/src/components/dashboard/devices/Light.js +++ b/smart-hut/src/components/dashboard/devices/Light.js @@ -65,20 +65,20 @@ class Light extends Component { setIntensity(intensity) { intensity *= 100; - + if (this.state.timeout) { clearTimeout(this.state.timeout); } - this.setState({ + this.setState({ intensity, timeout: setTimeout(() => { this.saveIntensity(); this.setState({ intensity: this.state.intensity, - timeout: null + timeout: null, }); - }, 100) + }, 100), }); } diff --git a/smart-hut/src/components/dashboard/devices/SmartPlug.js b/smart-hut/src/components/dashboard/devices/SmartPlug.js index f736d91..a180c28 100644 --- a/smart-hut/src/components/dashboard/devices/SmartPlug.js +++ b/smart-hut/src/components/dashboard/devices/SmartPlug.js @@ -5,10 +5,7 @@ The user can reset this value. **/ import React, { Component } from "react"; -import { - BottomPanel, - StyledDiv -} from "./styleComponents"; +import { BottomPanel, StyledDiv } from "./styleComponents"; import { Image } from "semantic-ui-react"; import { energyConsumedStyle, @@ -36,8 +33,9 @@ class SmartPlug extends Component { onClickDevice = () => { const on = !this.turnedOn; - this.props.saveDevice({ ...this.props.device, on }) - .catch((err) => console.error('smart plug update error', err)); + this.props + .saveDevice({ ...this.props.device, on }) + .catch((err) => console.error("smart plug update error", err)); }; getIcon = () => { @@ -48,9 +46,7 @@ class SmartPlug extends Component { return ( - - Smart Plug - + Smart Plug { const newOn = !this.turnedOn; const type = newOn ? "ON" : "OFF"; - this.props.switchOperate(this.props.id, type) - .catch(err => console.error('switch operate failed', err)) + this.props + .switchOperate(this.props.id, type) + .catch((err) => console.error("switch operate failed", err)); }; render() { return ( - - Switch - + Switch - - {this.turnedOn ? "ON" : "OFF"} - + {this.turnedOn ? "ON" : "OFF"} ); diff --git a/smart-hut/src/endpoint.js b/smart-hut/src/endpoint.js index 4891f1e..6dbc3b0 100644 --- a/smart-hut/src/endpoint.js +++ b/smart-hut/src/endpoint.js @@ -14,7 +14,6 @@ export function socketURL(token) { const protocol = isSecure ? "wss:" : "ws:"; const port = httpURL.port || (isSecure ? 443 : 80); const url = `${protocol}//${httpURL.hostname}:${port}/sensor-socket?token=${token}`; - console.log('socket url: ', url); + console.log("socket url: ", url); return url; } - \ No newline at end of file diff --git a/smart-hut/src/remote.js b/smart-hut/src/remote.js index c4814e9..3100417 100644 --- a/smart-hut/src/remote.js +++ b/smart-hut/src/remote.js @@ -4,7 +4,6 @@ import axios from "axios"; import { endpointURL, socketURL } from "./endpoint"; import { connect, disconnect } from "@giantmachines/redux-websocket"; - /** * An object returned by promise rejections in remoteservice * @typedef {Error} RemoteError @@ -170,8 +169,8 @@ export const RemoteService = { return (dispatch) => { return Endpoint.login(usernameOrEmail, password) .then((token) => { - dispatch(actions.loginSuccess(token)) - dispatch(connect(socketURL(token))) + dispatch(actions.loginSuccess(token)); + dispatch(connect(socketURL(token))); }) .catch((err) => { console.warn("login error", err); @@ -190,8 +189,8 @@ export const RemoteService = { logout: () => { return (dispatch) => Endpoint.logout().then(() => { - dispatch(disconnect()) - dispatch(actions.logout()) + dispatch(disconnect()); + dispatch(actions.logout()); }); }, diff --git a/smart-hut/src/store.js b/smart-hut/src/store.js index 2aa78fe..7b04a89 100644 --- a/smart-hut/src/store.js +++ b/smart-hut/src/store.js @@ -237,14 +237,14 @@ function reducer(previousState, action) { newState = reducer(previousState, { type: "DEVICES_UPDATE", partial: true, - devices + devices, }); break; default: console.warn(`Action type ${action.type} unknown`, action); return previousState; } - + return newState; } @@ -284,10 +284,11 @@ function createSmartHutStore() { initialState.login.token = null; } - const store = createStore(reducer, initialState, - compose( - applyMiddleware(thunk), - applyMiddleware(reduxWebSocket()))); + const store = createStore( + reducer, + initialState, + compose(applyMiddleware(thunk), applyMiddleware(reduxWebSocket())) + ); if (initialState.login.loggedIn) { store.dispatch(connect(socketURL(token))); } diff --git a/smart-hut/src/views/Forgot-pass-reset.js b/smart-hut/src/views/Forgot-pass-reset.js index 19116d6..24ed9e6 100644 --- a/smart-hut/src/views/Forgot-pass-reset.js +++ b/smart-hut/src/views/Forgot-pass-reset.js @@ -43,8 +43,11 @@ export default class ChangePass extends Component { Forms.submitResetPassword(this.props.query.token, this.state.password) .then(() => this.setState({ success: true })) - .catch((err) => this.setState({ error: - { state: true, message: err.messages.join(' - ') }})); + .catch((err) => + this.setState({ + error: { state: true, message: err.messages.join(" - ") }, + }) + ); }; render() { diff --git a/smart-hut/src/views/Forgot-password.js b/smart-hut/src/views/Forgot-password.js index 415d3af..511a881 100644 --- a/smart-hut/src/views/Forgot-password.js +++ b/smart-hut/src/views/Forgot-password.js @@ -35,7 +35,9 @@ export default class ForgotPass extends Component { Forms.submitInitResetPassword(this.state.user) .then(() => this.setState({ success: true })) - .catch((err) => this.setState({ error: { state: true, message: err.messages }})); + .catch((err) => + this.setState({ error: { state: true, message: err.messages } }) + ); }; render() { diff --git a/smart-hut/src/views/Signup.js b/smart-hut/src/views/Signup.js index 924bd77..5ad8af5 100644 --- a/smart-hut/src/views/Signup.js +++ b/smart-hut/src/views/Signup.js @@ -34,10 +34,11 @@ export default class Signup extends Component { username: this.state.username, }; - Forms - .submitRegistration(params) + Forms.submitRegistration(params) .then(() => this.setState({ success: true })) - .catch((err) => this.setState({ error: { state: true, message: err.messages }})); + .catch((err) => + this.setState({ error: { state: true, message: err.messages } }) + ); }; onChangeHandler = (event) => {