Skip to content

Commit

Permalink
Merge pull request opendatahub-io#2883 from christianvogt/enums
Browse files Browse the repository at this point in the history
update enum utils for string and mixed enums
  • Loading branch information
openshift-merge-bot[bot] authored Jun 6, 2024
2 parents f7c9979 + 3222d81 commit 78b9cd9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 18 deletions.
90 changes: 74 additions & 16 deletions frontend/src/utilities/__tests__/utils.spec.ts
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);
});
});
6 changes: 4 additions & 2 deletions frontend/src/utilities/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ export const isEnumMember = <T extends object>(
e: T,
): member is T[keyof T] => {
if (member != null) {
const values = Object.values(e);
return values.slice(values.length / 2).includes(member);
return Object.entries(e)
.filter(([key]) => Number.isNaN(Number(key)))
.map(([, value]) => value)
.includes(member);
}
return false;
};

0 comments on commit 78b9cd9

Please sign in to comment.