Tests for getDeviceAccountSettings()

pull/646/head
Rick Carlino 2018-01-26 09:39:41 -06:00
parent 427fd997db
commit ead0bbded7
2 changed files with 24 additions and 18 deletions

View File

@ -227,3 +227,17 @@ describe("maybeFindPlantById()", () => {
expect(result).toBeUndefined();
});
});
describe("getDeviceAccountSettings", () => {
it("crashes if < 1", () => {
pending();
});
it("crashes if > 1", () => {
pending();
});
it("returns exactly one device", () => {
pending();
});
});

View File

@ -30,7 +30,7 @@ import {
TaggedDevice,
TaggedWebAppConfig
} from "./tagged_resources";
import { CowardlyDictionary, betterCompact, sortResourcesById } from "../util";
import { CowardlyDictionary, betterCompact, sortResourcesById, bail } from "../util";
import { isNumber } from "util";
type StringMap = CowardlyDictionary<string>;
@ -451,24 +451,16 @@ export function maybeGetDevice(index: ResourceIndex): TaggedDevice | undefined {
return (dev && dev.kind === "Device") ?
dev : undefined;
}
export function getDeviceAccountSettings(index: ResourceIndex) {
export function getDeviceAccountSettings(index: ResourceIndex): TaggedDevice {
const list = index.byKind.Device;
const uuid = list[0];
const device = index.references[uuid || -1];
if (list.length === 0) {
throw new Error(`Tried to load device before it was loaded.`);
}
if (list.length > 1) {
throw new Error(`PROBLEM: Expected getDeviceAccountSettings() to return
exactly 1 device. Found multiple devices.`);
}
if (device && device.kind === "Device") {
sanityCheck(device);
return device;
} else {
throw new Error("Malformed device resource");
const uuid = list[0] || "_";
const device = index.references[uuid];
switch (list.length) {
case 0: return bail(`Tried to load device before it was loaded.`);
case 1: return (device && device.kind === "Device" && sanityCheck(device))
? device
: bail("Malformed device!");
default: return bail("Found more than 1 device");
}
}