diff --git a/Sprint-3/1-implement-and-rewrite-tests/README.md b/Sprint-3/1-implement-and-rewrite-tests/README.md new file mode 100644 index 000000000..18800d8f8 --- /dev/null +++ b/Sprint-3/1-implement-and-rewrite-tests/README.md @@ -0,0 +1,44 @@ +# Implement solutions and rewrite tests with Jest + +## 1 Implement solutions + +In the `implement` directory you've got a number of functions you'll need to implement. +For each function, you also have a number of different cases you'll need to check for your function. + +Write your assertions and build up your program case by case. Don't rush to a solution. The point of these assignments is to learn how to write assertions and build up a program step by step. + +Here is a recommended order: + +1. `1-get-angle-type.js` +1. `2-is-proper-fraction.js` +1. `3-get-card-value.js` + +## 2 Rewrite tests with Jest + +`console.log` is most often used as a debugging tool. We use to inspect the state of our program during runtime. + +We can use `console.assert` to write assertions: however, it is not very easy to use when writing large test suites. In the first section, Implement, we used a custom "helper function" to make our assertions more readable. + +Jest is a whole library of helper functions we can use to make our assertions more readable and easier to write. + +Your new task is to write the same tests as you wrote in the `implement` directory, but using Jest instead of `console.assert`. + +You shouldn't have to change the contents of `implement` to write these tests. + +There are files for your Jest tests in the `rewrite-tests-with-jest` directory. They will automatically use the functions you already implemented. + +You can run all the tests in this repo by running `npm test` in your terminal. However, VSCode has a built-in test runner that you can use to run the tests, and this should make it much easier to focus on building up your test cases one at a time. + +https://code.visualstudio.com/docs/editor/testing + +1. Go to rewrite-tests-with-jest/1-get-angle-type.test.js +1. Click the green play button to run the test. It's on the left of the test function in the gutter. +1. Read the output in the TEST_RESULTS tab at the bottom of the screen. +1. Explore all the tests in this repo by opening the TEST EXPLORER tab. The logo is a beaker. + +![VSCode Test Runner](../../run-this-test.png) + +![Test Results](../../test-results-output.png) + +> [!TIP] +> You can always run a single test file by running `npm test path/to/test-file.test.js`. diff --git a/Sprint-3/1-key-implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js similarity index 84% rename from Sprint-3/1-key-implement/1-get-angle-type.js rename to Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index 08d1f0cba..ca1dfe7f2 100644 --- a/Sprint-3/1-key-implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -8,10 +8,17 @@ // Then, write the next test! :) Go through this process until all the cases are implemented function getAngleType(angle) { - if (angle === 90) return "Right angle"; - // read to the end, complete line 36, then pass your test here + if (angle === 90) { + return "Right angle"; + } + // Run the tests, work out what Case 2 is testing, and implement the required code here. + // Then keep going for the other cases, one at a time. } +// The line below allows us to load the getAngleType function into tests in other files. +// This will be useful in the "rewrite tests with jest" step. +module.exports = getAngleType; + // we're going to use this helper function to make our assertions easier to read // if the actual output matches the target output, the test will pass function assertEquals(actualOutput, targetOutput) { diff --git a/Sprint-3/1-key-implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js similarity index 89% rename from Sprint-3/1-key-implement/2-is-proper-fraction.js rename to Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index 91583e941..a4739af77 100644 --- a/Sprint-3/1-key-implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -8,9 +8,15 @@ // write one test at a time, and make it pass, build your solution up methodically function isProperFraction(numerator, denominator) { - if (numerator < denominator) return true; + if (numerator < denominator) { + return true; + } } +// The line below allows us to load the isProperFraction function into tests in other files. +// This will be useful in the "rewrite tests with jest" step. +module.exports = isProperFraction; + // here's our helper again function assertEquals(actualOutput, targetOutput) { console.assert( diff --git a/Sprint-3/1-key-implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js similarity index 91% rename from Sprint-3/1-key-implement/3-get-card-value.js rename to Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index aa1cc9f90..266525d1b 100644 --- a/Sprint-3/1-key-implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -8,9 +8,15 @@ // write one test at a time, and make it pass, build your solution up methodically // just make one change at a time -- don't rush -- programmers are deep and careful thinkers function getCardValue(card) { - if (rank === "A") return 11; + if (rank === "A") { + return 11; + } } +// The line below allows us to load the getCardValue function into tests in other files. +// This will be useful in the "rewrite tests with jest" step. +module.exports = getCardValue; + // You need to write assertions for your function to check it works in different cases // we're going to use this helper function to make our assertions easier to read // if the actual output matches the target output, the test will pass diff --git a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js similarity index 77% rename from Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js rename to Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index b62827b7c..4a92a3e82 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -1,4 +1,6 @@ -const getAngleType = require("./1-get-angle-type"); +// This statement loads the getAngleType function you wrote in the implement directory. +// We will use the same function, but write tests for it using Jest in this file. +const getAngleType = require("../implement/1-get-angle-type"); test("should identify right angle (90°)", () => { expect(getAngleType(90)).toEqual("Right angle"); diff --git a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js similarity index 50% rename from Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js rename to Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index ff1cc8173..caf08d15b 100644 --- a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -1,4 +1,6 @@ -const isProperFraction = require("./2-is-proper-fraction"); +// This statement loads the isProperFraction function you wrote in the implement directory. +// We will use the same function, but write tests for it using Jest in this file. +const isProperFraction = require("../implement/2-is-proper-fraction"); test("should return true for a proper fraction", () => { expect(isProperFraction(2, 3)).toEqual(true); diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js new file mode 100644 index 000000000..04418ff72 --- /dev/null +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -0,0 +1,13 @@ +// This statement loads the getCardValue function you wrote in the implement directory. +// We will use the same function, but write tests for it using Jest in this file. +const getCardValue = require("../implement/3-get-card-value"); + +test("should return 11 for Ace of Spades", () => { + const aceofSpades = getCardValue("A♠"); + expect(aceofSpades).toEqual(11); +}); + +// Case 2: Handle Number Cards (2-10): +// Case 3: Handle Face Cards (J, Q, K): +// Case 4: Handle Ace (A): +// Case 5: Handle Invalid Cards: diff --git a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js deleted file mode 100644 index d61254bd7..000000000 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js +++ /dev/null @@ -1,18 +0,0 @@ -function getAngleType(angle) { - if (angle === 90) return "Right angle"; - // replace with your completed function from key-implement - -} - - - - - - - - -// Don't get bogged down in this detail -// Jest uses CommonJS module syntax by default as it's quite old -// We will upgrade our approach to ES6 modules in the next course module, so for now -// we have just written the CommonJS module.exports syntax for you -module.exports = getAngleType; \ No newline at end of file diff --git a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js deleted file mode 100644 index 9836fe398..000000000 --- a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js +++ /dev/null @@ -1,6 +0,0 @@ -function isProperFraction(numerator, denominator) { - if (numerator < denominator) return true; - // add your completed function from key-implement here -} - -module.exports = isProperFraction; \ No newline at end of file diff --git a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js deleted file mode 100644 index 0d95d3736..000000000 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js +++ /dev/null @@ -1,5 +0,0 @@ -function getCardValue(card) { - // replace with your code from key-implement - return 11; -} -module.exports = getCardValue; \ No newline at end of file diff --git a/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js b/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js deleted file mode 100644 index 03a8e2f34..000000000 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js +++ /dev/null @@ -1,11 +0,0 @@ -const getCardValue = require("./3-get-card-value"); - -test("should return 11 for Ace of Spades", () => { - const aceofSpades = getCardValue("A♠"); - expect(aceofSpades).toEqual(11); - }); - -// Case 2: Handle Number Cards (2-10): -// Case 3: Handle Face Cards (J, Q, K): -// Case 4: Handle Ace (A): -// Case 5: Handle Invalid Cards: diff --git a/Sprint-3/2-practice-tdd/README.md b/Sprint-3/2-practice-tdd/README.md new file mode 100644 index 000000000..ff28c131c --- /dev/null +++ b/Sprint-3/2-practice-tdd/README.md @@ -0,0 +1,13 @@ +# Practice TDD + +In this section you'll practice this key skill of building up your program test first. + +Use the Jest syntax and complete the provided files, meeting the acceptance criteria for each function. Use the VSCode test runner to run your tests and check your progress. + +Write the tests _before_ the code that will make them pass. + +Recommended order: + +1. `count.test.js` +1. `repeat.test.js` +1. `get-ordinal-number.test.js` diff --git a/Sprint-3/3-mandatory-practice/implement/count.js b/Sprint-3/2-practice-tdd/count.js similarity index 59% rename from Sprint-3/3-mandatory-practice/implement/count.js rename to Sprint-3/2-practice-tdd/count.js index fce249650..95b6ebb7d 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,5 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + return 5 } -module.exports = countChar; \ No newline at end of file +module.exports = countChar; diff --git a/Sprint-3/3-mandatory-practice/implement/count.test.js b/Sprint-3/2-practice-tdd/count.test.js similarity index 100% rename from Sprint-3/3-mandatory-practice/implement/count.test.js rename to Sprint-3/2-practice-tdd/count.test.js diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js new file mode 100644 index 000000000..f95d71db1 --- /dev/null +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -0,0 +1,5 @@ +function getOrdinalNumber(num) { + return "1st"; +} + +module.exports = getOrdinalNumber; diff --git a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js similarity index 88% rename from Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js rename to Sprint-3/2-practice-tdd/get-ordinal-number.test.js index 6d55dfbb4..dfe4b6091 100644 --- a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -9,5 +9,5 @@ const getOrdinalNumber = require("./get-ordinal-number"); // Then the function should return "1st" test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); - }); + expect(getOrdinalNumber(1)).toEqual("1st"); +}); diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js new file mode 100644 index 000000000..00e60d7f3 --- /dev/null +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -0,0 +1,5 @@ +function repeat() { + return "hellohellohello"; +} + +module.exports = repeat; diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js similarity index 89% rename from Sprint-3/3-mandatory-practice/implement/repeat.test.js rename to Sprint-3/2-practice-tdd/repeat.test.js index 8a4ab42ef..34097b09c 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -10,11 +10,11 @@ const repeat = require("./repeat"); // Then it should repeat the str count times and return a new string containing the repeated str values. test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("hellohellohello"); - }); + const str = "hello"; + const count = 3; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hellohellohello"); +}); // case: handle Count of 1: // Given a target string str and a count equal to 1, diff --git a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js deleted file mode 100644 index 24f528b0d..000000000 --- a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js +++ /dev/null @@ -1,5 +0,0 @@ -function getOrdinalNumber(num) { - return "1st"; -} - -module.exports = getOrdinalNumber; \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.js b/Sprint-3/3-mandatory-practice/implement/repeat.js deleted file mode 100644 index 621f9bd35..000000000 --- a/Sprint-3/3-mandatory-practice/implement/repeat.js +++ /dev/null @@ -1,5 +0,0 @@ -function repeat() { - return "hellohellohello"; -} - -module.exports = repeat; \ No newline at end of file diff --git a/Sprint-3/3-stretch/README.md b/Sprint-3/3-stretch/README.md new file mode 100644 index 000000000..8f01227bf --- /dev/null +++ b/Sprint-3/3-stretch/README.md @@ -0,0 +1,9 @@ +# 🔍 Stretch + +These stretch activities are not mandatory, but we hope you will explore them after you have completed the mandatory work. + +In this exercise, you'll need to **play computer** with the function `find`. This function makes use of while loop statement. Your task will be to step through the code to figure out what is happening when the computer executes the code. + +Next, try implementing the functions specified in `password-validator.js`. + +Finally, set up your own script and test files for `card-validator.md` diff --git a/Sprint-3/4-stretch-investigate/card-validator.md b/Sprint-3/3-stretch/card-validator.md similarity index 100% rename from Sprint-3/4-stretch-investigate/card-validator.md rename to Sprint-3/3-stretch/card-validator.md diff --git a/Sprint-3/4-stretch-investigate/find.js b/Sprint-3/3-stretch/find.js similarity index 100% rename from Sprint-3/4-stretch-investigate/find.js rename to Sprint-3/3-stretch/find.js diff --git a/Sprint-3/4-stretch-investigate/password-validator.js b/Sprint-3/3-stretch/password-validator.js similarity index 100% rename from Sprint-3/4-stretch-investigate/password-validator.js rename to Sprint-3/3-stretch/password-validator.js diff --git a/Sprint-3/4-stretch-investigate/password-validator.test.js b/Sprint-3/3-stretch/password-validator.test.js similarity index 100% rename from Sprint-3/4-stretch-investigate/password-validator.test.js rename to Sprint-3/3-stretch/password-validator.test.js diff --git a/Sprint-3/readme.md b/Sprint-3/readme.md index 4cbb096fb..491afb81c 100644 --- a/Sprint-3/readme.md +++ b/Sprint-3/readme.md @@ -8,66 +8,9 @@ > There is often a step by step video you can code along with too. > Do the prep. -## 🔧 1 Implement - -In the `implement` directory you've got a number of functions you'll need to implement. -For each function, you also have a number of different cases you'll need to check for your function. - -Write your assertions and build up your program case by case. Don't rush to a solution. The point of these assignments is to learn how to write assertions and build up a program step by step. - -Here is a recommended order: - -1. `1-get-angle-type.js` -1. `2-is-proper-fraction.js` -1. `3-get-card-value.js` - -## 🔧 2 Rewrite - -`console.log` is most often used as a debugging tool. We use to inspect the state of our program during runtime. - -We can use `console.assert` to write assertions: however, it is not very easy to use when writing large test suites. In the first section, Implement, we used a custom "helper function" to make our assertions more readable. - -Jest is a whole library of helper functions we can use to make our assertions more readable and easier to write. - -Your new task is to _rewrite_ the assertions from `./1-key-implement` using Jest syntax. -Blank files have been created for you. Each script file has a paired `.test.js` file. Write your tests in the test file and the implementation in the script file. - -You can run all the tests in this repo by running `npm test` in your terminal. However, VSCode has a built-in test runner that you can use to run the tests, and this should make it much easier to focus on building up your test cases one at a time. - -https://code.visualstudio.com/docs/editor/testing - -1. Go to 2-mandatory-rewrite/1-get-angle-type.test.js -1. Click the green play button to run the test. It's on the left of the test function in the gutter. -1. Read the output in the TEST_RESULTS tab at the bottom of the screen. -1. Explore all the tests in this repo by opening the TEST EXPLORER tab. The logo is a beaker. - -![VSCode Test Runner](../run-this-test.png) - -![Test Results](../test-results-output.png) - -> [!TIP] -> You can always run a single test file by running `npm test path/to/test-file.test.js`. - -## 3 Practice - -In this section you'll practice this key skill of building up your program test first. - -Use the Jest syntax and complete the provided files, meeting the acceptance criteria for each function. Use the VSCode test runner to run your tests and check your progress. - -Recommended order: - -1. `count.test.js` -1. `repeat.test.js` -1. `get-ordinal-number.test.js` - -## 🔍 4 Investigate Stretch - -These stretch activities are not mandatory, but we hope you will explore them after you have completed the key and mandatory work. - -In this exercise, you'll need to **play computer** with the function `find`. This function makes use of while loop statement. Your task will be to step through the code to figure out what is happening when the computer executes the code. - -Next, try implementing the functions specified in `password-validator.js`. - -Finally, set up your own script and test files for `card-validator.md` - +This sprint you are expected to produce multiple different pull requests: +1. One pull request for the `1-implement-and-rewrite-tests` directory. +2. One pull request for the `2-practice-tdd` directory. +3. Optionally, one pull request for the `3-stretch` directory. +Each directory contains a README.md file with instructions for that directory.