Skip to content
This repository was archived by the owner on Dec 9, 2021. It is now read-only.

Commit 74871ea

Browse files
committed
add tests for ShowsEffect
1 parent 69816e4 commit 74871ea

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"generate-template-files": "2.2.1",
9090
"gh-pages": "2.2.0",
9191
"husky": "4.2.3",
92+
"nock": "12.0.3",
9293
"node-sass": "4.13.1",
9394
"prettier": "1.19.1",
9495
"pretty-quick": "2.0.1",

Diff for: src/stores/shows/ShowsEffect.spec.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import ShowsEffect from './ShowsEffect';
2+
import nock from 'nock';
3+
import axios from 'axios';
4+
import environment from 'environment';
5+
import ShowModel from './models/shows/ShowModel';
6+
import StringUtil from '../../utilities/StringUtil';
7+
import EpisodeModel from './models/episodes/EpisodeModel';
8+
import CastModel from './models/cast/CastModel';
9+
10+
axios.defaults.adapter = require('axios/lib/adapters/http');
11+
12+
describe('ShowsEffect', () => {
13+
describe('requestShow', () => {
14+
it('has a successful response', async () => {
15+
const showId = '74';
16+
const endpoint = environment.api.shows.replace(':showId', showId);
17+
const [baseUrl, sourceUrl] = StringUtil.splitBySeparator(endpoint, '.com');
18+
19+
const scope = nock(baseUrl)
20+
.get(sourceUrl)
21+
.reply(200, { name: 'Robert' });
22+
23+
const actualResult = await ShowsEffect.requestShow(showId);
24+
25+
expect(actualResult).toBeInstanceOf(ShowModel);
26+
expect(actualResult.name).toEqual('Robert');
27+
28+
// Assert that the expected request was made.
29+
scope.done();
30+
});
31+
});
32+
33+
describe('requestEpisodes', () => {
34+
it('has a successful response', async () => {
35+
const showId = '74';
36+
const endpoint = environment.api.episodes.replace(':showId', showId);
37+
const [baseUrl, sourceUrl] = StringUtil.splitBySeparator(endpoint, '.com');
38+
39+
const scope = nock(baseUrl)
40+
.get(sourceUrl)
41+
.reply(200, [{ summary: 'Robert is cool' }]);
42+
43+
const actualResult = await ShowsEffect.requestEpisodes(showId);
44+
45+
expect(actualResult[0]).toBeInstanceOf(EpisodeModel);
46+
expect(actualResult[0].summary).toEqual('Robert is cool');
47+
48+
// Assert that the expected request was made.
49+
scope.done();
50+
});
51+
});
52+
53+
describe('requestCast', () => {
54+
it('has a successful response', async () => {
55+
const showId = '74';
56+
const endpoint = environment.api.cast.replace(':showId', showId);
57+
const [baseUrl, sourceUrl] = StringUtil.splitBySeparator(endpoint, '.com');
58+
59+
const scope = nock(baseUrl)
60+
.get(sourceUrl)
61+
.reply(200, [{ self: true }]);
62+
63+
const actualResult = await ShowsEffect.requestCast(showId);
64+
65+
expect(actualResult[0]).toBeInstanceOf(CastModel);
66+
expect(actualResult[0].self).toBe(true);
67+
68+
// Assert that the expected request was made.
69+
scope.done();
70+
});
71+
});
72+
});

Diff for: src/utilities/StringUtil.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default class StringUtil {
2+
/**
3+
* Splits a string in half by a separator
4+
*
5+
* @param str string
6+
* @param separator string
7+
* @example
8+
* splitBySeparator('https://api.tvmaze.com/search/shows?q=Friends', '.com');
9+
*
10+
* // ['https://api.tvmaze.com', '/search/shows?q=Friends']
11+
*/
12+
static splitBySeparator = (str, separator) => {
13+
return str.split(new RegExp(`(.*?${separator})`, 'g')).filter(Boolean);
14+
};
15+
}

Diff for: yarn.lock

+16-1
Original file line numberDiff line numberDiff line change
@@ -6672,7 +6672,7 @@ json-stable-stringify@^1.0.1:
66726672
dependencies:
66736673
jsonify "~0.0.0"
66746674

6675-
json-stringify-safe@~5.0.1:
6675+
json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
66766676
version "5.0.1"
66776677
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
66786678
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=
@@ -7464,6 +7464,16 @@ no-case@^3.0.3:
74647464
lower-case "^2.0.1"
74657465
tslib "^1.10.0"
74667466

7467+
7468+
version "12.0.3"
7469+
resolved "https://registry.yarnpkg.com/nock/-/nock-12.0.3.tgz#83f25076dbc4c9aa82b5cdf54c9604c7a778d1c9"
7470+
integrity sha512-QNb/j8kbFnKCiyqi9C5DD0jH/FubFGj5rt9NQFONXwQm3IPB0CULECg/eS3AU1KgZb/6SwUa4/DTRKhVxkGABw==
7471+
dependencies:
7472+
debug "^4.1.0"
7473+
json-stringify-safe "^5.0.1"
7474+
lodash "^4.17.13"
7475+
propagate "^2.0.0"
7476+
74677477
74687478
version "0.9.0"
74697479
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.9.0.tgz#d624050edbb44874adca12bb9a52ec63cb782579"
@@ -9079,6 +9089,11 @@ prop-types@^15.6.1, prop-types@^15.6.2, prop-types@^15.7.2:
90799089
object-assign "^4.1.1"
90809090
react-is "^16.8.1"
90819091

9092+
propagate@^2.0.0:
9093+
version "2.0.1"
9094+
resolved "https://registry.yarnpkg.com/propagate/-/propagate-2.0.1.tgz#40cdedab18085c792334e64f0ac17256d38f9a45"
9095+
integrity sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==
9096+
90829097
proxy-addr@~2.0.5:
90839098
version "2.0.6"
90849099
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.6.tgz#fdc2336505447d3f2f2c638ed272caf614bbb2bf"

0 commit comments

Comments
 (0)