From cdd196fd4ff8a56e34399e29750636faff921dbe Mon Sep 17 00:00:00 2001 From: Sean Hamilton Date: Tue, 2 Jul 2019 16:19:42 +0100 Subject: [PATCH] test(keyOf): add keyOf function tests --- src/__tests__/keyOf.spec.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/__tests__/keyOf.spec.ts diff --git a/src/__tests__/keyOf.spec.ts b/src/__tests__/keyOf.spec.ts new file mode 100644 index 00000000..e69645fd --- /dev/null +++ b/src/__tests__/keyOf.spec.ts @@ -0,0 +1,35 @@ +import keyOf from '../keyOf'; + +describe('keyOf', () => { + test('should return the key of the first value matched', () => { + const obj = { + a: 1, + b: 2, + c: 3, + }; + + expect(keyOf(obj, 1)).toBe('a'); + expect(keyOf(obj, 2)).toBe('b'); + expect(keyOf(obj, 3)).toBe('c'); + }); + + test('should return undefined if the value is not matched', () => { + const obj = { + a: 1, + }; + + expect(keyOf(obj, 2)).toBeUndefined(); + }); + + test('should throw TypeError for invalid arguments', () => { + const invalidObj: unknown = 'testing'; + const invalidFollow: unknown = 'testing'; + + expect(() => keyOf(invalidObj as OObject, 'test')) + .toThrow(new TypeError('Expected Object, got string testing')); + expect(() => keyOf({}, 'test', { + follow: invalidFollow as boolean, + })) + .toThrow(new TypeError('Expected Boolean, got string testing')); + }); +});