Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update instructions and test case description #2202

Merged
merged 7 commits into from
May 17, 2024
20 changes: 11 additions & 9 deletions exercises/concept/fruit-picker/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@

You are creating a new online portal for your patrons to order their fruit fresh from the grocer. The grocer has an API that you can use to see if they have the inventory desired by your customers. You need to create a small library of functions for interacting with the grocer's API.

## 1. Create a callback to be called when the order is successful
## 1. Notify your customer when their order was successful

Write a callback function called `onSuccess` to be called when the order is successful. It should invoke the imported `notify` function passing a success message to it.
The portal should notify your customer if their order was successful. Define the `onSuccess` callback function that will be called if the order was successful due to the grocer having enough fruit in stock. This function should invoke the imported `notify` function, passing `{ message: 'SUCCESS' }` to let your customer know that their order was successful.

```javascript
onSuccess();
// => `notify` called with `{ message: 'SUCCESS' }`
```

## 2. Create a callback to be called when the order fails with an error
## 2. Notify your customer when their order was unsuccessful

Write a callback function called `onError` to be called when the order encounters an error. It should invoke the imported `notify` function passing an error message to it.
The portal should notify your customer if their order was unsuccessful. Define the `onError` callback function that will be called if the order was unsuccessful because the grocer _does not have_ the fruit in stock or there was an error. This function should invoke the imported `notify` function, passing `{ message: 'ERROR' }` to let your customer know that their order was unsuccessful.

```javascript
onError();
// => `notify` called with `{ message: 'ERROR' }`
```

## 3. Create a wrapper to wrap the external api function
## 3. Create an API wrapper to wrap the grocer's API order function

The grocer's API provides a function to order from their inventory called `order`. It receives three arguments: a _query_, a _callback_ function to be invoked when the order is successful, and a _callback_ function to be invoked when the order encounters an error. You decide to wrap the api function call in a newly defined function `orderFromGrocer` to insulate your codebase from external changes. Your function should forward the arguments (which match the provided api function) to the api function.
Fruit orders are placed through the grocer's API via the provided `order` function. This function receives three arguments: a _query_, containing the `variety` and `quantity` of fruit requested, a _callback_ function to be invoked when the order is successful, and a _callback_ function to be invoked when the order encounters an error.

You want to insulate your codebase from potential external changes and decide to wrap the call to the `order` function inside a new function named `orderFromGrocer`. Implement the `orderFromGrocer` function that attempts to place an order via a call to the grocer's API `order` function, making sure to forward the arguments passed into `orderFromGrocer` to the API call.

The query takes the form of an _object_:

Expand All @@ -42,11 +44,11 @@ orderFromGrocer(
// => `order` was called with the query and the callbacks
```

## 4. Create a convenient short function
## 4. Simplify handling placed orders

You find that you are calling this function from many different places with the same functions. Seeing an opportunity to refactor your code, you want to create a function where you can supply the variety and quantity to order as arguments.
Your customers are now able to place fruit orders via your portal, however, you notice that you are invoking the `orderFromGrocer` function in many different places across your codebase, each time having to pass in a `query` and the two `callback` functions as arguments. Seeing an opportunity to refactor your code, you think it would be simpler if you could place an order by just passing the `variety` and `quantity` of fruit required. Define the `postOrder` helper function that takes `variety` and `quantity` as arguments and attempts to place an order with the grocer.

```javascript
postOrder('peach', 100);
// => order submitted for 100 peaches
// => order placed for 100 peaches
```
2 changes: 1 addition & 1 deletion exercises/concept/fruit-picker/fruit-picker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('task 2', () => {
});

describe('task 3', () => {
test('order from grocer passes callback function arguments forward', () => {
test('orderFromGrocer passes query and callback function arguments forward', () => {
const query = { variety: 'apple', quantity: 10 };
orderFromGrocer(query, onSuccess, onError);
expect(order).toHaveBeenCalledTimes(1);
Expand Down