Skip to content

Commit

Permalink
test(keyOf): add keyOf function tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hamilton committed Jul 2, 2019
1 parent 5a00ac1 commit cdd196f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/__tests__/keyOf.spec.ts
Original file line number Diff line number Diff line change
@@ -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'));
});
});

0 comments on commit cdd196f

Please # to comment.