Skip to content

Commit 386c707

Browse files
committed
outline step 9 tasks and tests
1 parent f3f1aa5 commit 386c707

File tree

17 files changed

+205
-54
lines changed

17 files changed

+205
-54
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,9 @@ Creating a "SORT_BY_POPULARITY" action.
123123
```js
124124
function sortByVotes(a, b) {
125125
switch(true) {
126-
case a.votes < b.votes:
127-
return 1;
128-
case a.votes > b.votes:
129-
return -1;
130-
default:
131-
return 0;
126+
case a.votes < b.votes: return 1;
127+
case a.votes > b.votes: return -1;
128+
default: return 0;
132129
}
133130
}
134131
```

coderoad.json

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,51 +486,88 @@
486486
},
487487
{
488488
"title": "Second Action",
489-
"description": "Creating a \"SORT_BY_POPULARITY\" action.\n\n```js\nfunction sortByVotes(a, b) {\n switch(true) {\n case a.votes < b.votes:\n return 1;\n case a.votes > b.votes:\n return -1;\n default:\n return 0;\n }\n }\n```\n\nSort pokemon by votes",
489+
"description": "Creating a \"SORT_BY_POPULARITY\" action.\n\n```js\nfunction sortByVotes(a, b) {\n switch(true) {\n case a.votes < b.votes: return 1;\n case a.votes > b.votes: return -1;\n default: return 0;\n }\n }\n```\n\nSort pokemon by votes",
490490
"tasks": [
491491
{
492-
"description": "create an action type for 'SORT_BY_POPULARITY'",
492+
"description": "in \"src/pokemon/index.js\", create an action type for 'SORT_BY_POPULARITY'",
493493
"tests": [
494494
"09/01"
495495
],
496496
"actions": [
497497
"open('src/pokemon/index.js')"
498+
],
499+
"hints": [
500+
"Try this: `const SORT_BY_POPULARITY = 'SORT_BY_POPULARITY';`"
498501
]
499502
},
500503
{
501-
"description": "create an action creator called 'sortByPopularity'",
504+
"description": "create the action creator called 'sortByPopularity' and export it",
502505
"tests": [
503506
"09/02"
507+
],
508+
"hints": [
509+
"It should be a function that returns `{ type: SORT_BY_POPULARITY }`",
510+
"Try this: `const sortByPopularity = () => ({ ... })`",
511+
"It should be exported using `export`"
504512
]
505513
},
506514
{
507-
"description": "dispatch a `sortByPopularity` action after the two voteUp dispatches",
515+
"description": "import `sortByPopularity` in \"src/index.js\".",
508516
"tests": [
509517
"09/03"
518+
],
519+
"hints": [
520+
"Try this: `import { sortByPopularity } from './pokemon';`"
510521
]
511522
},
512523
{
513-
"description": "add a `SORT_BY_POPULARITY` case that returns `pokemon.sort();`",
524+
"description": "in \"src/index.js\", dispatch a `sortByPopularity` action at the bottom of the page",
514525
"tests": [
515526
"09/04"
527+
],
528+
"hints": [
529+
"use `store.dispatch(actionCreator)`",
530+
"Try this: `store.dispatch(sortByPopularity())`"
516531
]
517532
},
518533
{
519-
"description": "create a sortByVotes function and pass it into the pokemon.sort function",
534+
"description": "add a `SORT_BY_POPULARITY` case that returns `pokemon.sort();`",
520535
"tests": [
521536
"09/05"
537+
],
538+
"hints": [
539+
"Try this: `case SORT_BY_POPULARITY: return pokemon.sort()`"
522540
]
523541
},
524542
{
525-
"description": "Make a `sortByKey` function, which is more reusable, by wrapping it in a function that takes a key",
543+
"description": "use the `sortByVotes` function and pass it into the `pokemon.sort` function. Sorting function return 1, -1, or 0 depending on where the index of b should be arranged around a.",
526544
"tests": [
527545
"09/06"
546+
],
547+
"hints": [
548+
"Try this: `case SORT_BY_POPULARITY: return pokemon.sort(sortByVotes)`"
549+
],
550+
"actions": [
551+
"insert('\nfunction sortByVotes(a, b) {\n switch(true) {\n case a.votes > b.votes:\n return 1;\n case a.votes < b.votes:\n return -1;\n default: return 0;\n }\n}\n\n')"
528552
]
529553
},
530554
{
531-
"description": "You've just created a **thunk** - a function that returns a function. Pass your function into the pokemon.sort() method and give it the key of 'votes'",
555+
"description": "Use the `sortByKey` function, which is more reusable. It should take a param of \"key\" and return a function similar to `sortByVotes`, but sorting on the \"key\" param.",
532556
"tests": [
533557
"09/07"
558+
],
559+
"actions": [
560+
"insert('\nfunction sortByKey(key) {\n return function(a, b) {\n\n // sorting cases here\n\n }\n}\n')"
561+
]
562+
},
563+
{
564+
"description": "You've just created a **thunk** - a function that returns a function. Pass your function into the `pokemon.sort()` method and give it the key of 'votes'",
565+
"tests": [
566+
"09/08"
567+
],
568+
"hints": [
569+
"Try this: `sortByKey('votes')`",
570+
"Try this: pokemon.sort(sortByKey('votes'))"
534571
]
535572
}
536573
],

tutorial/02/02.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
describe('02 createStore', () => {
22

3-
const createStore = indexJs.__get__('createStore');
4-
53
it('isn\'t imported. `import { createStore } from "redux";`', () => {
4+
const createStore = indexJs.__get__('createStore');
65
expect(createStore).to.not.be.undefined;
76
});
87

tutorial/06/03.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,4 @@ describe('03 defaultPokemon', () => {
1212
expect(defaultPokemon[0].name).to.equal('Luvdisc');
1313
});
1414

15-
// it('should be the default param for state in the pokemon reducer', () => {
16-
// expect(pokemon(undefined, { type: 'ANY' })).to.deep.equal(defaultPokemon);
17-
// });
18-
1915
});

tutorial/06/04.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
describe('04 pokemon reducer first param "state"', () => {
22

33
it('should have a default value of `defaultPokemon`', () => {
4-
4+
const regex = /=\s?defaultPokemon\s?,/;
5+
expect(pokemon.toString()).to.match(regex);
56
});
67

78
});

tutorial/06/07.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
describe('07 combineReducers', () => {
22

3-
const combineReducers = indexJs.__get__('combineReducers');
4-
53
it('should be loaded', () => {
4+
const combineReducers = indexJs.__get__('combineReducers');
65
expect(combineReducers).to.not.be.undefined;
76
});
87

tutorial/08/02.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ describe('02 createStore', () => {
22

33
it('should call applyMiddleware', () => {
44
const regex = /^[a-z]+\sstore\s?=.*applyMiddleware\(.+\)/m;
5-
expect(indexJs.__text__.to.match(regex);
5+
expect(indexJs.__text__).to.match(regex);
66
});
77

88
});

tutorial/08/05.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ describe('05 logger', () => {
77
});
88

99
it('should be set to `createLogger()`', () => {
10-
10+
expect(typeof logger).to.equal('function');
11+
expect(logger.toString()).to.match(/logEntry/);
1112
});
1213

1314
});

tutorial/09/01.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
const chai = require('chai');
2-
const spies = require('chai-spies');
3-
const expect = chai.expect;
4-
chai.use(spies);
1+
const { expect } = require('chai');
52

6-
let spy = chai.spy.on(console, 'log');
7-
8-
const pokemonIndexJs = require('BASE/pokemon/index.js');
3+
const indexJs = require('BASE/src/index.js');
4+
const pokemonIndexJs = require('BASE/src/pokemon/index.js');
95

106
describe('01 SORT_BY_POPULARITY action type', () => {
117

tutorial/09/02.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
describe('02 sortByPopularity action creator', () => {
22

33
const sortByPopularity = pokemonIndexJs.__get__('sortByPopularity');
4+
const action = { type: 'SORT_BY_POPULARITY' };
45

56
it('doesn\'t exist', () => {
67
expect(sortByPopularity).to.not.be.undefined;
78
});
89

9-
it('should equal the string "SORT_BY_POPULARITY"', () => {
10-
expect(sortByPopularity()).to.equal({ type: 'SORT_BY_POPULARITY' });
10+
it('should output an object with a type of "SORT_BY_POPULARITY"', () => {
11+
expect(sortByPopularity()).to.deep.equal(action);
12+
});
13+
14+
it('should be exported', () => {
15+
expect(require('BASE/src/pokemon/index.js').sortByPopularity())
16+
.to.deep.equal(action);
1117
});
1218

1319
});

tutorial/09/03.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
describe('03 dispatch sortByPopularity action', () => {
1+
describe('03 sortByPopularity', () => {
22

3-
3+
it('should be imported in "src/index.js"', () => {
4+
const sortByPopularity = indexJs.__get__('sortByPopularity');
5+
expect(sortByPopularity).to.not.be.undefined;
6+
expect(typeof sortByPopularity).to.equal('function');
7+
});
48

59
});

tutorial/09/04.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
describe('04 SORT_BY_POPULARITY reducer case', () => {
2-
1+
describe('04 sortByPopularity action', () => {
32

3+
it('should be dispatched', () => {
4+
const regex = /store.dispatch\s?\(\s?sortByPopularity\(\s?\)\s?)/;
5+
expect(indexJs.__text__).to.match(regex);
6+
});
47

58
});

tutorial/09/05.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
describe('05 sortByVotes function', () => {
1+
describe('05 pokemon reducer', () => {
22

3+
const pokemon = pokemonIndexJs.__get__('pokemon');
34

5+
it('has a SORT_BY_POPULARITY case', () => {
6+
const regex = /case\s?+SORT_BY_POPULARITY/;
7+
expect(pokemon.toString()).to.match(regex);
8+
});
9+
10+
it('case SORT_BY_POPULARITY should sort pokemon', () => {
11+
const regex = /pokemon.sort(.*)/;
12+
expect(pokemon.toString()).to.match(regex);
13+
});
414

515
});

tutorial/09/06.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
1-
describe('06 sortByKey function', () => {
1+
describe('06 sortByVotes function', () => {
22

3+
if (Number(process.env.TASK_POSITION) <= 5) {
34

5+
const sortByVotes = pokemonIndexJs.__get__('sortByVotes');
6+
7+
it('doesn\'t exist', () => {
8+
expect(sortByVotes).to.not.be.undefined;
9+
});
10+
11+
it('should be a function', () => {
12+
expect(typeof sortByVotes).to.equal('function');
13+
});
14+
15+
it('case SORT_BY_POPULARITY should sort pokemon', () => {
16+
const pokemon = pokemonIndexJs.__get__('pokemon');
17+
const regex = /pokemon.sort(.*sortBy.*)/;
18+
expect(pokemon.toString()).to.match(regex);
19+
});
20+
21+
it('should sort a list of objects with vote', () => {
22+
const list = [{votes: 3}, {votes: 1}, {votes: 2}];
23+
expect(list.sort(sortByVotes)).to.not.deep.equal(list);
24+
});
25+
26+
it('should sort a list in descending order', () => {
27+
const list = [{votes: 3}, {votes: 1}, {votes: 2}];
28+
const expected = [{votes: 3}, {votes: 2}, {votes: 1}];
29+
expect(list.sort(sortByVotes)).to.deep.equal(expected);
30+
});
31+
32+
} else {
33+
it('passes', () => expect(true).to.be(true));
34+
}
435

536
});

tutorial/09/07.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1-
describe('07 sorted pokemon', () => {
1+
describe('07 sortByKey function', () => {
22

3+
const sortByKey = pokemonIndexJs.__get__('sortByKey');
4+
5+
it('doesn\'t exist', () => {
6+
expect(sortByKey).to.not.be.undefined;
7+
});
8+
9+
it('should be a function', () => {
10+
expect(typeof sortByKey).to.equal('function');
11+
});
12+
13+
it('should take a key as a param', () => {
14+
expect(sortByKey).to.have.length(1);
15+
});
16+
17+
it('should sort a list of objects by a key', () => {
18+
const list = [{a: 3}, {a: 1}, {a: 2}];
19+
expect(list.sort(sortByKey('a'))).to.not.deep.equal(list);
20+
});
21+
22+
it('should sort a list in descending order', () => {
23+
const list = [{a: 3}, {a: 1}, {a: 2}];
24+
const expected = [{a: 3}, {a: 2}, {a: 1}];
25+
expect(list.sort(sortByKey('a'))).to.deep.equal(expected);
26+
});
327

428

529
});

tutorial/09/08.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
describe('08 pokemon case SORT_BY_POPULARITY', () => {
2+
3+
it('should call sortByKey with "votes" passed in', () => {
4+
const pokemon = pokemonIndexJs.__get__('pokemon');
5+
const regex = /pokemon.sort(.*sortByKey\s?\(["'`]votes["'`]\).*)/;
6+
expect(pokemon.toString()).to.match(regex);
7+
});
8+
9+
});

0 commit comments

Comments
 (0)