CEC-3086 Allow creating filters without interval (#237)

This commit is contained in:
arpanetus
2022-11-22 04:22:36 +06:00
committed by GitHub
parent 9888ab8a6c
commit 1ecc8943a3
15 changed files with 430 additions and 446 deletions

View File

@@ -1,5 +1,6 @@
import React, { useContext, useState } from "react";
import api from "../../services/CANFiltersAPI";
import { validateCANID, validateFilter } from "../../utils/validationSupplier";
const CANFiltersContext = React.createContext();
@@ -50,7 +51,6 @@ export const CANFiltersProvider = ({ children }) => {
setBusy(true);
validateVIN(vin);
validateID(canID);
validateFilter(filter);
const result = await api.updateFilter(vin, canID, filter, token);
@@ -68,7 +68,7 @@ export const CANFiltersProvider = ({ children }) => {
setBusy(true);
validateVIN(vin);
validateID(canID);
validateCANID(canID);
const result = await api.deleteFilter(vin, canID, token);
if (result.error) {
@@ -104,24 +104,6 @@ const validateVIN = (vin) => {
if (vin == null || vin.length !== 17) {
throw new Error("Invalid VIN");
}
}
const validateID = (can_id) => {
if (can_id == null || can_id === "") {
throw new Error("CAN ID required");
}
}
const validateFilter = (filter) => {
if (filter == null) {
throw new Error("No filter data");
}
validateID(filter.can_id)
if (filter.interval == null) {
throw new Error("Interval required");
}
};
export const useCANFiltersContext = () => useContext(CANFiltersContext);

View File

@@ -118,7 +118,7 @@ describe("CANFiltersContext", () => {
await waitFor(() =>
expect(screen.getByTestId("busy").innerHTML).toEqual("false")
);
checkBaseResults("CAN ID required", "false");
checkBaseResults("Invalid CAN ID", "false");
});
it("addFilter", async () => {
@@ -195,7 +195,7 @@ describe("CANFiltersContext", () => {
await waitFor(() =>
expect(screen.getByTestId("busy").innerHTML).toEqual("false")
);
checkBaseResults("CAN ID required", "false");
checkBaseResults("Invalid CAN ID", "false");
});
it("updateFilter", async () => {
@@ -228,10 +228,6 @@ describe("CANFiltersContext", () => {
data-testid="deleteFilterNull"
onClick={() => deleteF(null)}
/>
<button
data-testid="deleteFilterNonexistent"
onClick={() => deleteF(-1)}
/>
<button data-testid="deleteFilter" onClick={() => deleteF(123)} />
</>
);
@@ -258,15 +254,7 @@ describe("CANFiltersContext", () => {
await waitFor(() =>
expect(screen.getByTestId("busy").innerHTML).toEqual("false")
);
checkBaseResults("CAN ID required", "false");
});
it("deleteFilterNonexistent", async () => {
fireEvent.click(screen.getByTestId("deleteFilterNonexistent"));
await waitFor(() =>
expect(screen.getByTestId("busy").innerHTML).toEqual("false")
);
checkBaseResults("", "false");
checkBaseResults("Invalid CAN ID", "false");
});
it("deleteFilter", async () => {

View File

@@ -1,5 +1,6 @@
import React, { useContext, useState } from "react";
import api from "../../services/fleetsAPI";
import { validateCANID, validateFilter, validateVIN } from "../../utils/validationSupplier";
const FleetContext = React.createContext();
@@ -200,7 +201,6 @@ export const FleetProvider = ({ children }) => {
setBusy(true);
validateFleetName(name);
validateCANID(can_id);
validateFilter(filter);
const result = await api.updateFleetCANFilter(name, can_id, filter, token);
@@ -278,30 +278,6 @@ const validateFleetName = (name) => {
if (name == null || !/^[\w-]+$/.test(name)) {
throw new Error("Invalid name");
}
}
const validateVIN = (vin) => {
if (vin == null || vin.length !== 17) {
throw new Error("Invalid VIN");
}
}
const validateFilter = (filter) => {
if (filter == null) {
throw new Error("No CAN filter data");
}
validateCANID(filter.can_id)
if (filter.interval == null || !/^\d+$/.test(filter.interval)) {
throw new Error("Invalid interval");
}
}
const validateCANID = (can_id) => {
if (can_id == null || !/^\d+(-\d+)?$/.test(can_id)) {
throw new Error("Invalid CAN ID");
}
}
};
export const useFleetContext = () => useContext(FleetContext);