pokerogue/test/test-utils/matchers/to-have-weather.ts
Bertie690 1691951c87
[Test] Added Map key matcher; enforced strong typing on matchers (#6561)
* Added `toHaveKey` matcher + fixed imports

* Broke up the test matchers into multiple smaller interfaces

* Added restricted typing on matchers

Now we can't call `expect(game).toHaveFullHp()`!!!!!

* Updated comment

* Renamed `toEqualArrayUnsorted` into `toEqualUnsorted`

* Moved comment at top of file

* Fix `@module` doc comment

* Remove extra space

* Fix typo

* Fixed key ssue in matchers

* Update to-have-key.ts

* Update test/@types/vitest.d.ts

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>

* Fixed missing braces inside comment

---------

Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2025-09-20 17:24:27 -05:00

63 lines
2.1 KiB
TypeScript

/** biome-ignore-start lint/correctness/noUnusedImports: TSDoc imports */
import type { GameManager } from "#test/test-utils/game-manager";
/** biome-ignore-end lint/correctness/noUnusedImports: TSDoc imports */
import { WeatherType } from "#enums/weather-type";
import { isGameManagerInstance, receivedStr } from "#test/test-utils/test-utils";
import { toTitleCase } from "#utils/strings";
import type { MatcherState, SyncExpectationResult } from "@vitest/expect";
/**
* Matcher that checks if the current {@linkcode WeatherType} is as expected.
* @param received - The object to check. Should be the current {@linkcode GameManager}
* @param expectedWeatherType - The expected {@linkcode WeatherType}
* @returns Whether the matcher passed
*/
export function toHaveWeather(
this: MatcherState,
received: unknown,
expectedWeatherType: WeatherType,
): SyncExpectationResult {
if (!isGameManagerInstance(received)) {
return {
pass: this.isNot,
message: () => `Expected to receive a GameManager, but got ${receivedStr(received)}!`,
};
}
if (!received.scene?.arena) {
return {
pass: this.isNot,
message: () => `Expected GameManager.${received.scene ? "scene.arena" : "scene"} to be defined!`,
};
}
const actual = received.scene.arena.getWeatherType();
const pass = actual === expectedWeatherType;
const actualStr = toWeatherStr(actual);
const expectedStr = toWeatherStr(expectedWeatherType);
return {
pass,
message: () =>
pass
? `Expected the Arena to NOT have ${expectedStr} weather active, but it did!`
: `Expected the Arena to have ${expectedStr} weather active, but got ${actualStr} instead!`,
expected: expectedWeatherType,
actual,
};
}
/**
* Get a human readable representation of the current {@linkcode WeatherType}.
* @param weatherType - The {@linkcode WeatherType} to transform
* @returns A human readable string
*/
function toWeatherStr(weatherType: WeatherType) {
if (weatherType === WeatherType.NONE) {
return "no weather";
}
return toTitleCase(WeatherType[weatherType]);
}