1
0
mirror of https://gitlab.nic.cz/turris/reforis/foris-js.git synced 2024-07-02 20:30:27 +00:00

Merge branch 'dev' into 'new-channel-bandwidth'

# Conflicts:
#   src/common/WiFiSettings/WiFiForm.js
This commit is contained in:
Aleksandr Gumroian 2020-07-17 16:24:43 +02:00
commit e7f9fbca96
4 changed files with 112 additions and 16 deletions

View File

@ -38,12 +38,12 @@ WiFiForm.defaultProps = {
export default function WiFiForm({ export default function WiFiForm({
formData, formErrors, setFormValue, hasGuestNetwork, disabled, formData, formErrors, setFormValue, hasGuestNetwork, disabled,
}) { }) {
return formData.devices.map((device, idx) => ( return formData.devices.map((device, index) => (
<DeviceForm <DeviceForm
key={device.id} key={device.id}
formData={device} formData={device}
deviceNumber={idx} deviceIndex={index}
formErrors={(formErrors || [])[idx]} formErrors={(formErrors || [])[index]}
setFormValue={setFormValue} setFormValue={setFormValue}
hasGuestNetwork={hasGuestNetwork} hasGuestNetwork={hasGuestNetwork}
disabled={disabled} disabled={disabled}
@ -66,7 +66,7 @@ DeviceForm.propTypes = {
formErrors: PropTypes.object.isRequired, formErrors: PropTypes.object.isRequired,
setFormValue: PropTypes.func.isRequired, setFormValue: PropTypes.func.isRequired,
hasGuestNetwork: PropTypes.bool, hasGuestNetwork: PropTypes.bool,
deviceNumber: PropTypes.number, deviceIndex: PropTypes.number,
}; };
DeviceForm.defaultProps = { DeviceForm.defaultProps = {
@ -75,7 +75,7 @@ DeviceForm.defaultProps = {
}; };
function DeviceForm({ function DeviceForm({
formData, formErrors, setFormValue, hasGuestNetwork, deviceNumber, ...props formData, formErrors, setFormValue, hasGuestNetwork, deviceIndex, ...props
}) { }) {
const deviceID = formData.id; const deviceID = formData.id;
return ( return (
@ -86,7 +86,7 @@ function DeviceForm({
checked={formData.enabled} checked={formData.enabled}
onChange={setFormValue( onChange={setFormValue(
(value) => ({ devices: { [deviceNumber]: { enabled: { $set: value } } } }), (value) => ({ devices: { [deviceIndex]: { enabled: { $set: value } } } }),
)} )}
{...props} {...props}
@ -102,7 +102,7 @@ function DeviceForm({
onChange={setFormValue( onChange={setFormValue(
(value) => ({ (value) => ({
devices: { devices: {
[deviceNumber]: { [deviceIndex]: {
SSID: { $set: value }, SSID: { $set: value },
}, },
}, },
@ -129,7 +129,7 @@ function DeviceForm({
onChange={setFormValue( onChange={setFormValue(
(value) => ( (value) => (
{ devices: { [deviceNumber]: { password: { $set: value } } } } { devices: { [deviceIndex]: { password: { $set: value } } } }
), ),
)} )}
@ -143,7 +143,7 @@ function DeviceForm({
onChange={setFormValue( onChange={setFormValue(
(value) => ( (value) => (
{ devices: { [deviceNumber]: { hidden: { $set: value } } } } { devices: { [deviceIndex]: { hidden: { $set: value } } } }
), ),
)} )}
@ -160,7 +160,7 @@ function DeviceForm({
onChange={setFormValue( onChange={setFormValue(
(value) => ({ (value) => ({
devices: { devices: {
[deviceNumber]: { [deviceIndex]: {
hwmode: { $set: value }, hwmode: { $set: value },
channel: { $set: "0" }, channel: { $set: "0" },
}, },
@ -179,7 +179,7 @@ function DeviceForm({
onChange={setFormValue( onChange={setFormValue(
(value) => ( (value) => (
{ devices: { [deviceNumber]: { htmode: { $set: value } } } } { devices: { [deviceIndex]: { htmode: { $set: value } } } }
), ),
)} )}
@ -193,7 +193,7 @@ function DeviceForm({
onChange={setFormValue( onChange={setFormValue(
(value) => ( (value) => (
{ devices: { [deviceNumber]: { channel: { $set: value } } } } { devices: { [deviceIndex]: { channel: { $set: value } } } }
), ),
)} )}
@ -202,7 +202,7 @@ function DeviceForm({
{hasGuestNetwork && ( {hasGuestNetwork && (
<WifiGuestForm <WifiGuestForm
formData={{ id: deviceNumber, ...formData.guest_wifi }} formData={{ id: deviceIndex, ...formData.guest_wifi }}
formErrors={formErrors.guest_wifi || {}} formErrors={formErrors.guest_wifi || {}}
setFormValue={setFormValue} setFormValue={setFormValue}

View File

@ -64,7 +64,7 @@ function prepDataToSubmit(formData) {
return formData; return formData;
} }
function validator(formData) { export function validator(formData) {
const formErrors = formData.devices.map( const formErrors = formData.devices.map(
(device) => { (device) => {
if (!device.enabled) return {}; if (!device.enabled) return {};

View File

@ -13,8 +13,8 @@ import { fireEvent, render, wait } from "customTestRender";
import { WebSockets } from "webSockets/WebSockets"; import { WebSockets } from "webSockets/WebSockets";
import { mockJSONError } from "testUtils/network"; import { mockJSONError } from "testUtils/network";
import { wifiSettingsFixture } from "./__fixtures__/wifiSettings"; import { wifiSettingsFixture, oneDevice, twoDevices, threeDevices } from "./__fixtures__/wifiSettings";
import { WiFiSettings } from "../WiFiSettings"; import { WiFiSettings, validator } from "../WiFiSettings";
describe("<WiFiSettings/>", () => { describe("<WiFiSettings/>", () => {
let firstRender; let firstRender;
@ -159,4 +159,18 @@ describe("<WiFiSettings/>", () => {
}; };
expect(mockAxios.post).toHaveBeenCalledWith(endpoint, data, expect.anything()); 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);
});
}); });

View File

@ -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};