-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchAudiobook.test.ts
79 lines (73 loc) · 2.97 KB
/
searchAudiobook.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* eslint-disable max-len */
import nock from 'nock';
import { searchAudiobook } from '../../src/index';
import mockData from '../mock-answers/searchAudiobook.json';
const scope = nock('https://itunes.apple.com');
describe('Search Audiobook', () => {
beforeAll(() => {
nock.disableNetConnect();
});
afterAll(() => {
nock.enableNetConnect();
});
// eslint-disable-next-line jest/prefer-expect-assertions
it("should return results for an Audiobook called 'moby dick'", async () => {
scope.get('/search?term=moby%20dick&limit=1&country=US&language=en&entity=audiobook')
.reply(200, mockData);
const result = await searchAudiobook('moby dick', {
limit: 1,
timeout: 2000
});
expect(result.resultCount).toBe(1);
expect(result.results).toHaveLength(1);
});
it("should not return results for an Audiobook called 'EXPERIMENTAL_TERM_OF_SEARCH'", async () => {
scope.get('/search?term=EXPERIMENTAL_TERM_OF_SEARCH&limit=1&country=US&language=en&entity=audiobook')
.reply(200, {
"resultCount": 0,
"results": []
});
const result = await searchAudiobook('EXPERIMENTAL_TERM_OF_SEARCH');
expect(result.resultCount).toBe(0);
expect(result.results).toHaveLength(0);
});
describe("it validates if you're passing valid optional parameters", () => {
it("should throw exception when term is not a string, or empty string", async () => {
const exceptionMessage = 'A "term" is a string required on any search. "term" cannot have empty spaces as well.';
await expect(searchAudiobook('', {
limit: 3,
timeout: 2000,
}))
.rejects
.toHaveProperty(
'message', exceptionMessage
);
});
it('should throw exception when limit is not a number', async () => {
const exceptionMessage = 'Optional argument "limit" must be a number. Passed string(test)';
await expect(searchAudiobook('moby dick', { limit: "test" }))
.rejects
.toHaveProperty(
'message', exceptionMessage
);
});
it('should throw exception when language code is invalid', async () => {
const exceptionMessage = 'Optional argument "language" must be a string and should be a valid ISO 639-1 language code '
+ '(https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes). Passed string( "INVALID" )';
await expect(searchAudiobook('moby dick', { language:'INVALID' }))
.rejects
.toHaveProperty(
'message', exceptionMessage
);
});
it('should throw exception when country code is invalid', async () => {
const exceptionMessage = 'Optional argument "country" must be a string and should be a valid ISO 3166-1 Alpha 2 country code '
+ '(https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). Passed string( "INVALID" )';
await expect(searchAudiobook('moby dick', { country:'INVALID' }))
.rejects
.toHaveProperty(
'message', exceptionMessage
);
});
});
});