forked from red-hat-data-services/odh-dashboard
-
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.
Merge pull request opendatahub-io#2883 from christianvogt/enums
update enum utils for string and mixed enums
- Loading branch information
Showing
2 changed files
with
78 additions
and
18 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 |
---|---|---|
@@ -1,31 +1,89 @@ | ||
import { asEnumMember, isEnumMember } from '~/utilities/utils'; | ||
|
||
enum Test { | ||
first = '1st', | ||
second = '2nd', | ||
} | ||
|
||
enum TestMixed { | ||
first = 1, | ||
second = '2nd', | ||
third = '3rd', | ||
} | ||
|
||
enum TestNumeric { | ||
first, | ||
second, | ||
} | ||
|
||
describe('asEnumMember', () => { | ||
enum Test { | ||
first = 1, | ||
second = 2, | ||
} | ||
it('should return enum member', () => { | ||
expect(asEnumMember(1, Test)).toBe(Test.first); | ||
expect(asEnumMember(2, Test)).toBe(Test.second); | ||
expect(asEnumMember(3, Test)).toBe(null); | ||
it('should return enum member for string enums', () => { | ||
expect(asEnumMember('1st', Test)).toBe(Test.first); | ||
expect(asEnumMember('2nd', Test)).toBe(Test.second); | ||
expect(asEnumMember(1, Test)).toBe(null); | ||
expect(asEnumMember('first', Test)).toBe(null); | ||
expect(asEnumMember('second', Test)).toBe(null); | ||
expect(asEnumMember('unknown', Test)).toBe(null); | ||
expect(asEnumMember(null, Test)).toBe(null); | ||
}); | ||
|
||
it('should return enum member for mixed enums', () => { | ||
expect(asEnumMember(1, TestMixed)).toBe(TestMixed.first); | ||
expect(asEnumMember('2nd', TestMixed)).toBe(TestMixed.second); | ||
expect(asEnumMember('3rd', TestMixed)).toBe(TestMixed.third); | ||
expect(asEnumMember('1', TestMixed)).toBe(null); | ||
expect(asEnumMember(2, TestMixed)).toBe(null); | ||
expect(asEnumMember('first', TestMixed)).toBe(null); | ||
expect(asEnumMember('second', TestMixed)).toBe(null); | ||
expect(asEnumMember('third', TestMixed)).toBe(null); | ||
expect(asEnumMember('unknown', TestMixed)).toBe(null); | ||
expect(asEnumMember(null, TestMixed)).toBe(null); | ||
}); | ||
|
||
it('should return enum member for numeric enums', () => { | ||
expect(asEnumMember(0, TestNumeric)).toBe(TestNumeric.first); | ||
expect(asEnumMember(1, TestNumeric)).toBe(TestNumeric.second); | ||
expect(asEnumMember('1', TestNumeric)).toBe(null); | ||
expect(asEnumMember(2, TestNumeric)).toBe(null); | ||
expect(asEnumMember('first', TestNumeric)).toBe(null); | ||
expect(asEnumMember('second', TestNumeric)).toBe(null); | ||
expect(asEnumMember('unknown', TestNumeric)).toBe(null); | ||
expect(asEnumMember(null, TestNumeric)).toBe(null); | ||
}); | ||
}); | ||
|
||
describe('isEnumMember', () => { | ||
enum Test { | ||
first = 1, | ||
second = 2, | ||
} | ||
it('should identify enum member', () => { | ||
expect(isEnumMember(1, Test)).toBe(true); | ||
expect(isEnumMember(2, Test)).toBe(true); | ||
expect(isEnumMember(3, Test)).toBe(false); | ||
it('should return enum member for string enums', () => { | ||
expect(isEnumMember('1st', Test)).toBe(true); | ||
expect(isEnumMember('2nd', Test)).toBe(true); | ||
expect(isEnumMember(1, Test)).toBe(false); | ||
expect(isEnumMember('first', Test)).toBe(false); | ||
expect(isEnumMember('second', Test)).toBe(false); | ||
expect(isEnumMember('unknown', Test)).toBe(false); | ||
expect(isEnumMember(null, Test)).toBe(false); | ||
}); | ||
|
||
it('should identify enum member for mixed enums', () => { | ||
expect(isEnumMember(1, TestMixed)).toBe(true); | ||
expect(isEnumMember('2nd', TestMixed)).toBe(true); | ||
expect(isEnumMember('3rd', TestMixed)).toBe(true); | ||
expect(isEnumMember('1', TestMixed)).toBe(false); | ||
expect(isEnumMember(2, TestMixed)).toBe(false); | ||
expect(isEnumMember('first', TestMixed)).toBe(false); | ||
expect(isEnumMember('second', TestMixed)).toBe(false); | ||
expect(isEnumMember('third', TestMixed)).toBe(false); | ||
expect(isEnumMember('unknown', TestMixed)).toBe(false); | ||
expect(isEnumMember(null, TestMixed)).toBe(false); | ||
}); | ||
|
||
it('should identify enum member for numeric enums', () => { | ||
expect(isEnumMember(0, TestNumeric)).toBe(true); | ||
expect(isEnumMember(1, TestNumeric)).toBe(true); | ||
expect(isEnumMember('1', TestNumeric)).toBe(false); | ||
expect(isEnumMember(2, TestNumeric)).toBe(false); | ||
expect(isEnumMember('first', TestNumeric)).toBe(false); | ||
expect(isEnumMember('second', TestNumeric)).toBe(false); | ||
expect(isEnumMember('unknown', TestNumeric)).toBe(false); | ||
expect(isEnumMember(null, TestNumeric)).toBe(false); | ||
}); | ||
}); |
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