From 67e4abe4d164bf6deed962095b1f616ec0f8c33d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Sa=C5=A1ek?= Date: Thu, 2 Jul 2020 15:32:46 +0200 Subject: [PATCH 1/2] Add test suites for a Wi-Fi form submission --- src/common/WiFiSettings/WiFiSettings.js | 2 +- .../__tests__/WiFiSettings.test.js | 18 +++- .../__tests__/__fixtures__/wifiSettings.js | 82 +++++++++++++++++++ 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/src/common/WiFiSettings/WiFiSettings.js b/src/common/WiFiSettings/WiFiSettings.js index 1d20c9c..9f7fa15 100644 --- a/src/common/WiFiSettings/WiFiSettings.js +++ b/src/common/WiFiSettings/WiFiSettings.js @@ -64,7 +64,7 @@ function prepDataToSubmit(formData) { return formData; } -function validator(formData) { +export function validator(formData) { const formErrors = formData.devices.map( (device) => { if (!device.enabled) return {}; diff --git a/src/common/WiFiSettings/__tests__/WiFiSettings.test.js b/src/common/WiFiSettings/__tests__/WiFiSettings.test.js index 1622fba..2f4d0eb 100644 --- a/src/common/WiFiSettings/__tests__/WiFiSettings.test.js +++ b/src/common/WiFiSettings/__tests__/WiFiSettings.test.js @@ -13,8 +13,8 @@ import { fireEvent, render, wait } from "customTestRender"; import { WebSockets } from "webSockets/WebSockets"; import { mockJSONError } from "testUtils/network"; -import { wifiSettingsFixture } from "./__fixtures__/wifiSettings"; -import { WiFiSettings } from "../WiFiSettings"; +import { wifiSettingsFixture, oneDevice, twoDevices, threeDevices } from "./__fixtures__/wifiSettings"; +import { WiFiSettings, validator } from "../WiFiSettings"; describe("", () => { let firstRender; @@ -159,4 +159,18 @@ describe("", () => { }; expect(mockAxios.post).toHaveBeenCalledWith(endpoint, data, expect.anything()); }); + + it("Validator function using regex for one device", () => { + expect(validator(oneDevice)).toEqual(null); + }); + + it("Validator function using regex for two devices", () => { + const twoDevicesFormErrors = [{SSID: "SSID can't be empty"}, {}]; + expect(validator(twoDevices)).toEqual(twoDevicesFormErrors); + }); + + it("Validator function using regex for three devices", () => { + const threeDevicesFormErrors = [{}, {}, {password: "Password must contain at least 8 symbols"}]; + expect(validator(threeDevices)).toEqual(threeDevicesFormErrors); + }); }); diff --git a/src/common/WiFiSettings/__tests__/__fixtures__/wifiSettings.js b/src/common/WiFiSettings/__tests__/__fixtures__/wifiSettings.js index 71c8eb2..4ee0091 100644 --- a/src/common/WiFiSettings/__tests__/__fixtures__/wifiSettings.js +++ b/src/common/WiFiSettings/__tests__/__fixtures__/wifiSettings.js @@ -316,3 +316,85 @@ export function wifiSettingsFixture() { ], }; } +const oneDevice = { + devices: [ + { + SSID: "Turris1", + channel: 60, + enabled: true, + guest_wifi: { enabled: false }, + hidden: false, + htmode: "HT40", + hwmode: "11a", + id: 0, + password: "TestPass" + } + ] +}; + +const twoDevices = { + devices: [ + { + SSID: "", + channel: 60, + enabled: true, + guest_wifi: { enabled: false }, + hidden: false, + htmode: "HT40", + hwmode: "11a", + id: 0, + password: "TestPass" + }, + { + SSID: "Turris2", + channel: 60, + enabled: true, + guest_wifi: { enabled: false }, + hidden: false, + htmode: "HT40", + hwmode: "11a", + id: 0, + password: "TestPass" + } + ] +}; + +const threeDevices = { + devices: [ + { + SSID: "Turris1", + channel: 60, + enabled: true, + guest_wifi: { enabled: false }, + hidden: false, + htmode: "HT40", + hwmode: "11a", + id: 0, + password: "TestPass" + }, + { + SSID: "Turris2", + channel: 60, + enabled: false, + guest_wifi: { enabled: false }, + hidden: false, + htmode: "HT40", + hwmode: "11a", + id: 0, + password: "TestPass" + }, + { + SSID: "Turris3", + channel: 60, + enabled: true, + guest_wifi: { enabled: false }, + hidden: false, + htmode: "HT40", + hwmode: "11a", + id: 0, + password: "" + } + ] +}; + +export {oneDevice, twoDevices, threeDevices}; \ No newline at end of file From cea8aa0c12f516f1b257088e4563803f4f84eb4d Mon Sep 17 00:00:00 2001 From: Aleksandr Gumroian Date: Fri, 17 Jul 2020 12:15:40 +0200 Subject: [PATCH 2/2] Fix a Wi-Fi Form bug with additional Wi-Fi modules --- src/common/WiFiSettings/WiFiForm.js | 32 ++++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/common/WiFiSettings/WiFiForm.js b/src/common/WiFiSettings/WiFiForm.js index 34aa2f7..e0d203a 100644 --- a/src/common/WiFiSettings/WiFiForm.js +++ b/src/common/WiFiSettings/WiFiForm.js @@ -31,18 +31,19 @@ WiFiForm.propTypes = { WiFiForm.defaultProps = { formData: { devices: [] }, - setFormValue: () => {}, + setFormValue: () => { }, hasGuestNetwork: true, }; export default function WiFiForm({ formData, formErrors, setFormValue, hasGuestNetwork, disabled, }) { - return formData.devices.map((device) => ( + return formData.devices.map((device, index) => ( ({ devices: { [deviceID]: { enabled: { $set: value } } } }), + (value) => ({ devices: { [deviceIndex]: { enabled: { $set: value } } } }), )} {...props} @@ -98,7 +100,13 @@ function DeviceForm({ error={formErrors.SSID || null} required onChange={setFormValue( - (value) => ({ devices: { [deviceID]: { SSID: { $set: value } } } }), + (value) => ({ + devices: { + [deviceIndex]: { + SSID: { $set: value }, + }, + }, + }), )} {...props} @@ -121,7 +129,7 @@ function DeviceForm({ onChange={setFormValue( (value) => ( - { devices: { [deviceID]: { password: { $set: value } } } } + { devices: { [deviceIndex]: { password: { $set: value } } } } ), )} @@ -135,7 +143,7 @@ function DeviceForm({ onChange={setFormValue( (value) => ( - { devices: { [deviceID]: { hidden: { $set: value } } } } + { devices: { [deviceIndex]: { hidden: { $set: value } } } } ), )} @@ -152,7 +160,7 @@ function DeviceForm({ onChange={setFormValue( (value) => ({ devices: { - [deviceID]: { + [deviceIndex]: { hwmode: { $set: value }, channel: { $set: "0" }, }, @@ -171,7 +179,7 @@ function DeviceForm({ onChange={setFormValue( (value) => ( - { devices: { [deviceID]: { htmode: { $set: value } } } } + { devices: { [deviceIndex]: { htmode: { $set: value } } } } ), )} @@ -185,7 +193,7 @@ function DeviceForm({ onChange={setFormValue( (value) => ( - { devices: { [deviceID]: { channel: { $set: value } } } } + { devices: { [deviceIndex]: { channel: { $set: value } } } } ), )} @@ -194,7 +202,7 @@ function DeviceForm({ {hasGuestNetwork && (