Skip to content

Commit 19b4ba1

Browse files
Ryan HamblinRyan Hamblin
authored andcommitted
adding jest spying
1 parent de039d0 commit 19b4ba1

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

tests/arrays.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ describe('arrays', () => {
1717
});
1818
expect(count).toBe(9);
1919
});
20+
it('should call the callback passed to it for each element in array given', () => {
21+
const callBackMockFn = jest.fn(); // this function is going to act as your callback, if it's not called your test will fail.
22+
expect(callBackMockFn.mock.calls.length).toBe(0);
23+
arrayMethods.each([1, 2, 3, 'four'], callBackMockFn);
24+
expect(callBackMockFn.mock.calls.length).toBe(4);
25+
});
2026
});
2127

2228
describe('map', () => {
@@ -30,6 +36,12 @@ describe('arrays', () => {
3036
const mappedArr = arrayMethods.map(arr, n => (n * 2));
3137
expect(mappedArr).toEqual([2, 4, 6]);
3238
});
39+
it('should call the callback passed to it for each element in array given', () => {
40+
const callBackMockFn = jest.fn(); // this function is going to act as your callback, if it's not called your test will fail.
41+
expect(callBackMockFn.mock.calls.length).toBe(0);
42+
arrayMethods.each([1, 2, 3, 'four'], callBackMockFn);
43+
expect(callBackMockFn.mock.calls.length).toBe(4);
44+
});
3345
});
3446

3547
describe('reduce', () => {
@@ -48,8 +60,15 @@ describe('arrays', () => {
4860
const result = arrayMethods.reduce(arr, (memo, num) => (memo + num));
4961
expect(result).toBe('hello!');
5062
});
63+
it('should call the given callback per every item but one in the array and return an expected value', () => {
64+
const callBackMockFn = jest.fn();
65+
callBackMockFn.mockReturnValue(25);
66+
const arr = [1, 2, 3, 4, 5];
67+
const result = arrayMethods.reduce(arr, callBackMockFn);
68+
expect(result).toBe(25);
69+
expect(callBackMockFn.mock.calls.length).toBe(4);
70+
});
5171
});
52-
5372
describe('find', () => {
5473
it('should return the first element that passes the truth test', () => {
5574
const arr = [1, 2, 3, 4, 5];

0 commit comments

Comments
 (0)