Skip to content

Commit

Permalink
test(slice): add slice function tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hamilton committed Jul 4, 2019
1 parent 68abe16 commit 6108878
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/__tests__/slice.spec.ts
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'));
});
});

0 comments on commit 6108878

Please # to comment.