Browse Source

updated tests

pull/671/head
Dan Ditomaso 1 year ago
parent
commit
3c84194c62
  1. 24
      src/components/PageComponents/Messages/MessageInput.test.tsx
  2. 4
      src/components/PageComponents/Messages/MessageInput.tsx
  3. 6
      src/core/hooks/useFavoriteNode.test.ts
  4. 12
      src/core/hooks/useFavoriteNode.ts
  5. 15
      src/core/hooks/useIgnoreNode.ts
  6. 15
      src/i18n/locales/en/ui.json

24
src/components/PageComponents/Messages/MessageInput.test.tsx

@ -86,7 +86,7 @@ describe("MessageInput", () => {
it("should render the input field, byte counter, and send button", () => { it("should render the input field, byte counter, and send button", () => {
renderComponent(); renderComponent();
expect(screen.getByPlaceholderText("Enter Message")).toBeInTheDocument(); expect(screen.getByTestId("message-input-field")).toBeInTheDocument();
expect(screen.getByTestId("byte-counter")).toBeInTheDocument(); expect(screen.getByTestId("byte-counter")).toBeInTheDocument();
expect(screen.getByRole("button")).toBeInTheDocument(); expect(screen.getByRole("button")).toBeInTheDocument();
expect(screen.getByTestId("send-icon")).toBeInTheDocument(); expect(screen.getByTestId("send-icon")).toBeInTheDocument();
@ -100,10 +100,6 @@ describe("MessageInput", () => {
renderComponent(); renderComponent();
const inputElement = screen.getByPlaceholderText(
"Enter Message",
) as HTMLInputElement;
expect(inputElement.value).toBe(initialDraft);
expect(mockGetDraft).toHaveBeenCalledWith(defaultProps.to); expect(mockGetDraft).toHaveBeenCalledWith(defaultProps.to);
const expectedBytes = new Blob([initialDraft]).size; const expectedBytes = new Blob([initialDraft]).size;
expect(screen.getByTestId("byte-counter")).toHaveTextContent( expect(screen.getByTestId("byte-counter")).toHaveTextContent(
@ -113,7 +109,7 @@ describe("MessageInput", () => {
it("should update input value, byte counter, and call setDraft on change within limits", () => { it("should update input value, byte counter, and call setDraft on change within limits", () => {
renderComponent(); renderComponent();
const inputElement = screen.getByPlaceholderText("Enter Message"); const inputElement = screen.getByTestId("message-input-field");
const testMessage = "Hello there!"; const testMessage = "Hello there!";
const expectedBytes = new Blob([testMessage]).size; const expectedBytes = new Blob([testMessage]).size;
@ -130,7 +126,7 @@ describe("MessageInput", () => {
it("should NOT update input value or call setDraft if maxBytes is exceeded", () => { it("should NOT update input value or call setDraft if maxBytes is exceeded", () => {
const smallMaxBytes = 5; const smallMaxBytes = 5;
renderComponent({ maxBytes: smallMaxBytes }); renderComponent({ maxBytes: smallMaxBytes });
const inputElement = screen.getByPlaceholderText("Enter Message"); const inputElement = screen.getByTestId("message-input-field");
const initialValue = "12345"; const initialValue = "12345";
const excessiveValue = "123456"; const excessiveValue = "123456";
@ -150,7 +146,7 @@ describe("MessageInput", () => {
it("should call onSend, clear input, reset byte counter, and call clearDraft on valid submit", async () => { it("should call onSend, clear input, reset byte counter, and call clearDraft on valid submit", async () => {
renderComponent(); renderComponent();
const inputElement = screen.getByPlaceholderText("Enter Message"); const inputElement = screen.getByTestId("message-input-field");
const formElement = screen.getByRole("form"); const formElement = screen.getByRole("form");
const testMessage = "Send this message"; const testMessage = "Send this message";
@ -171,7 +167,7 @@ describe("MessageInput", () => {
it("should trim whitespace before calling onSend", async () => { it("should trim whitespace before calling onSend", async () => {
renderComponent(); renderComponent();
const inputElement = screen.getByPlaceholderText("Enter Message"); const inputElement = screen.getByTestId("message-input-field");
const formElement = screen.getByRole("form"); const formElement = screen.getByRole("form");
const testMessageWithWhitespace = " Trim me! "; const testMessageWithWhitespace = " Trim me! ";
const expectedTrimmedMessage = "Trim me!"; const expectedTrimmedMessage = "Trim me!";
@ -190,7 +186,7 @@ describe("MessageInput", () => {
it("should not call onSend or clearDraft if input is empty on submit", async () => { it("should not call onSend or clearDraft if input is empty on submit", async () => {
renderComponent(); renderComponent();
const inputElement = screen.getByPlaceholderText("Enter Message"); const inputElement = screen.getByTestId("message-input-field");
const formElement = screen.getByRole("form"); const formElement = screen.getByRole("form");
expect((inputElement as HTMLInputElement).value).toBe(""); expect((inputElement as HTMLInputElement).value).toBe("");
@ -236,10 +232,14 @@ describe("MessageInput", () => {
expect(mockGetDraft).toHaveBeenCalledWith(broadcastDest); expect(mockGetDraft).toHaveBeenCalledWith(broadcastDest);
expect( expect(
(screen.getByPlaceholderText("Enter Message") as HTMLInputElement).value, (screen.getByTestId(
"message-input-field",
) as HTMLInputElement).value,
).toBe("Broadcast draft"); ).toBe("Broadcast draft");
const inputElement = screen.getByPlaceholderText("Enter Message"); const inputElement = screen.getByTestId(
"message-input-field",
) as HTMLInputElement;
const formElement = screen.getByRole("form"); const formElement = screen.getByRole("form");
const newMessage = "New broadcast msg"; const newMessage = "New broadcast msg";

4
src/components/PageComponents/Messages/MessageInput.tsx

@ -18,7 +18,7 @@ export const MessageInput = ({
maxBytes, maxBytes,
}: MessageInputProps) => { }: MessageInputProps) => {
const { setDraft, getDraft, clearDraft } = useMessageStore(); const { setDraft, getDraft, clearDraft } = useMessageStore();
const { t } = useTranslation(); const { t } = useTranslation("messages");
const calculateBytes = (text: string) => new Blob([text]).size; const calculateBytes = (text: string) => new Blob([text]).size;
@ -61,7 +61,7 @@ export const MessageInput = ({
autoFocus autoFocus
minLength={1} minLength={1}
name="messageInput" name="messageInput"
placeholder={t("messages.sendMessage.placeholder")} placeholder={t("sendMessage.placeholder")}
autoComplete="off" autoComplete="off"
value={localDraft} value={localDraft}
onChange={handleInputChange} onChange={handleInputChange}

6
src/core/hooks/useFavoriteNode.test.ts

@ -43,7 +43,7 @@ describe("useFavoriteNode hook", () => {
expect(mockUpdateFavorite).toHaveBeenCalledWith(1234, true); expect(mockUpdateFavorite).toHaveBeenCalledWith(1234, true);
expect(mockGetNode).toHaveBeenCalledWith(1234); expect(mockGetNode).toHaveBeenCalledWith(1234);
expect(mockToast).toHaveBeenCalledWith({ expect(mockToast).toHaveBeenCalledWith({
title: "Added Test Node to favorites", title: "Added Test Node to favorites.",
}); });
}); });
@ -57,7 +57,7 @@ describe("useFavoriteNode hook", () => {
expect(mockUpdateFavorite).toHaveBeenCalledWith(1234, false); expect(mockUpdateFavorite).toHaveBeenCalledWith(1234, false);
expect(mockGetNode).toHaveBeenCalledWith(1234); expect(mockGetNode).toHaveBeenCalledWith(1234);
expect(mockToast).toHaveBeenCalledWith({ expect(mockToast).toHaveBeenCalledWith({
title: "Removed Test Node from favorites", title: "Removed Test Node from favorites.",
}); });
}); });
@ -74,7 +74,7 @@ describe("useFavoriteNode hook", () => {
}); });
expect(mockToast).toHaveBeenCalledWith({ expect(mockToast).toHaveBeenCalledWith({
title: "Added node to favorites", title: "Added Node to favorites.",
}); });
}); });

12
src/core/hooks/useFavoriteNode.ts

@ -10,7 +10,7 @@ interface FavoriteNodeOptions {
export function useFavoriteNode() { export function useFavoriteNode() {
const { updateFavorite, getNode } = useDevice(); const { updateFavorite, getNode } = useDevice();
const { t } = useTranslation("ui"); const { t } = useTranslation();
const { toast } = useToast(); const { toast } = useToast();
const updateFavoriteCB = useCallback( const updateFavoriteCB = useCallback(
@ -21,10 +21,14 @@ export function useFavoriteNode() {
updateFavorite(nodeNum, isFavorite); updateFavorite(nodeNum, isFavorite);
toast({ toast({
title: t("toast.favoriteNode", { title: t("toast.favoriteNode.title", {
action: isFavorite ? t("button.added") : t("button.removed"), action: isFavorite
? t("toast.favoriteNode.action.added")
: t("toast.favoriteNode.action.removed"),
nodeName: node?.user?.longName ?? t("node"), nodeName: node?.user?.longName ?? t("node"),
location: isFavorite ? t("to") : t("from"), direction: isFavorite
? t("toast.favoriteNode.action.to")
: t("toast.favoriteNode.action.from"),
}), }),
}); });
}, },

15
src/core/hooks/useIgnoreNode.ts

@ -1,6 +1,7 @@
import { useCallback } from "react"; import { useCallback } from "react";
import { useDevice } from "@core/stores/deviceStore.ts"; import { useDevice } from "@core/stores/deviceStore.ts";
import { useToast } from "@core/hooks/useToast.ts"; import { useToast } from "@core/hooks/useToast.ts";
import { useTranslation } from "react-i18next";
interface IgnoreNodeOptions { interface IgnoreNodeOptions {
nodeNum: number; nodeNum: number;
@ -9,6 +10,8 @@ interface IgnoreNodeOptions {
export function useIgnoreNode() { export function useIgnoreNode() {
const { updateIgnored, getNode } = useDevice(); const { updateIgnored, getNode } = useDevice();
const { t } = useTranslation();
const { toast } = useToast(); const { toast } = useToast();
const updateIgnoredCB = useCallback( const updateIgnoredCB = useCallback(
@ -19,9 +22,15 @@ export function useIgnoreNode() {
updateIgnored(nodeNum, isIgnored); updateIgnored(nodeNum, isIgnored);
toast({ toast({
title: `${isIgnored ? "Added" : "Removed"} ${ title: t("toast.ignoreNode.title", {
node?.user?.longName ?? "node" nodeName: node?.user?.longName ?? "node",
} ${isIgnored ? "to" : "from"} ignore list`, action: isIgnored
? t("toast.ignoreNode.action.added")
: t("toast.ignoreNode.action.removed"),
direction: isIgnored
? t("toast.ignoreNode.action.to")
: t("toast.ignoreNode.action.from"),
}),
}); });
}, },
[updateIgnored, getNode], [updateIgnored, getNode],

15
src/i18n/locales/en/ui.json

@ -68,10 +68,19 @@
"description": "The configuration change {{case}} has been saved." "description": "The configuration change {{case}} has been saved."
}, },
"favoriteNode": { "favoriteNode": {
"title": "Node {{nodeName}} {{action}} {{direction} favorites.", "title": "{{action}} {{nodeName}} {{direction}} favorites.",
"action": { "action": {
"added": "added", "added": "Added",
"removed": "removed", "removed": "Removed",
"to": "to",
"from": "from"
}
},
"ignoreNode": {
"title": "{{action}} {{nodeName}} {{direction}} ignore list",
"action": {
"added": "Added",
"removed": "Removed",
"to": "to", "to": "to",
"from": "from" "from": "from"
} }

Loading…
Cancel
Save