-
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.
test(filter): add filter function tests
- Loading branch information
Sean Hamilton
committed
Jul 1, 2019
1 parent
cc26f32
commit a2effcc
Showing
1 changed file
with
72 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import filter from '../filter'; | ||
|
||
describe('filter', () => { | ||
test('should only return key/values which the callback evaluates as true', () => { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
c: 'testing', | ||
}; | ||
|
||
const resultA = filter(obj, (k, v) => typeof v === 'number'); | ||
const resultB = filter(obj, (k, v) => typeof v === 'string'); | ||
|
||
expect(Object.keys(resultA)).toHaveLength(2); | ||
expect(Object.keys(resultB)).toHaveLength(1); | ||
}); | ||
|
||
test('should evaluate all key/values', () => { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
}; | ||
const cb = jest.fn(() => true); | ||
|
||
filter(obj, cb); | ||
|
||
expect(cb.mock.calls).toHaveLength(2); | ||
}); | ||
|
||
test('should follow deep objects if follow is true', () => { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
c: { | ||
d: 3, | ||
e: 'testing', | ||
}, | ||
}; | ||
|
||
const cbA: FilterCallback = (k, v) => typeof v === 'number'; | ||
const cbB: FilterCallback = (k, v) => typeof v === 'string'; | ||
|
||
const resultA = filter(obj, cbA, { | ||
follow: false, | ||
}); | ||
const resultB = filter(obj, cbA, { | ||
follow: true, | ||
}); | ||
const resultC = filter(obj, cbB, { | ||
follow: true, | ||
}); | ||
|
||
expect(resultA.a + resultA.b).toBe(3); | ||
expect(resultB.a + resultB.b + resultB.c.d).toBe(6); | ||
expect(resultC.c.e).toBe('testing'); | ||
}); | ||
|
||
test('should throw TypeError for invalid arguments', () => { | ||
const invalidObj: unknown = 'testing'; | ||
const invalidCallback: unknown = 'testing'; | ||
const invalidFollow: unknown = 'testing'; | ||
|
||
expect(() => filter(invalidObj as OObject, jest.fn())) | ||
.toThrow(new TypeError('Expected Object, got string testing')); | ||
expect(() => filter({}, invalidCallback as EveryCallback)) | ||
.toThrow(new TypeError('Expected Function, got string testing')); | ||
expect(() => filter({}, jest.fn(), { | ||
follow: invalidFollow as boolean, | ||
})) | ||
.toThrow(new TypeError('Expected Boolean, got string testing')); | ||
}); | ||
}); |