Files
ota-admin-portal/src/components/Controls/SendCommand/sanitize.test.js
2022-09-07 23:21:57 +06:00

150 lines
4.1 KiB
JavaScript

import {emptyCommands, hmol, hmolCommands, onOff, onOffCommands, onOffTemp, precond, sanitize,} from "./sanitize";
const randomValues = [null, undefined, "someString", 33]
describe("Sanitize test", () => {
it("empty commands", () => {
for (const command of emptyCommands) {
const cmd = sanitize({command})
expect(cmd.command).toEqual(command)
expect("data" in cmd).toEqual(false);
expect("start" in cmd).toEqual(false);
expect("end" in cmd).toEqual(false);
}
})
it("on-off commands with proper values", () => {
for (const command of onOffCommands) {
for (const data of [false, true]) {
const cmd = sanitize({command, data})
expect(cmd.data).toEqual(onOff[data])
expect(cmd.command).toEqual(command)
expect("start" in cmd).toEqual(false);
expect("end" in cmd).toEqual(false);
}
}
})
it("on-off commands with dirty values", () => {
for (const command of onOffCommands) {
const cmd = {command}
for (const rVal of randomValues) {
if (rVal !== undefined) {
cmd.data = rVal
}
const res = sanitize(cmd)
expect(res.data).toEqual("off")
expect(res.command).toEqual(command)
expect("start" in res).toEqual(false);
expect("end" in res).toEqual(false);
}
}
})
it("high-mid-low-off with proper values", () => {
for (const command of hmolCommands) {
for (const data of [0,1,2,3]) {
const cmd = sanitize({command, data})
expect(cmd.data).toEqual(hmol[data])
expect(cmd.command).toEqual(command)
expect("start" in cmd).toEqual(false);
expect("end" in cmd).toEqual(false);
}
}
})
it("precondition with proper values", () => {
for (const data of [0,1,2,3]) {
const cmd = sanitize({command:"precondition", data})
expect(cmd.data).toEqual(precond[data])
expect(cmd.command).toEqual("precondition")
expect("start" in cmd).toEqual(false);
expect("end" in cmd).toEqual(false);
}
})
it("precondition with wrong values", () => {
const cmd = {command:"precondition"}
for (const rVal of randomValues) {
if (rVal !== undefined) {
cmd.data = rVal
}
const res = sanitize(cmd)
expect(res.data).toEqual("battery")
expect(res.command).toEqual("precondition")
expect("start" in res).toEqual(false);
expect("end" in res).toEqual(false);
}
})
it("high-mid-low-off with wrong values", () => {
for (const command of hmolCommands) {
const cmd = {command}
for (const rVal of randomValues) {
if (rVal !== undefined) {
cmd.data = rVal
}
const res = sanitize(cmd)
expect(res.data).toEqual("off")
expect(res.command).toEqual(command)
expect("start" in res).toEqual(false);
expect("end" in res).toEqual(false);
}
}
})
it("open trunk", () => {
const cmd = {command: "trunk_open"}
for (let i = 1; i <= 5; i++) {
cmd.data = i
const res = sanitize(cmd)
expect(res.data).toEqual(i.toString())
expect(res.command).toEqual("trunk_open")
expect("start" in res).toEqual(false);
expect("end" in res).toEqual(false);
}
})
it("open trunk with wrong values", () => {
const cmd = {command: "trunk_open"}
for (const rVal of randomValues) {
if (rVal !== undefined) {
cmd.data = rVal
}
const res = sanitize(cmd)
expect(res.data).toEqual("1")
expect(res.command).toEqual("trunk_open")
expect("start" in res).toEqual(false);
expect("end" in res).toEqual(false);
}
})
it("cabin temp with period with proper values", () => {
for (let i = 0; i <= 27; i++) {
const res = sanitize({
command: "temp_cabin",
data: i,
start: new Date(),
end: new Date(),
})
expect(res.command).toEqual("temp_cabin")
expect(res.data).toEqual(onOffTemp[i])
expect(typeof res.start).toEqual("object")
expect(typeof res.end).toEqual("object")
}
})
})