forked from trezor/trezor-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(utils): add test for isArrayMember
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { isArrayMember } from '../src/isArrayMember'; | ||
|
||
describe(isArrayMember.name, () => { | ||
it('determines if value is contained in an array', () => { | ||
const arr = ['a', 'b', 'c'] as const; | ||
expect(isArrayMember('a' as string, arr)).toBe(true); | ||
expect(isArrayMember('d' as any, arr)).toBe(false); | ||
expect(isArrayMember(undefined as any, arr)).toBe(false); | ||
}); | ||
|
||
it('correctly narrows down value type (typescript only)', () => { | ||
type Variant = 'a' | 'b' | 'c' | 'd'; | ||
const skippedVariants = ['a', 'b'] satisfies Variant[]; | ||
const variant = 'a' as Variant; | ||
|
||
// the unit test is trivial here, but type-check verifies that the type narrowing works as supposed to | ||
if (isArrayMember(variant, skippedVariants)) { | ||
expect(variant === 'a').toBe(true); | ||
// @ts-expect-error TS2367: variant was narrowed down to 'a' | 'b', so it has no overlap with 'c' | ||
expect(variant === 'c').toBe(false); | ||
} else { | ||
expect(variant === 'c').toBe(false); | ||
// @ts-expect-error TS2367: variant was narrowed down to 'c' | 'd', so it has no overlap with 'a' | ||
expect(variant === 'a').toBe(true); | ||
} | ||
}); | ||
}); |