Skip to content

Commit 7b755c8

Browse files
committed
testing up and running for callbacks
1 parent b4314ef commit 7b755c8

3 files changed

Lines changed: 109 additions & 19 deletions

File tree

.eslintrc.json

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
{
2-
"extends": "airbnb-base",
3-
"plugins": [
4-
"import"
5-
],
2+
"extends": "airbnb-base",
3+
"plugins": ["import"],
4+
"consistent-return": 0,
5+
"rules": {
6+
"no-param-reassign": 0,
7+
"max-len": 0,
8+
"no-plusplus": 0,
9+
"linebreak-style": 0,
610
"consistent-return": 0,
7-
"rules": {
8-
"no-param-reassign": 0,
9-
"max-len": 0,
10-
"no-plusplus": 0,
11-
"linebreak-style": 0,
12-
"consistent-return": 0,
13-
"no-useless-return": 0,
14-
"no-return-assign": 0,
15-
"arrow-body-style": 0,
16-
"no-unused-vars": 0,
17-
"no-useless-constructor": 0,
18-
"import/no-unresolved": 0
19-
}
11+
"no-useless-return": 0,
12+
"no-return-assign": 0,
13+
"arrow-body-style": 0,
14+
"no-unused-vars": 0,
15+
"no-useless-constructor": 0,
16+
"import/no-unresolved": 0
17+
}
2018
}

src/callbacks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const contains = (item, list, cb) => {
2424
// contains checks if an item is present inside of the given array/list.
2525
// Pass true to the callback if it is, otherwise pass false.
2626
};
27-
27+
/* STRETCH PROBLEM */
2828
const removeDuplicates = (array, cb) => {
2929
// removeDuplicates removes all duplicate values from the given array.
3030
// Pass the duplicate free array to the callback function.

tests/callbacks.test.js

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,104 @@ describe('callback functions', () => {
1111
];
1212

1313
describe('firstItem', () => {
14-
it('should pass the first item in the list to callBack', () => {
14+
it('should pass the first item in the list to the callBack', () => {
1515
let itemToTest;
1616
callBackMethods.firstItem(listOfFood, (first) => {
1717
itemToTest = first;
1818
});
1919
expect(itemToTest).toBe(listOfFood[0]);
2020
});
21+
it('should call the callback with the expected input', () => {
22+
const mock = jest.fn();
23+
callBackMethods.firstItem(listOfFood, mock);
24+
expect(mock).toBeCalledWith('cheetoes');
25+
});
26+
});
27+
28+
describe('getLength', () => {
29+
it('should pass back the length of the array to the callback', () => {
30+
let len = 0;
31+
callBackMethods.getLength(listOfFood, (length) => {
32+
len = length;
33+
});
34+
expect(len).toBe(5);
35+
});
36+
it('should call the callback with the expected input', () => {
37+
const mock = jest.fn();
38+
callBackMethods.getLength(listOfFood, mock);
39+
expect(mock).toBeCalledWith(5);
40+
});
41+
});
42+
43+
describe('last', () => {
44+
it('should pass back the last item in the array to the callback', () => {
45+
let item;
46+
callBackMethods.last(listOfFood, (lastItem) => {
47+
item = lastItem;
48+
});
49+
expect(item).toBe('bananas');
50+
});
51+
it('should call the given callback with the expected input', () => {
52+
const mock = jest.fn();
53+
callBackMethods.last(listOfFood, mock);
54+
expect(mock).toBeCalledWith('bananas');
55+
});
56+
});
57+
58+
describe('sumNums', () => {
59+
it('should pass back the result of the added nums to the callback', () => {
60+
let result = 0;
61+
callBackMethods.sumNums(10, 20, (res) => {
62+
result = res;
63+
});
64+
expect(result).toBe(30);
65+
});
66+
it('should call the given callback with the expected input', () => {
67+
const mock = jest.fn();
68+
callBackMethods.sumNums(11, 22, mock);
69+
expect(mock).toBeCalledWith(33);
70+
});
71+
});
72+
73+
describe('multiplyNums', () => {
74+
it('should pass back the result of the multiplied nums to the callback', () => {
75+
let result = 0;
76+
callBackMethods.multiplyNums(10, 20, (res) => {
77+
result = res;
78+
});
79+
expect(result).toBe(200);
80+
});
81+
it('should call the given callback with the expected input', () => {
82+
const mock = jest.fn();
83+
callBackMethods.multiplyNums(5, 11, mock);
84+
expect(mock).toBeCalledWith(55);
85+
});
86+
});
87+
88+
describe('contains', () => {
89+
it('should pass back true if item is in array', () => {
90+
let flag;
91+
callBackMethods.contains('bananas', listOfFood, (truf) => {
92+
flag = truf;
93+
});
94+
expect(flag).toBe(true);
95+
});
96+
it('should pass back false if item is NOT in array', () => {
97+
let flag;
98+
callBackMethods.contains('boat', listOfFood, (truf) => {
99+
flag = truf;
100+
});
101+
expect(flag).toBe(false);
102+
});
103+
});
104+
105+
describe('removeDuplicates', () => {
106+
it('should remove any duplicate items from the array', () => {
107+
let arrToTest = [];
108+
callBackMethods.removeDuplicates(listOfFood, (arrayFreeOfDups) => {
109+
arrToTest = arrayFreeOfDups;
110+
});
111+
expect(arrToTest.length).toBe(4);
112+
});
21113
});
22114
});

0 commit comments

Comments
 (0)