-
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(slice): add slice function tests
- Loading branch information
Sean Hamilton
committed
Jul 4, 2019
1 parent
68abe16
commit 6108878
Showing
1 changed file
with
91 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,91 @@ | ||
import slice from '../slice'; | ||
|
||
describe('slice', () => { | ||
test('should return a new object not a reference', () => { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
}; | ||
|
||
const result = slice(obj, 0, 0); | ||
result.a = 2; | ||
|
||
expect(typeof result).toBe('object'); | ||
expect(obj.a).toBe(1); | ||
}); | ||
|
||
test('should only return the object keys within the slice range', () => { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
d: 4, | ||
e: 5, | ||
}; | ||
|
||
const resultA = slice(obj, 0, 1); | ||
const resultB = slice(obj, 2, 4); | ||
|
||
expect(Object.keys(resultA)).toHaveLength(1); | ||
expect(Object.keys(resultA)[0]).toBe('a'); | ||
expect(Object.keys(resultB)).toHaveLength(2); | ||
expect(Object.keys(resultB)[0]).toBe('c'); | ||
expect(Object.keys(resultB)[1]).toBe('d'); | ||
}); | ||
|
||
test('should slice deep objects when follow is true', () => { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
c: { | ||
d: 4, | ||
e: 5, | ||
}, | ||
}; | ||
|
||
const resultA = slice(obj, 0, 1, { | ||
follow: true, | ||
}); | ||
const resultB = slice(obj, 2, 3, { | ||
follow: true, | ||
}); | ||
|
||
expect(Object.keys(resultA)).toHaveLength(1); | ||
expect(Object.keys(resultA)[0]).toBe('a'); | ||
expect(Object.keys(resultB)).toHaveLength(1); | ||
expect(Object.keys(resultB)[0]).toBe('c'); | ||
expect(Object.keys(resultB.c)).toHaveLength(1); | ||
expect(Object.keys(resultB.c)[0]).toBe('d'); | ||
}); | ||
|
||
test('end argument should default to object key length', () => { | ||
const obj = { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}; | ||
|
||
const result = slice(obj, 1); | ||
|
||
expect(Object.keys(result)).toHaveLength(2); | ||
expect(Object.keys(result)[0]).toBe('b'); | ||
expect(Object.keys(result)[1]).toBe('c'); | ||
}); | ||
|
||
test('should throw TypeError for invalid arguments', () => { | ||
const invalidObj: unknown = 'testing'; | ||
const invalidNumber: unknown = 'testing'; | ||
const invalidFollow: unknown = 'testing'; | ||
|
||
expect(() => slice(invalidObj as OObject, 0, 0)) | ||
.toThrow(new TypeError('Expected Object, got string testing')); | ||
expect(() => slice({}, invalidNumber as number, 0)) | ||
.toThrow(new TypeError('Expected Number, got string testing')); | ||
expect(() => slice({}, 0, invalidNumber as number)) | ||
.toThrow(new TypeError('Expected Number, got string testing')); | ||
expect(() => slice({}, 0, 0, { | ||
follow: invalidFollow as boolean, | ||
})) | ||
.toThrow(new TypeError('Expected Boolean, got string testing')); | ||
}); | ||
}); |