diff --git a/frontend/src/utilities/__tests__/utils.spec.ts b/frontend/src/utilities/__tests__/utils.spec.ts index 906c5741ef..04f4135917 100644 --- a/frontend/src/utilities/__tests__/utils.spec.ts +++ b/frontend/src/utilities/__tests__/utils.spec.ts @@ -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); + }); }); diff --git a/frontend/src/utilities/utils.ts b/frontend/src/utilities/utils.ts index 12c4e25c49..2b7def86e2 100644 --- a/frontend/src/utilities/utils.ts +++ b/frontend/src/utilities/utils.ts @@ -169,8 +169,10 @@ export const isEnumMember = ( 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; };