Skip to content

Commit c6363c6

Browse files
committed
Update requirements
1 parent 2817622 commit c6363c6

File tree

5 files changed

+107
-172
lines changed

5 files changed

+107
-172
lines changed

app/Book.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/catalogue_service.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,38 @@ const catalogue = [
1919
"Great Expectations by Charles Dickens (1)",
2020
"The Blind Assassin by Margaret Atwood (8)",
2121
"Why Be Happy When You Could Be Normal? by Jeanette Winterson (19)",
22-
"The Origin of Species by Charles Darwin (50)",
22+
"The Origin of Species by Charles Darwin (50)"
2323
];
2424

25-
function checkBook(title) {
26-
if (!title) throw new Error("Please provide a title");
25+
function countBooksByAuthor(author) {
2726
// Your code here
2827
}
2928

30-
function countBooksByKeyword(keyword) {
31-
if (!keyword) throw new Error("Please provide a keyword");
29+
function checkBookByTitle(title) {
3230
// Your code here
3331
}
3432

35-
function getBooksByAuthor(author) {
36-
if (!author) throw new Error("Please provide an author");
33+
function countBooksByFirstLetter(letter) {
3734
// Your code here
3835
}
3936

40-
function getStockCount(title) {
41-
if (!title) throw new Error("Please provide a title");
37+
function getQuantity(title) {
38+
// Your code here
39+
}
40+
41+
function getBooksByAuthor(author) {
4242
// Your code here
4343
}
4444

45-
function stockReview(title) {
46-
if (!title) throw new Error("Please provide a title");
45+
function checkQuantity(title, quantity) {
4746
// Your code here
4847
}
4948

5049
module.exports = {
51-
checkBook,
52-
countBooksByKeyword,
50+
countBooksByAuthor,
51+
checkBookByTitle,
52+
countBooksByFirstLetter,
53+
getQuantity,
5354
getBooksByAuthor,
54-
getStockCount,
55-
stockReview
55+
checkQuantity
5656
};

docs/TASKS.md

Lines changed: 87 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -52,115 +52,142 @@ const catalogue = [
5252

5353
Update **every** book in the array and save the file.
5454

55-
## 3) Implement the catalogue functions
55+
## 3) Run the tests for the `countBooksByAuthor` function
5656

57-
Time to implement the 5 catalogue functions. You have already implemented these earlier in the week, in [programming_fundamentals_002](https://github.com/techreturners/programming_fundamentals_002), but now you need to make sure all the functions work with an _array of objects_ instead of an array of strings.
57+
The tests for `countBooksByAuthor` are already written.
5858

59-
Tests are already written for the `checkBook` function but you will need to write your own tests for the other functions.
59+
Run `npm test` in the terminal to run them. See that they fail.
6060

61-
This is likely to take you some time.
61+
In these tests, we have written **three expectations** in the same test, as 3 examples of the same behaviour. All 3 expectations must pass for the test to pass:
6262

63-
Feel free to refer to your previous work for help, and to the [Tasks for programming_fundamentals_002](https://github.com/techreturners/programming_fundamentals_002/blob/master/docs/TASKS.md) if you need a reminder of how the Catalogue Service functions should behave.
63+
```javascript
64+
describe("catalogueService.countBooksByAuthor", () => {
65+
test("returns the total number of books written by the given author", () => {
66+
expect(catalogueService.countBooksByAuthor("Hilary Mantel")).toBe(5);
67+
expect(catalogueService.countBooksByAuthor("Celeste Ng")).toBe(2);
68+
expect(catalogueService.countBooksByAuthor("Charles Dickens")).toBe(3);
69+
});
70+
});
71+
```
6472

65-
## 4) A Book Class
73+
## 4) Implement the `countBooksByAuthor` function
6674

67-
Currently you have created individual book objects withou the use of a class:
75+
This function should return the total number of books in the catalogue written by the given author.
6876

69-
```javascript
70-
const catalogue = [
71-
{title: "The Catcher in the Rye", author: "J.D. Salinger", quantity: 10},
72-
{title: "Dracula", author: "Bram Stoker", quantity: 0},
73-
{title: "Between the Assassinations", author: "Aravind Adiga", quantity: 9},
74-
...
75-
];
76-
```
77+
For example, there are 5 books by Hilary Mantel and 1 by Celeste Ng.
7778

78-
However, we might want to create a Class of `Book` to make it easier to create new books that follow a common blueprint, and to encapsulate common behaviour.
79+
Go to the `app/catalogue_service.js` file and implement the function so that the tests pass.
7980

80-
Open the file `Book.js` in the `/app` directory and take a look at the class defined in there.
81+
## 5) Write a test for the `checkBookByTitle` function
8182

82-
Now open `test/Book.test.js` and look at the tests which have been written for the book class.
83+
This function receives a title as an argument and should return `true` if a book with this title exists in the array, and `false` otherwise.
8384

84-
The first test should be passing, but all the other tests are disabled, with a little `x` at the beginning of the `describe` block:
85+
E.g.
8586

8687
```javascript
87-
xdescribe("addStock()", () => {
88-
// tests here
89-
});
90-
88+
checkBookByTitle("The Origin of Species"); // true
89+
checkBookByTitle("The Chronicles of Narnia"); // false
9190
```
9291

93-
Remove the `x` from the beginning of the `addStock()` tests and run `npm test` in the terminal to run the next set of tests too.
92+
Your test suite now may look as follows:
9493

9594
```javascript
96-
describe("addStock()", () => {
97-
// tests here
95+
describe("catalogueService", () => {
96+
describe("catalogueService.countBooksByAuthor", () => {
97+
test("returns the total number of books written by the given author", () => {
98+
expect(catalogueService.countBooksByAuthor("Hilary Mantel")).toBe(5);
99+
expect(catalogueService.countBooksByAuthor("Celeste Ng")).toBe(2);
100+
expect(catalogueService.countBooksByAuthor("Charles Dickens")).toBe(3);
101+
});
102+
});
103+
104+
describe("catalogueService.checkBookByTitle", () => {
105+
test("returns true if the book exists", () => {
106+
expect(
107+
catalogueService.checkBookByTitle(
108+
"The Assassination of Margaret Thatcher"
109+
)
110+
).toBe(5);
111+
});
112+
});
98113
});
99114
```
100115

101-
You should see the following error:
102-
103-
```
104-
TypeError: book.addStock is not a function
105-
```
116+
## 6) Implement the `checkBookByTitle` function
106117

107-
## 5) Create an addStock method for a book
118+
Go to the `app/catalogue_service.js` file and implement the function so that the tests you have just written pass.
108119

109-
Create an `addStock` method inside the `Book` class.
120+
## 7) The other case
110121

111-
This method should take an argument of a number, and increase the number of books in stock by this number.
122+
Write another test that ensures the function `checkBookByTitle` returns false when the book does not exists.
112123

113-
Run the tests again to ensure that they all pass.
124+
Run this test and see whether it passes or fails.
114125

115-
## 6) Un-x the next test
126+
If it fails, you'll need to update your function to ensure it returns `false` if the book is not found.
116127

117-
Now un-x the next test which tests the `removeStock()` method.
128+
## 8) The `countBooksByFirstLetter` function
118129

119-
Run the tests and see that they fail.
130+
This function should receive a letter as an argument (e.g. "H") and return the total number of books that begin with this letter. Note that the quantity is not relevant here. For example:
120131

121-
## 7) Create a removeStock method for a book
132+
```javascript
133+
countBooksByFirstLetter("W"); // returns 2
134+
```
122135

123-
Create a `removeStock` method inside the `Book` class.
136+
This returns 2 because Wolf Hall and Why Be Happy When You Could Be Normal? both begin with W.
124137

125-
This method should take an argument of a number, and decrease the number of books in stock by this number.
138+
Begin by writing a test, as always, before implementing the function.
126139

127-
Note that the number of books in stock cannot fall below 0.
140+
## 9) A lowercase letter
128141

129-
Run the tests again to ensure that they all pass.
142+
What if the `countBooksByFirstLetter` function is given a lowercase letter, e.g. `countBooksByFirstLetter("h")`?
130143

131-
## 8) An array of Book Objects
144+
It should still count the books beginning with H, ignoring the case of the letter.
132145

133-
Now we should be able to use our `Book` class to create the book objects that are populating our `catalogue` array.
146+
Write a test for this functionality, and if the test doesn't pass, update you function so that it passes.
134147

135-
First, we need to **require** the Book class into the file where we're going to use it.
148+
## 10) The `getQuantity` function
136149

137-
Add this line to the top of the `catalogue_service.js` file:
150+
This function should receive a title as an argument (e.g. "The Origin of Species") and return the quantity of this item which is in stock. For example:
138151

139152
```javascript
140-
const Book = require("./Book");
153+
getQuantity("A Place of Greater Safety"); // returns 11
141154
```
142155

143-
Then, update each of the objects in the `catalogue` array to be new Book instances:
156+
Begin by writing a test, as always, before implementing the function.
157+
158+
## 11) The `getBooksByAuthor` function
159+
160+
This function should receive an author as an argument (e.g. "Robert Bolaño") and return an array of books. For example:
144161

145162
```javascript
146-
const catalogue = [
147-
new Book("The Catcher in the Rye", "J.D. Salinger", 10),
148-
new Book("Dracula", "Bram Stoker", 0),
149-
new Book("Between the Assassinations", "Aravind Adiga", 9),
150-
...
163+
getBooksByAuthor("Robert Bolaño");
164+
165+
// Returns:
166+
[
167+
{ title: "2666", author: "Robert Bolaño", quantity: 12 },
168+
{ title: "By Night In Chile", author: "Robert Bolaño", quantity: 8 }
151169
];
152170
```
153171

154-
Update **all** of your book objects so they look like the above.
172+
Begin by writing a test, as always, before implementing the function.
155173

156-
Your tests should all still pass at this point.
174+
Remember that when testing an array return value you should use `toEqual` and not `toBe` (see yesterday's work for a reminder)
175+
176+
## 12) The `checkQuantity` function
177+
178+
This function should receive a title and a quantity as an argument (e.g. "By Night In Chile" and 4) and return `true` if there are at least as many books in stock as the given quantity, and false otherwise. For example:
179+
180+
```javascript
181+
checkQuantity("By Night In Chile", 4); // true
182+
checkQuantity("By Night In Chile", 100); // false
183+
```
184+
185+
Begin by writing a test, as always, before implementing the function.
157186

158187
## Written Questions
159188

160189
1. If a book is an object, what is its encapsulated data?
161190

162-
2. Why do we create a new Book for every single test in `Book.test.js`?
163-
164-
3. What data, of a book's encapsulated data, is changeable?
191+
2. What data, of a book's encapsulated data, might be changeable?
165192

166-
4. Why is representing a book as an object a better idea than representing it as a string?
193+
3. Why is representing a book as an object a better idea than representing it as a string?

test/Book.test.js

Lines changed: 0 additions & 73 deletions
This file was deleted.

test/catalogue_service.test.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
const catalogueService = require("../app/catalogue_service");
22

33
describe("catalogueService", () => {
4-
describe("catalogueService.checkBook", () => {
5-
test("returns true when a book is found by title", () => {
6-
expect(catalogueService.checkBook("Great Expectations")).toBe(true);
7-
});
8-
9-
test("returns false when a book cannot be found by title", () => {
10-
expect(catalogueService.checkBook("Gone With The Wind")).toBe(false);
11-
});
12-
13-
test("returns true when a partial match is found", () => {
14-
expect(catalogueService.checkBook("Expectations")).toBe(true);
15-
});
16-
17-
test("returns false when not even a partial match is found", () => {
18-
expect(catalogueService.checkBook("The Wind")).toBe(false);
4+
describe("catalogueService.countBooksByAuthor", () => {
5+
test("returns the total number of books written by the given author", () => {
6+
expect(catalogueService.countBooksByAuthor("Hilary Mantel")).toBe(5);
7+
expect(catalogueService.countBooksByAuthor("Celeste Ng")).toBe(2);
8+
expect(catalogueService.countBooksByAuthor("Charles Dickens")).toBe(3);
199
});
2010
});
2111
});

0 commit comments

Comments
 (0)