Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions src/lib/util/checkName.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { checkName } from "./checkName";

const warningMessage = (value: string): string =>
"HAP-NodeJS WARNING: The accessory 'displayName' has an invalid 'Name' characteristic ('" + value +
"'). Please ensure the name starts and ends with a letter or number. Only letters, numbers, spaces, apostrophes, " +
"and common punctuation are supported. Avoid emojis or unsupported symbols. This may prevent the accessory from being added " +
"in the Home App or cause unresponsiveness.";

describe("#checkName()", () => {
let consoleWarnSpy: jest.SpyInstance;

Expand All @@ -15,37 +21,69 @@ describe("#checkName()", () => {
checkName("displayName", "Name", "bad name!");

expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' has an invalid 'Name' characteristic ('bad name!'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");
expect(consoleWarnSpy).toHaveBeenCalledWith(warningMessage("bad name!"));
});

test("Accessory Name beginning with !", async () => {
checkName("displayName", "Name", "!bad name");

expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' has an invalid 'Name' characteristic ('!bad name'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");
expect(consoleWarnSpy).toHaveBeenCalledWith(warningMessage("!bad name"));
});

test("Accessory Name containing !", async () => {
checkName("displayName", "Name", "bad ! name");

expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' has an invalid 'Name' characteristic ('bad ! name'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");
expect(consoleWarnSpy).toHaveBeenCalledTimes(0);
});

test("Accessory Name beginning with '", async () => {
checkName("displayName", "Name", " 'bad name");

expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
// eslint-disable-next-line max-len
expect(consoleWarnSpy).toHaveBeenCalledWith("HAP-NodeJS WARNING: The accessory 'displayName' has an invalid 'Name' characteristic (' 'bad name'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.");
expect(consoleWarnSpy).toHaveBeenCalledWith(warningMessage(" 'bad name"));
});

test("Accessory Name containing '", async () => {
checkName("displayName", "Name", "good ' name");

expect(consoleWarnSpy).toHaveBeenCalledTimes(0);
});

test("Accessory Name containing common punctuation", async () => {
[
"TV / Lounge",
"Heating & Cooling",
"Camera: Front Yard",
"Rear (Garage) Door",
"Fan_Light",
"Zone-2",
"Kitchen, Dining",
"Room; Sensor",
].forEach(value => {
checkName("displayName", "Name", value);

expect(consoleWarnSpy).toHaveBeenCalledTimes(0);
consoleWarnSpy.mockClear();
});
});

test("Accessory Name ending with punctuation", async () => {
[
"TV /",
"Heating &",
"Camera:",
"Garage Door)",
"Fan_",
"Zone-",
"Kitchen,",
"Room;",
].forEach(value => {
checkName("displayName", "Name", value);

expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
expect(consoleWarnSpy).toHaveBeenCalledWith(warningMessage(value));
consoleWarnSpy.mockClear();
});
});
});
Loading