Add support for expect() .toBeLessThan and .toBeGreaterThan (#48086)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/48086

Changelog: [internal]

Just adding a bit of coverage for the `expect` API adding `toBeLessThan` and `toBeGreaterThan`.

Reviewed By: sammy-SC

Differential Revision: D66753268

fbshipit-source-id: 6a26f558f985ccbb5eb0daacecd93759841149e9
This commit is contained in:
Rubén Norte
2024-12-05 05:18:08 -08:00
committed by Facebook GitHub Bot
parent ff934cd249
commit b487e65869
+42
View File
@@ -165,6 +165,27 @@ class Expect {
}
}
toBeGreaterThan(expected: number): void {
if (typeof this.#received !== 'number') {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)} to be a number but it was a ${typeof this.#received}`,
).blameToPreviousFrame();
}
if (typeof expected !== 'number') {
throw new ErrorWithCustomBlame(
`Expected ${String(expected)} to be a number but it was a ${typeof expected}`,
).blameToPreviousFrame();
}
const pass = this.#received > expected;
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to be greater than or equal to ${expected}`,
).blameToPreviousFrame();
}
}
toBeGreaterThanOrEqual(expected: number): void {
if (typeof this.#received !== 'number') {
throw new ErrorWithCustomBlame(
@@ -186,6 +207,27 @@ class Expect {
}
}
toBeLessThan(expected: number): void {
if (typeof this.#received !== 'number') {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)} to be a number but it was a ${typeof this.#received}`,
).blameToPreviousFrame();
}
if (typeof expected !== 'number') {
throw new ErrorWithCustomBlame(
`Expected ${String(expected)} to be a number but it was a ${typeof expected}`,
).blameToPreviousFrame();
}
const pass = this.#received < expected;
if (!this.#isExpectedResult(pass)) {
throw new ErrorWithCustomBlame(
`Expected ${String(this.#received)}${this.#maybeNotLabel()} to be less than or equal to ${expected}`,
).blameToPreviousFrame();
}
}
toBeLessThanOrEqual(expected: number): void {
if (typeof this.#received !== 'number') {
throw new ErrorWithCustomBlame(