@@ -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