|
| 1 | +const { |
| 2 | + getNthElement, |
| 3 | + arrayToCSVString, |
| 4 | + csvStringToArray, |
| 5 | + addToArray, |
| 6 | + addToArray2, |
| 7 | + removeNthElement, |
| 8 | + numbersToStrings, |
| 9 | + uppercaseWordsInArray, |
| 10 | + reverseWordsInArray, |
| 11 | + onlyEven, |
| 12 | + removeNthElement2, |
| 13 | + elementsStartingWithAVowel, |
| 14 | + removeSpaces, |
| 15 | + sumNumbers, |
| 16 | + sortByLastLetter |
| 17 | +} = require('../arrays'); |
| 18 | + |
| 19 | +describe('getNthElement', () => { |
| 20 | + const array = ['cat', 'dog', 'elephant', 'fox']; |
| 21 | + |
| 22 | + xit('returns the element at the given position', () => { |
| 23 | + expect(getNthElement(0, array)).toEqual('cat'); |
| 24 | + expect(getNthElement(2, array)).toEqual('elephant'); |
| 25 | + expect(getNthElement(3, array)).toEqual('fox'); |
| 26 | + }); |
| 27 | + |
| 28 | + xit('if n is greater than the number of elements, it cycles back to the start', () => { |
| 29 | + expect(getNthElement(4, array)).toEqual('cat'); |
| 30 | + expect(getNthElement(5, array)).toEqual('dog'); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe('arrayToCSVString', () => { |
| 35 | + xit('returns the array elements as a comma-seperated string', () => { |
| 36 | + expect(arrayToCSVString(['a', 'b', 'c', 'd'])).toEqual('a,b,c,d'); |
| 37 | + expect(arrayToCSVString([1, 2, 3, 4, 5])).toEqual('1,2,3,4,5'); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe('csvStringToArray', () => { |
| 42 | + xit('converts the csv string as an array', () => { |
| 43 | + expect(csvStringToArray('a,b,c,d')).toEqual(['a', 'b', 'c', 'd']); |
| 44 | + expect(csvStringToArray('1,2,3,4,5')).toEqual(['1', '2', '3', '4', '5']); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('addToArray', () => { |
| 49 | + xit('adds the item to the end of the array', () => { |
| 50 | + const array = []; |
| 51 | + const array2 = [1, 2, 3]; |
| 52 | + |
| 53 | + expect(addToArray('a', array)).toEqual(undefined); |
| 54 | + expect(addToArray(4, array2)).toEqual(undefined); |
| 55 | + |
| 56 | + expect(array).toEqual(['a']); |
| 57 | + expect(array2).toEqual([1, 2, 3, 4]); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe('addToArray2', () => { |
| 62 | + xit('returns a new array with the value appended', () => { |
| 63 | + const array = ['a', 'b', 'c']; |
| 64 | + const array2 = [1, 2, 3]; |
| 65 | + |
| 66 | + expect(addToArray2('d', array)).toEqual(['a', 'b', 'c', 'd']); |
| 67 | + expect(array).toEqual(['a', 'b', 'c']); |
| 68 | + |
| 69 | + expect(addToArray2(4, array2)).toEqual([1, 2, 3, 4]); |
| 70 | + expect(array2).toEqual([1, 2, 3]); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('removeNthElement', () => { |
| 75 | + xit('removes the element at position n', () => { |
| 76 | + const array = ['ant', 'bison', 'cockerel', 'duck', 'elephant']; |
| 77 | + removeNthElement(2, array); |
| 78 | + expect(array).toEqual(['ant', 'bison', 'duck', 'elephant']); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('numbersToStrings', () => { |
| 83 | + xit('converts every number in the array to a string', () => { |
| 84 | + expect(numbersToStrings([1, 2, 3])).toEqual(['1', '2', '3']); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('uppercaseWordsInArray', () => { |
| 89 | + xit('makes every string in the array uppercase', () => { |
| 90 | + expect(uppercaseWordsInArray(['cat', 'mouse', 'banana'])).toEqual([ |
| 91 | + 'CAT', |
| 92 | + 'MOUSE', |
| 93 | + 'BANANA' |
| 94 | + ]); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('reverseWordsInArray', () => { |
| 99 | + xit('reverses every string in an array', () => { |
| 100 | + expect(reverseWordsInArray(['cat', 'Mouse', 'banana'])).toEqual([ |
| 101 | + 'tac', |
| 102 | + 'esuoM', |
| 103 | + 'ananab' |
| 104 | + ]); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('onlyEven', () => { |
| 109 | + xit('filters the array and only returns even numbers', () => { |
| 110 | + expect(onlyEven([1, 2, 3, 4, 5, 6, 7, 8])).toEqual([2, 4, 6, 8]); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('removeNthElement2', () => { |
| 115 | + xit('returns an array with the nth element removed, and does not mutate the original', () => { |
| 116 | + const array = ['bike', 'car', 'train', 'bus']; |
| 117 | + expect(removeNthElement2(2, array)).toEqual(['bike', 'car', 'bus']); |
| 118 | + expect(array).toEqual(['bike', 'car', 'train', 'bus']); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe('elementsStartingWithAVowel', () => { |
| 123 | + xit('returns elements starting with a vowel', () => { |
| 124 | + expect( |
| 125 | + elementsStartingWithAVowel([ |
| 126 | + 'apple', |
| 127 | + 'bapple', |
| 128 | + 'capple', |
| 129 | + 'dapple', |
| 130 | + 'epple', |
| 131 | + 'fepple', |
| 132 | + 'gepple', |
| 133 | + 'hepple', |
| 134 | + 'ipple', |
| 135 | + 'jipple', |
| 136 | + 'kipple', |
| 137 | + 'lipple', |
| 138 | + 'mipple', |
| 139 | + 'nipple', |
| 140 | + 'opple', |
| 141 | + 'popple', |
| 142 | + 'qopple', |
| 143 | + 'ropple', |
| 144 | + 'sopple', |
| 145 | + 'topple', |
| 146 | + 'upple', |
| 147 | + 'vupple', |
| 148 | + 'wupple', |
| 149 | + 'xupple', |
| 150 | + 'yupple', |
| 151 | + 'zupple' |
| 152 | + ]) |
| 153 | + ).toEqual(['apple', 'epple', 'ipple', 'opple', 'upple']); |
| 154 | + }); |
| 155 | + |
| 156 | + xit('is case insensitive', () => { |
| 157 | + expect( |
| 158 | + elementsStartingWithAVowel([ |
| 159 | + 'Apple', |
| 160 | + 'Bapple', |
| 161 | + 'Capple', |
| 162 | + 'Dapple', |
| 163 | + 'Epple', |
| 164 | + 'Fepple', |
| 165 | + 'Gepple', |
| 166 | + 'Hepple', |
| 167 | + 'Ipple', |
| 168 | + 'Jipple', |
| 169 | + 'Kipple', |
| 170 | + 'Lipple', |
| 171 | + 'Mipple', |
| 172 | + 'Nipple', |
| 173 | + 'Opple', |
| 174 | + 'Popple', |
| 175 | + 'Qopple', |
| 176 | + 'Ropple', |
| 177 | + 'Sopple', |
| 178 | + 'Topple', |
| 179 | + 'Upple', |
| 180 | + 'Vupple', |
| 181 | + 'Wupple', |
| 182 | + 'Xupple', |
| 183 | + 'Yupple', |
| 184 | + 'Zupple' |
| 185 | + ]) |
| 186 | + ).toEqual(['Apple', 'Epple', 'Ipple', 'Opple', 'Upple']); |
| 187 | + }); |
| 188 | +}); |
| 189 | + |
| 190 | +describe('removeSpaces', () => { |
| 191 | + xit('returns the string with the space characters removed', () => { |
| 192 | + expect(removeSpaces('this string has spaces')).toEqual( |
| 193 | + 'thisstringhasspaces' |
| 194 | + ); |
| 195 | + expect(removeSpaces(' this one has sneaky spaces ')).toEqual( |
| 196 | + 'thisonehassneakyspaces' |
| 197 | + ); |
| 198 | + }); |
| 199 | +}); |
| 200 | + |
| 201 | +describe('sumNumbers', () => { |
| 202 | + xit('returns the sum of the numbers in the array', () => { |
| 203 | + expect(sumNumbers([1, 3, 5, 6, 2, 8])).toEqual(25); |
| 204 | + }); |
| 205 | +}); |
| 206 | + |
| 207 | +describe('sortByLastLetter', () => { |
| 208 | + xit('sorts the string by the last character', () => { |
| 209 | + expect( |
| 210 | + sortByLastLetter(['Lannister', 'Stark', 'Greyjoy', 'Targaryen']) |
| 211 | + ).toEqual(['Stark', 'Targaryen', 'Lannister', 'Greyjoy']); |
| 212 | + }); |
| 213 | +}); |
0 commit comments