├── .gitignore ├── anagram.js ├── anagram.test.js ├── chunk.js ├── chunk.test.js ├── functions.js ├── functions.test.js ├── package-lock.json ├── package.json ├── reversestring.js └── reversestring.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /anagram.js: -------------------------------------------------------------------------------- 1 | function isAnagram(str1, str2) { 2 | return formatStr(str1) === formatStr(str2); 3 | } 4 | 5 | // Helper function 6 | function formatStr(str) { 7 | return str 8 | .replace(/[^\w]/g, '') 9 | .toLowerCase() 10 | .split('') 11 | .sort() 12 | .join(''); 13 | } 14 | 15 | module.exports = isAnagram; 16 | -------------------------------------------------------------------------------- /anagram.test.js: -------------------------------------------------------------------------------- 1 | const isAnagram = require('./anagram'); 2 | 3 | test('isAnagram function exists', () => { 4 | expect(typeof isAnagram).toEqual('function'); 5 | }); 6 | 7 | test('"cinema" is an anagram of "iceman"', () => { 8 | expect(isAnagram('cinema', 'iceman')).toBeTruthy(); 9 | }); 10 | 11 | test('"Dormitory" is an anagram of "dirty room##"', () => { 12 | expect(isAnagram('Dormitory', 'dirty room##')).toBeTruthy(); 13 | }); 14 | 15 | test('"Hello" is NOT an anagram of "Aloha"', () => { 16 | expect(isAnagram('Hello', 'Aloha')).toBeFalsy(); 17 | }); 18 | -------------------------------------------------------------------------------- /chunk.js: -------------------------------------------------------------------------------- 1 | const chunkArray = (arr, len) => { 2 | // Init chunked arr 3 | const chunkedArr = []; 4 | 5 | // Loop through arr 6 | arr.forEach(val => { 7 | // Get last element 8 | const last = chunkedArr[chunkedArr.length - 1]; 9 | 10 | // Check if last and if last length is equal to the chunk len 11 | if (!last || last.length === len) { 12 | chunkedArr.push([val]); 13 | } else { 14 | last.push(val); 15 | } 16 | }); 17 | 18 | return chunkedArr; 19 | }; 20 | 21 | module.exports = chunkArray; 22 | -------------------------------------------------------------------------------- /chunk.test.js: -------------------------------------------------------------------------------- 1 | const chunkArray = require('./chunk'); 2 | 3 | test('chunkArray function exists', () => { 4 | expect(chunkArray).toBeDefined(); 5 | }); 6 | 7 | test('Chunk an array of 10 values with length of 2', () => { 8 | const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 9 | const len = 2; 10 | const chunkedArr = chunkArray(numbers, len); 11 | 12 | expect(chunkedArr).toEqual([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]); 13 | }); 14 | 15 | test('Chunk an array of 3 values with length of 1', () => { 16 | const numbers = [1, 2, 3]; 17 | const len = 1; 18 | const chunkedArr = chunkArray(numbers, len); 19 | 20 | expect(chunkedArr).toEqual([[1], [2], [3]]); 21 | }); 22 | -------------------------------------------------------------------------------- /functions.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios'); 2 | 3 | const functions = { 4 | add: (num1, num2) => num1 + num2, 5 | isNull: () => null, 6 | checkValue: x => x, 7 | createUser: () => { 8 | const user = { firstName: 'Brad' }; 9 | user['lastName'] = 'Traversy'; 10 | return user; 11 | }, 12 | fetchUser: () => 13 | axios 14 | .get('https://jsonplaceholder.typicode.com/users/1') 15 | .then(res => res.data) 16 | .catch(err => 'error') 17 | }; 18 | 19 | module.exports = functions; 20 | -------------------------------------------------------------------------------- /functions.test.js: -------------------------------------------------------------------------------- 1 | const functions = require('./functions'); 2 | 3 | // beforeEach(() => initDatabase()); 4 | // afterEach(() => closeDatabase()); 5 | 6 | // beforeAll(() => initDatabase()); 7 | // afterAll(() => closeDatabase()); 8 | 9 | // const initDatabase = () => console.log('Database Initialized...'); 10 | // const closeDatabase = () => console.log('Database Closed...'); 11 | const nameCheck = () => console.log('Checking Name....'); 12 | 13 | describe('Checking Names', () => { 14 | beforeEach(() => nameCheck()); 15 | 16 | test('User is Jeff', () => { 17 | const user = 'Jeff'; 18 | expect(user).toBe('Jeff'); 19 | }); 20 | 21 | test('User is Karen', () => { 22 | const user = 'Karen'; 23 | expect(user).toBe('Karen'); 24 | }); 25 | }); 26 | 27 | // toBe 28 | test('Adds 2 + 2 to equal 4', () => { 29 | expect(functions.add(2, 2)).toBe(4); 30 | }); 31 | 32 | // not 33 | test('Adds 2 + 2 to NOT equal 5', () => { 34 | expect(functions.add(2, 2)).not.toBe(5); 35 | }); 36 | 37 | // CHECK FOR TRUTHY & FALSY VALUES 38 | // toBeNull matches only null 39 | // toBeUndefined matches only undefined 40 | // toBeDefined is the opposite of toBeUndefined 41 | // toBeTruthy matches anything that an if statement treats as true 42 | // toBeFalsy matches anything that an if statement treats as false 43 | 44 | // toBeNull 45 | test('Should be null', () => { 46 | expect(functions.isNull()).toBeNull(); 47 | }); 48 | 49 | // toBeFalsy 50 | test('Should be falsy', () => { 51 | expect(functions.checkValue(undefined)).toBeFalsy(); 52 | }); 53 | 54 | // toEqual 55 | test('User should be Brad Traversy object', () => { 56 | expect(functions.createUser()).toEqual({ 57 | firstName: 'Brad', 58 | lastName: 'Traversy' 59 | }); 60 | }); 61 | 62 | // Less than and greater than 63 | test('Should be under 1600', () => { 64 | const load1 = 800; 65 | const load2 = 800; 66 | expect(load1 + load2).toBeLessThanOrEqual(1600); 67 | }); 68 | 69 | // Regex 70 | test('There is no I in team', () => { 71 | expect('team').not.toMatch(/I/i); 72 | }); 73 | 74 | // Arrays 75 | test('Admin should be in usernames', () => { 76 | usernames = ['john', 'karen', 'admin']; 77 | expect(usernames).toContain('admin'); 78 | }); 79 | 80 | // Working with async data 81 | 82 | // Promise 83 | // test('User fetched name should be Leanne Graham', () => { 84 | // expect.assertions(1); 85 | // return functions.fetchUser().then(data => { 86 | // expect(data.name).toEqual('Leanne Graham'); 87 | // }); 88 | // }); 89 | 90 | // Async Await 91 | test('User fetched name should be Leanne Graham', async () => { 92 | expect.assertions(1); 93 | const data = await functions.fetchUser(); 94 | expect(data.name).toEqual('Leanne Graham'); 95 | }); 96 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest_testing_basics", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest", 8 | "testwatch": "jest --watchAll" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "devDependencies": { 14 | "jest": "^22.4.3" 15 | }, 16 | "dependencies": { 17 | "axios": "^0.18.0" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /reversestring.js: -------------------------------------------------------------------------------- 1 | const reverseString = str => 2 | str 3 | .toLowerCase() 4 | .split('') 5 | .reverse() 6 | .join(''); 7 | 8 | module.exports = reverseString; 9 | -------------------------------------------------------------------------------- /reversestring.test.js: -------------------------------------------------------------------------------- 1 | const reverseString = require('./reversestring'); 2 | 3 | test('reverseString function exists', () => { 4 | expect(reverseString).toBeDefined(); 5 | }); 6 | 7 | test('String reverses', () => { 8 | expect(reverseString('hello')).toEqual('olleh'); 9 | }); 10 | 11 | test('String reverses with uppercase', () => { 12 | expect(reverseString('Hello')).toEqual('olleh'); 13 | }); 14 | --------------------------------------------------------------------------------