Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Sprint-3/1-implement-and-rewrite-tests/README.md
Original file line number Diff line number Diff line change
@@ -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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. Click the green play button to run the test. It's on the left of the test function in the gutter.
2. 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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. Read the output in the TEST_RESULTS tab at the bottom of the screen.
3. 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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. Explore all the tests in this repo by opening the TEST EXPLORER tab. The logo is a beaker.
4. 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`.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
18 changes: 0 additions & 18 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js

This file was deleted.

6 changes: 0 additions & 6 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js

This file was deleted.

5 changes: 0 additions & 5 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.js

This file was deleted.

11 changes: 0 additions & 11 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js

This file was deleted.

13 changes: 13 additions & 0 deletions Sprint-3/2-practice-tdd/README.md
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
return 5
}

module.exports = countChar;
module.exports = countChar;
5 changes: 5 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function getOrdinalNumber(num) {
return "1st";
}

module.exports = getOrdinalNumber;
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
5 changes: 5 additions & 0 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function repeat() {
return "hellohellohello";
}

module.exports = repeat;
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js

This file was deleted.

5 changes: 0 additions & 5 deletions Sprint-3/3-mandatory-practice/implement/repeat.js

This file was deleted.

9 changes: 9 additions & 0 deletions Sprint-3/3-stretch/README.md
Original file line number Diff line number Diff line change
@@ -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`
File renamed without changes.
67 changes: 5 additions & 62 deletions Sprint-3/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Loading