├── .circleci └── config.yml ├── .gitignore ├── .vscode └── launch.json ├── I.pass-tests ├── 01-add-one │ ├── add-one.js │ └── add-one.test.js ├── 02-get-word-lengths │ ├── get-word-lengths.js │ └── get-word-lengths.test.js ├── 03-sum-numbers │ ├── add-numbers.js │ └── add-numbers.test.js ├── 04-find-needle │ ├── find-needle.js │ └── find-needle.test.js ├── 05-factorial │ ├── factorial.js │ └── factorial.test.js ├── 06-remove-middle │ ├── remove-middle.js │ └── remove-middle.test.js ├── 07-second-largest │ ├── second-largest.js │ └── second-largest.test.js ├── 08-get-average │ ├── get-average.js │ └── get-average.test.js ├── 09-car-sales │ ├── car-sales.js │ └── car-sales.test.js ├── 10-paint-ford │ ├── paint-cars.js │ └── paint-cars.test.js ├── 11-cities-formatter │ ├── cities.js │ └── cities.test.js └── ES-6-practice │ ├── es6.js │ └── es6.test.js ├── II.write-tests ├── 01-greet-people │ ├── greet-people.js │ └── greet-people.test.js ├── 02-remove-vowels │ ├── remove-vowels.js │ └── remove-vowels.test.js ├── 03-remove-vowels-from-array │ ├── remove-vowels-in-array.js │ └── remove-vowels-in-array.test.js ├── 04-numbers-greater-than-10 │ ├── numbersGreaterThan10.js │ └── numbersGreaterThan10.test.js ├── 05-get-second-third-smallest │ ├── get-second-third.js │ └── get-second-third.test.js ├── 06-largest-number │ ├── largest-number.js │ └── largest-number.test.js ├── 07-get-even-numbers │ ├── get-even-numbers.js │ └── get-even-numbers.test.js └── 09-test-async │ ├── async-1.js │ ├── async-1.test.js │ ├── async-2.js │ ├── async-2.test.js │ └── fetcher.js ├── III.tdd-katas ├── calculator-tdd │ └── README.md ├── fizz-buzz │ ├── README.md │ ├── fizz-buzz.js │ └── fizz-buzz.test.js ├── password-verifier │ └── README.md └── roman-converter │ └── README.md ├── README.md ├── package-lock.json └── package.json /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | jobs: 3 | build: 4 | docker: 5 | # specify the version you desire here 6 | - image: circleci/node:7.10 7 | 8 | # Specify service dependencies here if necessary 9 | # CircleCI maintains a library of pre-built images 10 | # documented at https://circleci.com/docs/2.0/circleci-images/ 11 | # - image: circleci/mongo:3.4.4 12 | 13 | working_directory: ~/repo 14 | 15 | steps: 16 | - checkout 17 | 18 | # Download and cache dependencies 19 | - restore_cache: 20 | keys: 21 | - v1-dependencies-{{ checksum "package.json" }} 22 | # fallback to using the latest cache if no exact match is found 23 | - v1-dependencies- 24 | 25 | - run: npm install 26 | 27 | - save_cache: 28 | paths: 29 | - node_modules 30 | key: v1-dependencies-{{ checksum "package.json" }} 31 | 32 | # run tests! 33 | - run: npm test -- pass-tests 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage/ 3 | .vscode -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "type": "node", 6 | "request": "launch", 7 | "name": "Launch Program", 8 | "program": "${file}" 9 | }, 10 | { 11 | "name": "Debug Jest", // name to show in Debug config picker 12 | "type": "node", 13 | "request": "launch", 14 | "address": "localhost", 15 | // everything above here is standard node debug 16 | // some people specify "port": 5858, but that didn't work; Code seems to pick the right one now 17 | "sourceMaps": false, // if you are using Babel or TS, make this true 18 | // you can add another arg to pattern-match limit the tests, just as when normally running jest 19 | "runtimeArgs": [ 20 | "${workspaceRoot}/node_modules/jest/bin/jest.js", 21 | "--runInBand", // https://facebook.github.io/jest/docs/en/cli.html#runinband - don't parallelize 22 | "--no-cache" // https://facebook.github.io/jest/docs/en/cli.html#cache - just avoid caching issues 23 | ], 24 | "env": { 25 | "NODE_ENV": "test" // make sure it matches your target; useful for babel config 26 | } 27 | } 28 | ] 29 | } -------------------------------------------------------------------------------- /I.pass-tests/01-add-one/add-one.js: -------------------------------------------------------------------------------- 1 | module.exports = function(numbers) {}; 2 | -------------------------------------------------------------------------------- /I.pass-tests/01-add-one/add-one.test.js: -------------------------------------------------------------------------------- 1 | var addOne = require("./add-one.js"); 2 | 3 | test("Add 1 to each item in myArray", function() { 4 | var myArray = [31, 57, 12, 5]; 5 | 6 | var unchanged = [31, 57, 12, 5]; 7 | var expected = [32, 58, 13, 6]; 8 | var output = addOne(myArray); 9 | 10 | expect(output).toEqual(expected); 11 | expect(myArray).toEqual(unchanged); 12 | }); 13 | -------------------------------------------------------------------------------- /I.pass-tests/02-get-word-lengths/get-word-lengths.js: -------------------------------------------------------------------------------- 1 | var getWordLengths = function(someWords) {}; 2 | 3 | module.exports = getWordLengths; 4 | -------------------------------------------------------------------------------- /I.pass-tests/02-get-word-lengths/get-word-lengths.test.js: -------------------------------------------------------------------------------- 1 | var wordLengths = require("./get-word-lengths"); 2 | 3 | test("Get word lengths", function() { 4 | var words = ["sun", "potato", "roundabout", "pizza"]; 5 | var expected = [3, 6, 10, 5]; 6 | 7 | var output = wordLengths(words); 8 | expect(output).toEqual(expected); 9 | }); 10 | -------------------------------------------------------------------------------- /I.pass-tests/03-sum-numbers/add-numbers.js: -------------------------------------------------------------------------------- 1 | function addNumbers(numbers) {} 2 | module.exports = addNumbers; 3 | -------------------------------------------------------------------------------- /I.pass-tests/03-sum-numbers/add-numbers.test.js: -------------------------------------------------------------------------------- 1 | var addAllnumbers = require("./add-numbers"); 2 | 3 | test("Add all numbers", function() { 4 | var numbers = [9, 23, 10, 3, 8]; 5 | var expected = 53; 6 | 7 | var output = addAllnumbers(numbers); 8 | 9 | expect(output).toEqual(expected); 10 | }); 11 | -------------------------------------------------------------------------------- /I.pass-tests/04-find-needle/find-needle.js: -------------------------------------------------------------------------------- 1 | function findNeedle(words) {} 2 | 3 | module.exports = findNeedle; 4 | -------------------------------------------------------------------------------- /I.pass-tests/04-find-needle/find-needle.test.js: -------------------------------------------------------------------------------- 1 | var findTheNeedle = require("./find-needle"); 2 | 3 | test("Find the needle", function() { 4 | var words = ["house", "train", "slide", "needle", "book"]; 5 | var expected = 3; 6 | 7 | var output = findTheNeedle(words, "needle"); 8 | expect(output).toEqual(expected); 9 | }); 10 | 11 | test("Find the plant", function() { 12 | var words = ["plant", "shelf", "arrow", "bird"]; 13 | var expected = 0; 14 | 15 | var output = findTheNeedle(words, "plant"); 16 | expect(output).toEqual(expected); 17 | }); 18 | -------------------------------------------------------------------------------- /I.pass-tests/05-factorial/factorial.js: -------------------------------------------------------------------------------- 1 | // int is an integer 2 | // a factorial is the product of all non-negative integers 3 | // less than or equal to the iniital number. 4 | 5 | // for example the factorial of 5 is 120 6 | // 120 = 1 * 2 * 3 * 4 * 5 7 | 8 | // calculate and return the factorial of int 9 | // note: factorial of 0 is 1 10 | 11 | function factorial(int) {} 12 | 13 | module.exports = factorial; 14 | -------------------------------------------------------------------------------- /I.pass-tests/05-factorial/factorial.test.js: -------------------------------------------------------------------------------- 1 | var factorial = require("./factorial"); 2 | 3 | describe("Factorial", function() { 4 | test("Factorial", function() { 5 | var in1 = 5; 6 | var exp1 = 120; 7 | 8 | var in2 = 9; 9 | var exp2 = 362880; 10 | 11 | var in3 = 1; 12 | var exp3 = 1; 13 | 14 | var in4 = 0; 15 | var exp4 = 1; 16 | 17 | var in5 = 3; 18 | var exp5 = 6; 19 | 20 | var out1 = factorial(in1); 21 | var out2 = factorial(in2); 22 | var out3 = factorial(in3); 23 | var out4 = factorial(in4); 24 | var out5 = factorial(in5); 25 | 26 | expect(out1).toEqual(exp1); 27 | expect(out2).toEqual(exp2); 28 | expect(out3).toEqual(exp3); 29 | expect(out4).toEqual(exp4); 30 | expect(out5).toEqual(exp5); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /I.pass-tests/06-remove-middle/remove-middle.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeYourFuture/js-exercises-tdd/6bc364d8874621ac89143711b3051f19b84d87bb/I.pass-tests/06-remove-middle/remove-middle.js -------------------------------------------------------------------------------- /I.pass-tests/06-remove-middle/remove-middle.test.js: -------------------------------------------------------------------------------- 1 | var removeMiddle = require("./remove-middle"); 2 | 3 | test("Remove middle", function() { 4 | var words = ["mouse", "giraffe", "queen", "window", "bottle"]; 5 | 6 | var expectedWords = ["mouse", "giraffe", "window", "bottle"]; 7 | var expectedOutput = ["queen"]; 8 | 9 | var output = removeMiddle(words); 10 | 11 | expect(output).toEqual(expectedOutput); 12 | expect(words).toEqual(expectedWords); 13 | }); 14 | -------------------------------------------------------------------------------- /I.pass-tests/07-second-largest/second-largest.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeYourFuture/js-exercises-tdd/6bc364d8874621ac89143711b3051f19b84d87bb/I.pass-tests/07-second-largest/second-largest.js -------------------------------------------------------------------------------- /I.pass-tests/07-second-largest/second-largest.test.js: -------------------------------------------------------------------------------- 1 | var secondLargest = require("./second-largest"); 2 | 3 | test("Second largest", function() { 4 | var numbers = [2, 0, 23, 0, 57, 1, 230]; 5 | 6 | var output = secondLargest(numbers); 7 | 8 | expect(output).toEqual(57); 9 | }); 10 | -------------------------------------------------------------------------------- /I.pass-tests/08-get-average/get-average.js: -------------------------------------------------------------------------------- 1 | // the input is an array of numbers and strings 2 | // return the average of all the numbers 3 | // be sure to exclude the strings 4 | -------------------------------------------------------------------------------- /I.pass-tests/08-get-average/get-average.test.js: -------------------------------------------------------------------------------- 1 | var average = require("./get-average"); 2 | 3 | test("Average", function() { 4 | var numbers = [4, "-", 8, 11, "hello", "57", 0, 2]; 5 | var expected = 5; 6 | 7 | var output = average(numbers); 8 | 9 | expect(output).toEqual(expected); 10 | }); 11 | -------------------------------------------------------------------------------- /I.pass-tests/09-car-sales/car-sales.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeYourFuture/js-exercises-tdd/6bc364d8874621ac89143711b3051f19b84d87bb/I.pass-tests/09-car-sales/car-sales.js -------------------------------------------------------------------------------- /I.pass-tests/09-car-sales/car-sales.test.js: -------------------------------------------------------------------------------- 1 | var sales = require("./car-sales"); 2 | 3 | test("Car sales", function() { 4 | var carsSold = [ 5 | { make: "Ford", model: "Fiesta", colour: "Red", price: 5999 }, 6 | { make: "Land Rover", model: "Defender", colour: "Muddy", price: 12000 }, 7 | { make: "Toyota", model: "Prius", colour: "Silver", price: 6500 }, 8 | { make: "Honda", model: "Civic", colour: "Yellow", price: 8000 }, 9 | { make: "Ford", model: "Fiesta", colour: "Red", price: 15000 }, 10 | { make: "Land Rover", model: "Discovery", colour: "Blue", price: 9000 }, 11 | { make: "Ford", model: "Fiesta", colour: "Green", price: 2000 } 12 | ]; 13 | 14 | var totals = { 15 | Ford: 22999, 16 | Honda: 8000, 17 | "Land Rover": 21000, 18 | Toyota: 6500 19 | }; 20 | 21 | var output = sales(carsSold); 22 | 23 | expect(output).toEqual(totals); 24 | }); 25 | -------------------------------------------------------------------------------- /I.pass-tests/10-paint-ford/paint-cars.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeYourFuture/js-exercises-tdd/6bc364d8874621ac89143711b3051f19b84d87bb/I.pass-tests/10-paint-ford/paint-cars.js -------------------------------------------------------------------------------- /I.pass-tests/10-paint-ford/paint-cars.test.js: -------------------------------------------------------------------------------- 1 | var paintShop = require("./paint-cars"); 2 | 3 | test("Paint shop", function() { 4 | var cars = [ 5 | { make: "Ford", model: "Fiesta", colour: "Red" }, 6 | { make: "Land Rover", model: "Defender", colour: "Muddy" }, 7 | { make: "Toyota", model: "Prius", colour: "Silver" }, 8 | { make: "Honda", model: "Civic", colour: "Yellow" } 9 | ]; 10 | 11 | var unpaintedCars = [ 12 | { make: "Ford", model: "Fiesta", colour: "Red" }, 13 | { make: "Land Rover", model: "Defender", colour: "Muddy" }, 14 | { make: "Toyota", model: "Prius", colour: "Silver" }, 15 | { make: "Honda", model: "Civic", colour: "Yellow" } 16 | ]; 17 | 18 | var repaintedCars = [ 19 | { make: "Ford", model: "Fiesta", colour: "Pink" }, 20 | { make: "Land Rover", model: "Defender", colour: "Muddy" }, 21 | { make: "Toyota", model: "Prius", colour: "Silver" }, 22 | { make: "Honda", model: "Civic", colour: "Yellow" } 23 | ]; 24 | 25 | var output = paintShop(cars, "Pink"); 26 | 27 | expect(output).toEqual(repaintedCars); 28 | expect(cars).toEqual(unpaintedCars); 29 | }); 30 | -------------------------------------------------------------------------------- /I.pass-tests/11-cities-formatter/cities.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeYourFuture/js-exercises-tdd/6bc364d8874621ac89143711b3051f19b84d87bb/I.pass-tests/11-cities-formatter/cities.js -------------------------------------------------------------------------------- /I.pass-tests/11-cities-formatter/cities.test.js: -------------------------------------------------------------------------------- 1 | var formatCities = require("./cities"); 2 | 3 | test("Cities", function() { 4 | var capitals = [ 5 | { city: "Paris", country: "France" }, 6 | { city: "Madrid", country: "Spain" }, 7 | { city: "Rome", country: "Italy" } 8 | ]; 9 | 10 | function transform({ city, country }) { 11 | return `${city} is the capital of ${country}`; 12 | } 13 | 14 | var expected = [ 15 | "Paris is the capital of France", 16 | "Madrid is the capital of Spain", 17 | "Rome is the capital of Italy" 18 | ]; 19 | 20 | var result = formatCities(capitals, transform); 21 | 22 | expect(result).toEqual(expected); 23 | }); 24 | -------------------------------------------------------------------------------- /I.pass-tests/ES-6-practice/es6.js: -------------------------------------------------------------------------------- 1 | // Turn this function into an arrow function 2 | function arrow() { 3 | return "es6 is awesome!"; 4 | } 5 | 6 | //Use the es6 syntax for default parameter 7 | function defaultParameter(name) { 8 | var name = name || "sam"; 9 | return name; 10 | } 11 | 12 | // Use the spread operator to combine arr1 and arr2 13 | function combineArrays(arr1, arr2) { 14 | return arr1.concat(arr2); 15 | } 16 | 17 | //use destructuring to return the object's cyf property 18 | function destructuring(obj) { 19 | return obj.cyf; 20 | } 21 | 22 | // use template literal to return a string with the sum of a and b 23 | function templateString(a, b) { 24 | return "The sum is equal to " + (a + b).toString(); 25 | } 26 | 27 | module.exports = { 28 | arrow, 29 | defaultParameter, 30 | combineArrays, 31 | destructuring, 32 | templateString 33 | }; 34 | -------------------------------------------------------------------------------- /I.pass-tests/ES-6-practice/es6.test.js: -------------------------------------------------------------------------------- 1 | const { 2 | arrow, 3 | defaultParameter, 4 | combineArrays, 5 | destructuring, 6 | templateString 7 | } = require("./index"); 8 | 9 | test("function arrow is instance of a function", () => { 10 | expect(arrow).toBeInstanceOf(Function); 11 | }); 12 | 13 | test("function arrow returns a string of es6 is awesome", () => { 14 | expect(arrow()).toEqual("es6 is awesome!"); 15 | }); 16 | 17 | test("defaultParameter function should return sam if no name argument passed in", () => { 18 | expect(defaultParameter()).toEqual("sam"); 19 | }); 20 | 21 | test("defaultParameter function should return name if name argument is passed in", () => { 22 | expect(defaultParameter("samatar")).toEqual("samatar"); 23 | }); 24 | 25 | test("combineArrays function should combine arr1 and arr2", () => { 26 | expect(combineArrays(arr1, arr2)).toEqual([1, 2, 3, 4, 5, 6]); 27 | }); 28 | 29 | test("destructuring function should return cyf property of object", () => { 30 | expect(destructuring(testObj)).toEqual("awesome"); 31 | }); 32 | 33 | test("templateString should return a string with the sum of parameter a and b", () => { 34 | expect(templateString(1, 2)).toEqual("The sum is equal to 3"); 35 | }); 36 | 37 | const arr1 = [1, 2, 3]; 38 | const arr2 = [4, 5, 6]; 39 | const testObj = { 40 | cyf: "awesome", 41 | test: "100%", 42 | learning: "always" 43 | }; 44 | -------------------------------------------------------------------------------- /II.write-tests/01-greet-people/greet-people.js: -------------------------------------------------------------------------------- 1 | function greetPeople(people) { 2 | var greeting = "Hello "; 3 | 4 | people.forEach(function(person) { 5 | greeting = greeting + person; 6 | }); 7 | 8 | return greeting; 9 | } 10 | 11 | module.exports = greetPeople; 12 | 13 | /* 14 | Let's trace this piece of code - what is the value of result with this input 15 | 16 | var mentors = ['Irina', 'Ashleigh', 'Etza']; 17 | var result = greetPeople(mentors) 18 | */ 19 | -------------------------------------------------------------------------------- /II.write-tests/01-greet-people/greet-people.test.js: -------------------------------------------------------------------------------- 1 | test("print list of names prefixed with Hello", function() { 2 | // Arrange 3 | // Act 4 | // Assert 5 | }); 6 | -------------------------------------------------------------------------------- /II.write-tests/02-remove-vowels/remove-vowels.js: -------------------------------------------------------------------------------- 1 | function removeVowels(word) { 2 | var characters = word.split(""); 3 | 4 | var result = []; 5 | 6 | characters.forEach(function(character) { 7 | if ( 8 | character === "a" || 9 | character === "o" || 10 | character === "i" || 11 | character === "e" || 12 | character === "u" 13 | ) { 14 | result.push(character); 15 | } else { 16 | result.push("_"); 17 | } 18 | }); 19 | 20 | return result.join(""); 21 | } 22 | 23 | module.exports = removeVowels; 24 | 25 | /* 26 | Let's trace this piece of code - what is the value of result with this input 27 | 28 | var result = removeVowels('samuel'); 29 | 30 | what is the value of result? 31 | */ 32 | -------------------------------------------------------------------------------- /II.write-tests/02-remove-vowels/remove-vowels.test.js: -------------------------------------------------------------------------------- 1 | test("remove vowels from word", function() { 2 | // Arrange 3 | // Act 4 | // Assert 5 | }); 6 | -------------------------------------------------------------------------------- /II.write-tests/03-remove-vowels-from-array/remove-vowels-in-array.js: -------------------------------------------------------------------------------- 1 | var removeVowels = require("../02-remove-vowels/remove-vowels"); 2 | 3 | function removeVowelsForWords(words) { 4 | var result = words.map(function(word) { 5 | return removeVowels(word); 6 | }); 7 | 8 | return result; 9 | } 10 | 11 | module.exports = removeVowelsForWords; 12 | 13 | /* 14 | input: ["Irina", "Etza", "Daniel"] 15 | expected output: ["rn", "tz", "Dnl"] 16 | */ 17 | -------------------------------------------------------------------------------- /II.write-tests/03-remove-vowels-from-array/remove-vowels-in-array.test.js: -------------------------------------------------------------------------------- 1 | test("remove vowels from all words in array", function() { 2 | // Arrange 3 | // Act 4 | // Assert 5 | }); 6 | 7 | // example 8 | // input: ["Irina", "Etza", "Daniel"] 9 | // expected output: ["rn", "tz", "Dnl"] 10 | -------------------------------------------------------------------------------- /II.write-tests/04-numbers-greater-than-10/numbersGreaterThan10.js: -------------------------------------------------------------------------------- 1 | module.exports = function(array) { 2 | return array.filter(number => { 3 | return number > 10; 4 | }); 5 | }; 6 | -------------------------------------------------------------------------------- /II.write-tests/04-numbers-greater-than-10/numbersGreaterThan10.test.js: -------------------------------------------------------------------------------- 1 | var largerThanTen = require("./numbersGreaterThan10"); 2 | 3 | test("Get numbers greater than 10", function() {}); 4 | 5 | // input: [4, 10, 32, 9, 21]; 6 | // expected output: [32, 21]; 7 | -------------------------------------------------------------------------------- /II.write-tests/05-get-second-third-smallest/get-second-third.js: -------------------------------------------------------------------------------- 1 | module.exports = function(array) { 2 | const newArray = array.slice(); 3 | newArray.sort(function(x, y) { 4 | return x - y; 5 | }); 6 | return [newArray[1], newArray[2]]; 7 | }; 8 | -------------------------------------------------------------------------------- /II.write-tests/05-get-second-third-smallest/get-second-third.test.js: -------------------------------------------------------------------------------- 1 | // example 2 | // input = [90, 5, 11, 8, 6]; 3 | // expected output = [6, 8]; 4 | 5 | // also test that the original array has not changed 6 | -------------------------------------------------------------------------------- /II.write-tests/06-largest-number/largest-number.js: -------------------------------------------------------------------------------- 1 | function getLargestNumber(array) { 2 | var largestNumber; 3 | for (var i = 0; i < array.length - 1; i++) { 4 | if (array[i] > array[i + 1]) { 5 | largestNumber = array[i]; 6 | } 7 | } 8 | return largestNumber; 9 | } 10 | 11 | module.exports = getLargestNumber; 12 | -------------------------------------------------------------------------------- /II.write-tests/06-largest-number/largest-number.test.js: -------------------------------------------------------------------------------- 1 | // example 2 | // input: [3, 21, 88, 4, 36]; 3 | // expected: 88; 4 | 5 | // also test that the original array hasn't changed 6 | -------------------------------------------------------------------------------- /II.write-tests/07-get-even-numbers/get-even-numbers.js: -------------------------------------------------------------------------------- 1 | function getEven(numbers) { 2 | return numbers.filter(function(number) { 3 | return number % 2 === 0; 4 | }); 5 | } 6 | 7 | module.exports = getEven; 8 | -------------------------------------------------------------------------------- /II.write-tests/07-get-even-numbers/get-even-numbers.test.js: -------------------------------------------------------------------------------- 1 | // example 2 | // input: [22, 13, 73, 82, 4]; 3 | // expected: [22, 82, 4]; 4 | -------------------------------------------------------------------------------- /II.write-tests/09-test-async/async-1.js: -------------------------------------------------------------------------------- 1 | const fetch = require("node-fetch"); 2 | 3 | const getRepos = function(repoName) { 4 | return fetch(repoName) 5 | .then(data => data.json()) 6 | .then(function(response) { 7 | return response.map(function(rep) { 8 | return rep.name; 9 | }); 10 | }); 11 | }; 12 | 13 | module.exports = getRepos; 14 | -------------------------------------------------------------------------------- /II.write-tests/09-test-async/async-1.test.js: -------------------------------------------------------------------------------- 1 | const getRepos = require("./async-1"); 2 | 3 | test("gets a list of repositories names", function() { 4 | // arrange 5 | var url = "https://api.github.com/users/kabaros/repos"; 6 | // act 7 | return getRepos(url).then(function(result) { 8 | // assert 9 | expect(result).toContain("js-exercises"); 10 | expect(result).toContain("dom-ajax-repo"); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /II.write-tests/09-test-async/async-2.js: -------------------------------------------------------------------------------- 1 | const fetcher = require("./fetcher"); 2 | 3 | const getRepos = function(repoName) { 4 | return fetcher(repoName).then(function(response) { 5 | return response.map(function(rep) { 6 | return rep.name; 7 | }); 8 | }); 9 | }; 10 | 11 | module.exports = getRepos; 12 | -------------------------------------------------------------------------------- /II.write-tests/09-test-async/async-2.test.js: -------------------------------------------------------------------------------- 1 | const getRepos = require("./async-2"); 2 | const fetcher = require("./fetcher"); 3 | jest.mock("./fetcher"); 4 | 5 | test("gets a list of repositories names (with mock)", function() { 6 | // arrange 7 | fetcher.mockResolvedValue([{ name: "js-exercises" }]); 8 | var url = "https://api.github.com/users/kabaros/repos"; 9 | 10 | // act 11 | return getRepos(url).then(function(result) { 12 | // assert 13 | expect(result).toContain("js-exercises"); 14 | }); 15 | }); 16 | 17 | test("a more deterministic test", function() {}); 18 | -------------------------------------------------------------------------------- /II.write-tests/09-test-async/fetcher.js: -------------------------------------------------------------------------------- 1 | const fetch = require("node-fetch"); 2 | 3 | const fetcher = function(repoName) { 4 | return fetch(repoName).then(data => data.json()); 5 | }; 6 | 7 | module.exports = fetcher; 8 | -------------------------------------------------------------------------------- /III.tdd-katas/calculator-tdd/README.md: -------------------------------------------------------------------------------- 1 | # String Calculator Kata 2 | The following is a TDD Kata, an exercise in coding, refactoring and test-first, that you should apply daily for at least 15-30 minutes. 3 | 4 | ## Before you start 5 | * Try not to read ahead. 6 | * Do one task at a time. The trick is to learn to work incrementally. 7 | * Make sure you only test for correct inputs. There is no need to test for invalid inputs for this kata. 8 | 9 | ## The kata 10 | 11 | ### Step 1: the simplest thing 12 | Create a simple String calculator with a method ``int add(String numbers)``. 13 | 14 | * The string argument can contain 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example ``""`` or ``"1"`` or ``"1,2"``. 15 | * Start with the simplest test case of an empty string and move to 1 and two numbers. 16 | * Remember to solve things as simply as possible so that you force yourself to write tests you did not think about. 17 | * Remember to refactor after each passing test. 18 | 19 | ### Step 2: handle an unknown amount of numbers 20 | Allow the ``add()`` method to handle an unknown amount of numbers. 21 | 22 | ### Step 3: handle new lines between numbers 23 | Allow the ``add()`` method to handle new lines between numbers (instead of commas). 24 | 25 | * the following input is ok: ``"1\n2,3"`` (will equal 6) 26 | * the following input is NOT ok: ``"1,\n"`` (not need to prove it - just clarifying) 27 | 28 | ### Step 4: support different delimiters 29 | Support different delimiters: to change a delimiter, the beginning of the string will contain a separate line that looks like this: 30 | 31 | ``"//[delimiter]\n[numbers...]"`` 32 | 33 | For example ``"//;\n1;2"`` should return 3 where the default delimiter is ``';'``. 34 | 35 | The first line is optional. 36 | All existing scenarios should still be supported. 37 | 38 | ### Step 5: negative numbers 39 | Calling ``add()`` with a negative number will throw an exception ``"negatives not allowed"`` - and the negative that was passed. 40 | 41 | For example ``add("1,4,-1")`` should throw an exception with the message ``"negatives not allowed: -1"``. 42 | 43 | If there are multiple negatives, show all of them in the exception message. 44 | 45 | ### Step 6: ignore big numbers 46 | Numbers bigger than 1000 should be ignored, so adding 2 + 1001 = 2 47 | 48 | ## General requirements 49 | - Use whatever language and frameworks you want. Use something that you know well. 50 | - Provide a README with instructions on how to compile and run the application. 51 | 52 | **IMPORTANT:** Implement the requirements focusing on **writing the best code** you can produce. 53 | 54 | **CODE SUBMISSION:** Add the code to your own Github account and send us the link. 55 | 56 | Credits to [Roy Osherove](http://osherove.com/tdd-kata-1) for the original idea. -------------------------------------------------------------------------------- /III.tdd-katas/fizz-buzz/README.md: -------------------------------------------------------------------------------- 1 | # Fizz buzz Kata 2 | The following is a TDD Kata, an exercise in coding, refactoring and test-first, that you should apply daily for at least 15-30 minutes. 3 | 4 | ## Before you start 5 | * Try not to read ahead. 6 | * Do one task at a time. The trick is to learn to work incrementally. 7 | * Make sure you only test for correct inputs. There is no need to test for invalid inputs for this kata. 8 | 9 | ## The kata 10 | 11 | ### Step 1 12 | Write a program that prints the numbers from 1 to n (where n is the number to stop counting). But for multiples of three print "Fizz" instead of the 13 | number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five 14 | print "FizzBuzz?". 15 | 16 | Sample output: 17 | ``` 18 | 1 19 | 2 20 | Fizz 21 | 4 22 | Buzz 23 | Fizz 24 | 7 25 | 8 26 | Fizz 27 | Buzz 28 | 11 29 | Fizz 30 | 13 31 | 14 32 | FizzBuzz 33 | 16 34 | 17 35 | Fizz 36 | 19 37 | Buzz 38 | ... etc up to n 39 | ``` 40 | 41 | ## Step 2 - new requirements 42 | * A number is fizz if it is divisible by 3 or if it has a 3 in it 43 | * A number is buzz if it is divisible by 5 or if it has a 5 in it -------------------------------------------------------------------------------- /III.tdd-katas/fizz-buzz/fizz-buzz.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeYourFuture/js-exercises-tdd/6bc364d8874621ac89143711b3051f19b84d87bb/III.tdd-katas/fizz-buzz/fizz-buzz.js -------------------------------------------------------------------------------- /III.tdd-katas/fizz-buzz/fizz-buzz.test.js: -------------------------------------------------------------------------------- 1 | var fizzbuzz = require("./fizz-buzz"); 2 | 3 | test("prints 1", function() { 4 | var result = fizzbuzz(1); 5 | expect(result).toEqual("1"); 6 | }); 7 | 8 | test("prints 1,2", function() { 9 | var result = fizzbuzz(2); 10 | expect(result).toEqual("1, 2"); 11 | }); 12 | -------------------------------------------------------------------------------- /III.tdd-katas/password-verifier/README.md: -------------------------------------------------------------------------------- 1 | Source: http://osherove.com/tdd-kata-3-refactoring/ 2 | TDD Kata 3 3 | -------------------------------------------------------------------------- 4 | Create a Password verifications class called “PasswordVerifier”. 5 | 6 | 1. Add the following verifications to a master function called “Verify()” 7 | 8 | 1. - password should be larger than 8 chars 9 | 2. - password should not be null 10 | 3. - password should have one uppercase letter at least 11 | 4. - password should have one lowercase letter at least 12 | 5. - password should have one number at least 13 | 14 | Each one of these should throw an exception with a different message of your choosing 15 | 16 | 2. Add feature: Password is OK if at least three of the previous conditions is true 17 | 3. Add feature: password is never OK if item 1.4 is not true. 18 | 4. Assume Each verification takes 1 second to complete. How would you solve items 2 and 3 so tests can run faster? -------------------------------------------------------------------------------- /III.tdd-katas/roman-converter/README.md: -------------------------------------------------------------------------------- 1 | # Roman calculator 2 | 3 | Using TDD, write a function that converts an Arabic number (the numbers used here) to Roman Numerals. 4 | 5 | > Always write tests first 6 | 7 | ### Part One: Old Roman Numerals 8 | 9 | | Arabic Number | Roman Numeral | 10 | | -------------- | ------------- | 11 | | 1 | I | 12 | | 5 | V | 13 | | 10 | X | 14 | | 50 | L | 15 | | 100 | C | 16 | | 500 | D | 17 | | 1000 | M | 18 | 19 | *Table 1*. Arabic number and their old Roman numeral equivalents. 20 | 21 | In the early days of Roman numerals, the Romans built their numerals from the individual characters in Table 1 (e.g., I, V, X, etc.) written largest value to smallest from left to right. To determine the value of any numeral, they used straight addition. I is equivalent to 1. II is equivalent to 1 + 1, or 2. VIIII is equivalent to 5 + 1 + 1 + 1 + 1, or 9. 22 | 23 | ### More examples 24 | 25 | | Arabic Number | Roman Numeral | 26 | | -------------- | ------------- | 27 | | 1 | I | 28 | | 3 | III | 29 | | 7 | VII | 30 | | 15 | XV | 31 | | 18 | XVIII | 32 | | 22 | XXII | 33 | 34 | We are going to begin writing a function `convertToOldRoman`. When passed an integer between 1 and 3000, this function will return a string containing the proper old Roman Numeral. In other words, `convertToOldRoman(4)` should return the string `'IIII'`. Don't worry about checking whether or not the number passed to the method is between 1 and 3000. 35 | 36 | *Hint*: It might be useful to use the integer division `/` and modulus `%` methods. 37 | 38 | ### Part Two: Modern Roman Numerals 39 | 40 | | Arabic Number | Roman Numeral | 41 | | ------------- | ------------- | 42 | | 4 | IV | 43 | | 9 | IX | 44 | | 14 | XIV | 45 | | 44 | XLIV | 46 | | 99 | XCIX | 47 | | 400 | CD | 48 | | 944 | CMXLIV | 49 | 50 | *Table 2*. Modern Roman numeral examples. 51 | 52 | Eventually, the Romans changed systems. Now, putting a smaller numeral before a larger one meant you had to subtract the smaller one. So, instead of 4 being represented by 1 + 1 + 1 + 1, it was now 5 - 1, or IV. 4 was not the only value affected (see Table 2 for more examples). 53 | 54 | We're going to write a second method so that will return modern Roman Numerals. We'll most likely use some similar logic as in the first function. 55 | 56 | ```javascript 57 | convertToOldRoman(4) 58 | => "IIII" 59 | 60 | convertToNewRoman(4) 61 | => "IV" 62 | ``` 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # README 2 | 3 | ## Before you start 4 | 5 | First step is to `fork` this repo to your account, then `clone` it locally. 6 | 7 | After you do that, `add a remote` to point to CodeYourFuture repo, that will allow you to update you forked copy when we update the repo with more exercises. 8 | 9 | This is the command to run (see https://help.github.com/articles/working-with-forks/): 10 | 11 | ``` 12 | git remote add upstream https://github.com/CodeYourFuture/js-exercises-tdd.git 13 | ``` 14 | 15 | **Always work on a branch not on master!** 16 | 17 | ## How to run the tests 18 | Once you cloned the repo, first, run `npm install`. 19 | 20 | To run the tests from the console, run the command: `npm test` 21 | To keep the tests running (auto updating when you save files), run the command: `npm test -- --watch` then press `a` to run all the tests and keep watching the files for changes. To quit the tests, type `q`. 22 | 23 | To run a specifc test, do: `npm test -- filname` for example `npm test -- remove-vowels` (that will run only the files that matches remove-vowels, i.e. remove-vowels.test.js) 24 | 25 | ### Write Tests 26 | The first set of exercises involve implemented code that we will add tests for. You can run only the tests in the *I.write-tests* folder by running `npm test -- write-tests` or you can run them with a watch with `npm test -- write-tests --watch` 27 | 28 | Think about **edge cases** while writing tests. 29 | 30 | ### Pass Tests 31 | The second set of exercises involve a set of tests that we will write code to make it pass. You can run only the tests in the *I.pass-tests* folder by running `npm test -- pass-tests` or you can run them with a watch with `npm test -- pass-tests --watch`. 32 | 33 | Once a test passes. Add another test for another test case, preferably another **edge case** to make sure your code is not *buggy*. 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tdd", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "devDependencies": { 12 | "jest": "^22.4.3" 13 | }, 14 | "coverageThreshold": { 15 | "global": { 16 | "branches": 80, 17 | "functions": 80, 18 | "lines": 80, 19 | "statements": -10 20 | } 21 | }, 22 | "dependencies": { 23 | "node-fetch": "^2.1.2" 24 | }, 25 | "jest": { 26 | "testURL": "http://localhost/" 27 | } 28 | } 29 | --------------------------------------------------------------------------------