Skip to content

Commit d1838c7

Browse files
authored
Move from blanket+mocha to jest (#206)
- Use jest as the test runner - Remove should - Remove grunt
1 parent 36077be commit d1838c7

21 files changed

+4599
-2693
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/node_modules
2-
coverage.html
1+
node_modules
2+
coverage

.npmignore

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/node_modules
1+
__mocks__
2+
__tests__
3+
node_modules
4+
.coveralls.yml
5+
.npmignore
26
*.log
3-
/test
4-
Gruntfile.js
5-
.npmignore

.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ node_js:
33
- 'node'
44
- '7'
55
- '6'
6-
- '5.11'
76
after_success:
8-
- npm run coveralls
7+
- npm run travis

Gruntfile.js

-28
This file was deleted.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ It includes helper functions to do the following:
6666
* Check if users are following a Playlist
6767

6868
#### Player
69+
6970
* Get a user's available devices
7071
* Get information about the user's current playback
7172
* Transfer a user's playback
@@ -77,7 +78,6 @@ It includes helper functions to do the following:
7778
* Set volume
7879
* Seek playback to a given position
7980

80-
8181
All methods require authentication, which can be done using these flows:
8282

8383
* [Client credentials flow](http://tools.ietf.org/html/rfc6749#section-4.4) (Application-only authentication)
@@ -969,4 +969,4 @@ See something you think can be improved? [Open an issue](https://github.com/thel
969969

970970
### Running tests
971971

972-
You can run the unit tests executing `mocha` and get a test coverage report running `mocha -r blanket -R html-cov > coverage.html`.
972+
You can run the unit tests executing `npm test` and get a test coverage report running `npm test -- --coverage`.

__mocks__/superagent.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict';
2+
3+
//mock for superagent - __mocks__/superagent.js
4+
5+
var mockDelay;
6+
var mockError;
7+
var mockResponse = {
8+
status() {
9+
return 200;
10+
},
11+
ok() {
12+
return true;
13+
},
14+
get: jest.genMockFunction(),
15+
toError: jest.genMockFunction()
16+
};
17+
18+
var Request = {
19+
put() {
20+
return this;
21+
},
22+
del() {
23+
return this;
24+
},
25+
post() {
26+
return this;
27+
},
28+
get() {
29+
return this;
30+
},
31+
send() {
32+
return this;
33+
},
34+
query() {
35+
return this;
36+
},
37+
field() {
38+
return this;
39+
},
40+
set() {
41+
return this;
42+
},
43+
accept() {
44+
return this;
45+
},
46+
timeout() {
47+
return this;
48+
},
49+
end: jest.genMockFunction().mockImplementation(function(callback) {
50+
if (mockDelay) {
51+
this.delayTimer = setTimeout(callback, 0, mockError, mockResponse);
52+
return;
53+
}
54+
55+
callback(mockError, mockResponse);
56+
}),
57+
//expose helper methods for tests to set
58+
__setMockDelay(boolValue) {
59+
mockDelay = boolValue;
60+
},
61+
__setMockResponse(mockRes) {
62+
mockResponse = mockRes;
63+
},
64+
__setMockError(mockErr) {
65+
mockError = mockErr;
66+
},
67+
__reset() {
68+
this.__setMockResponse({
69+
status() {
70+
return 200;
71+
},
72+
ok() {
73+
return true;
74+
}
75+
});
76+
this.__setMockError(null);
77+
this.__setMockDelay(false);
78+
}
79+
};
80+
81+
module.exports = Request;

__tests__/authentication-request.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var AuthenticationRequest = require('../src/authentication-request');
2+
3+
describe('Create Authentication Requests', () => {
4+
test('Should use default settings if none are supplied', () => {
5+
var request = AuthenticationRequest.builder().build();
6+
7+
expect(request.getHost()).toBe('accounts.spotify.com');
8+
expect(request.getPort()).toBe(443);
9+
expect(request.getScheme()).toBe('https');
10+
expect(request.getHeaders()).toBeFalsy();
11+
expect(request.getPath()).toBeFalsy();
12+
expect(request.getQueryParameters()).toBeFalsy();
13+
expect(request.getBodyParameters()).toBeFalsy();
14+
});
15+
16+
test('Can overwrite one of the default parameters', () => {
17+
var request = AuthenticationRequest.builder()
18+
.withHost('such.host.wow')
19+
.build();
20+
21+
expect(request.getHost()).toBe('such.host.wow');
22+
expect(request.getPort()).toBe(443);
23+
expect(request.getScheme()).toBe('https');
24+
expect(request.getHeaders()).toBeFalsy();
25+
expect(request.getPath()).toBeFalsy();
26+
expect(request.getQueryParameters()).toBeFalsy();
27+
expect(request.getBodyParameters()).toBeFalsy();
28+
});
29+
});

__tests__/base-request.js

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
var Request = require('../src/base-request');
2+
3+
describe('Create Requests', () => {
4+
test('Should create host, port, and scheme', () => {
5+
var request = Request.builder()
6+
.withHost('such.api.wow')
7+
.withPort(1337)
8+
.withScheme('http')
9+
.build();
10+
11+
expect(request.getHost()).toBe('such.api.wow');
12+
expect(request.getPort()).toBe(1337);
13+
expect(request.getScheme()).toBe('http');
14+
});
15+
16+
test('Should add query parameters', () => {
17+
var request = Request.builder()
18+
.withHost('such.api.wow')
19+
.withPort(1337)
20+
.withScheme('http')
21+
.withQueryParameters({
22+
oneParameter: 1,
23+
anotherParameter: true,
24+
thirdParameter: 'hello'
25+
})
26+
.build();
27+
28+
expect(request.getQueryParameters().oneParameter).toBe(1);
29+
expect(request.getQueryParameters().anotherParameter).toBe(true);
30+
expect(request.getQueryParameters().thirdParameter).toBe('hello');
31+
});
32+
33+
test('Should add query parameters (multiple calls)', () => {
34+
var request = Request.builder()
35+
.withHost('such.api.wow')
36+
.withPort(1337)
37+
.withScheme('http')
38+
.withQueryParameters({
39+
oneParameter: 1,
40+
anotherParameter: true
41+
})
42+
.withQueryParameters({
43+
thirdParameter: 'hello'
44+
})
45+
.build();
46+
47+
expect(request.getQueryParameters().oneParameter).toBe(1);
48+
expect(request.getQueryParameters().anotherParameter).toBe(true);
49+
expect(request.getQueryParameters().thirdParameter).toBe('hello');
50+
});
51+
52+
test('Should add query parameters (combine calls)', () => {
53+
var request = Request.builder()
54+
.withHost('such.api.wow')
55+
.withPort(1337)
56+
.withScheme('http')
57+
.withQueryParameters(
58+
{
59+
oneParameter: 1,
60+
anotherParameter: true
61+
},
62+
{
63+
thirdParameter: 'hello'
64+
}
65+
)
66+
.build();
67+
68+
expect(request.getQueryParameters().oneParameter).toBe(1);
69+
expect(request.getQueryParameters().anotherParameter).toBe(true);
70+
expect(request.getQueryParameters().thirdParameter).toBe('hello');
71+
});
72+
73+
test('Should add body parameters', () => {
74+
var request = Request.builder()
75+
.withHost('such.api.wow')
76+
.withPort(1337)
77+
.withScheme('http')
78+
.withBodyParameters({
79+
one: 1,
80+
two: true,
81+
three: 'world'
82+
})
83+
.build();
84+
85+
expect(request.getBodyParameters().one).toBe(1);
86+
expect(request.getBodyParameters().two).toBe(true);
87+
expect(request.getBodyParameters().three).toBe('world');
88+
});
89+
90+
test('Should add array to body parameters', () => {
91+
var request = Request.builder()
92+
.withHost('such.api.wow')
93+
.withPort(1337)
94+
.withScheme('http')
95+
.withBodyParameters(['3VNWq8rTnQG6fM1eldSpZ0'])
96+
.build();
97+
98+
expect(request.getBodyParameters()).toEqual(['3VNWq8rTnQG6fM1eldSpZ0']);
99+
});
100+
101+
test('Should add header parameters', () => {
102+
var request = Request.builder()
103+
.withHost('such.api.wow')
104+
.withPort(1337)
105+
.withScheme('http')
106+
.withHeaders({
107+
Authorization: 'Basic WOOP',
108+
'Content-Type': 'application/lol'
109+
})
110+
.build();
111+
112+
expect(request.getHeaders().Authorization).toBe('Basic WOOP');
113+
expect(request.getHeaders()['Content-Type']).toBe('application/lol');
114+
});
115+
116+
test('Should add path', () => {
117+
var request = Request.builder()
118+
.withHost('such.api.wow')
119+
.withPort(1337)
120+
.withPath('/v1/users/meriosweg')
121+
.build();
122+
123+
expect(request.getPath()).toBe('/v1/users/meriosweg');
124+
});
125+
126+
test('Should build URI', () => {
127+
var request = Request.builder()
128+
.withHost('such.api.wow')
129+
.withScheme('https')
130+
.withPort(1337)
131+
.withPath('/v1/users/meriosweg')
132+
.build();
133+
134+
expect(request.getURI()).toBe(
135+
'https://such.api.wow:1337/v1/users/meriosweg'
136+
);
137+
});
138+
139+
test('Should construct empty query paramaters string', () => {
140+
var request = Request.builder()
141+
.withQueryParameters({})
142+
.build();
143+
144+
expect(request.getQueryParameterString()).toBeFalsy();
145+
});
146+
147+
test('Should construct query paramaters string for one parameter', () => {
148+
var request = Request.builder()
149+
.withQueryParameters({
150+
one: 1
151+
})
152+
.build();
153+
154+
expect(request.getQueryParameterString()).toBe('?one=1');
155+
});
156+
157+
test('Should construct query paramaters string for multiple parameters', () => {
158+
var request = Request.builder()
159+
.withQueryParameters({
160+
one: 1,
161+
two: true,
162+
three: 'world'
163+
})
164+
.build();
165+
166+
expect(request.getQueryParameterString()).toBe(
167+
'?one=1&two=true&three=world'
168+
);
169+
});
170+
171+
test('Should construct query paramaters string and exclude undefined values', () => {
172+
var request = Request.builder()
173+
.withQueryParameters({
174+
one: 1,
175+
two: undefined,
176+
three: 'world'
177+
})
178+
.build();
179+
180+
expect(request.getQueryParameterString()).toBe('?one=1&three=world');
181+
});
182+
});

0 commit comments

Comments
 (0)