Files
OpenFrontIO/tests/MessageTypeClasses.test.ts
T
Scott Anderson dccf2b3866 Avoid using as to cast values (#1115)
## Description:

- Use `<argument> is <type>` return type declarations in favor of `as`.
- Use `satisfies` instead of `as`.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

---------

Co-authored-by: Scott Anderson <662325+scottanderson@users.noreply.github.com>
2025-06-10 16:10:49 -07:00

56 lines
1.7 KiB
TypeScript

import { getMessageTypeClasses, severityColors } from "../src/client/Utils";
import { MessageType } from "../src/core/game/Game";
describe("getMessageTypeClasses", () => {
// Spy on console.warn to track when the default case is hit
let consoleSpy: jest.SpyInstance;
beforeEach(() => {
consoleSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
});
afterEach(() => {
consoleSpy.mockRestore();
});
it("should return a valid CSS class for every MessageType", () => {
const messageTypes = Object.values(MessageType).filter(
(value): value is MessageType => typeof value === "number",
);
messageTypes.forEach((messageType) => {
const result = getMessageTypeClasses(messageType);
expect(Object.values(severityColors)).toContain(result);
expect(result).toBeTruthy();
expect(typeof result).toBe("string");
});
});
it("should not trigger console.warn for any MessageType", () => {
const messageTypes = Object.values(MessageType).filter(
(value): value is MessageType => typeof value === "number",
);
messageTypes.forEach((messageType) => {
getMessageTypeClasses(messageType);
});
// No message type should fall through to the default case
expect(consoleSpy).not.toHaveBeenCalled();
});
it("should return white color and warn for unknown message types", () => {
// Cast to MessageType to test the default case
const unknownType = 999 as MessageType;
const result = getMessageTypeClasses(unknownType);
expect(result).toBe(severityColors["white"]);
expect(consoleSpy).toHaveBeenCalledWith(
"Message type 999 has no explicit color",
);
});
});