Skip to content

Commit

Permalink
Move from blanket+mocha to jest (#206)
Browse files Browse the repository at this point in the history
- Use jest as the test runner
- Remove should
- Remove grunt
  • Loading branch information
JMPerez committed Apr 28, 2018
1 parent 36077be commit d1838c7
Show file tree
Hide file tree
Showing 21 changed files with 4,599 additions and 2,693 deletions.
4 changes: 2 additions & 2 deletions .gitignore
@@ -1,2 +1,2 @@
/node_modules
coverage.html
node_modules
coverage
9 changes: 5 additions & 4 deletions .npmignore
@@ -1,5 +1,6 @@
/node_modules
__mocks__
__tests__
node_modules
.coveralls.yml
.npmignore
*.log
/test
Gruntfile.js
.npmignore
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -3,6 +3,5 @@ node_js:
- 'node'
- '7'
- '6'
- '5.11'
after_success:
- npm run coveralls
- npm run travis
28 changes: 0 additions & 28 deletions Gruntfile.js

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -66,6 +66,7 @@ It includes helper functions to do the following:
* Check if users are following a Playlist

#### Player

* Get a user's available devices
* Get information about the user's current playback
* Transfer a user's playback
Expand All @@ -77,7 +78,6 @@ It includes helper functions to do the following:
* Set volume
* Seek playback to a given position


All methods require authentication, which can be done using these flows:

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

### Running tests

You can run the unit tests executing `mocha` and get a test coverage report running `mocha -r blanket -R html-cov > coverage.html`.
You can run the unit tests executing `npm test` and get a test coverage report running `npm test -- --coverage`.
81 changes: 81 additions & 0 deletions __mocks__/superagent.js
@@ -0,0 +1,81 @@
'use strict';

//mock for superagent - __mocks__/superagent.js

var mockDelay;
var mockError;
var mockResponse = {
status() {
return 200;
},
ok() {
return true;
},
get: jest.genMockFunction(),
toError: jest.genMockFunction()
};

var Request = {
put() {
return this;
},
del() {
return this;
},
post() {
return this;
},
get() {
return this;
},
send() {
return this;
},
query() {
return this;
},
field() {
return this;
},
set() {
return this;
},
accept() {
return this;
},
timeout() {
return this;
},
end: jest.genMockFunction().mockImplementation(function(callback) {
if (mockDelay) {
this.delayTimer = setTimeout(callback, 0, mockError, mockResponse);
return;
}

callback(mockError, mockResponse);
}),
//expose helper methods for tests to set
__setMockDelay(boolValue) {
mockDelay = boolValue;
},
__setMockResponse(mockRes) {
mockResponse = mockRes;
},
__setMockError(mockErr) {
mockError = mockErr;
},
__reset() {
this.__setMockResponse({
status() {
return 200;
},
ok() {
return true;
}
});
this.__setMockError(null);
this.__setMockDelay(false);
}
};

module.exports = Request;
29 changes: 29 additions & 0 deletions __tests__/authentication-request.js
@@ -0,0 +1,29 @@
var AuthenticationRequest = require('../src/authentication-request');

describe('Create Authentication Requests', () => {
test('Should use default settings if none are supplied', () => {
var request = AuthenticationRequest.builder().build();

expect(request.getHost()).toBe('accounts.spotify.com');
expect(request.getPort()).toBe(443);
expect(request.getScheme()).toBe('https');
expect(request.getHeaders()).toBeFalsy();
expect(request.getPath()).toBeFalsy();
expect(request.getQueryParameters()).toBeFalsy();
expect(request.getBodyParameters()).toBeFalsy();
});

test('Can overwrite one of the default parameters', () => {
var request = AuthenticationRequest.builder()
.withHost('such.host.wow')
.build();

expect(request.getHost()).toBe('such.host.wow');
expect(request.getPort()).toBe(443);
expect(request.getScheme()).toBe('https');
expect(request.getHeaders()).toBeFalsy();
expect(request.getPath()).toBeFalsy();
expect(request.getQueryParameters()).toBeFalsy();
expect(request.getBodyParameters()).toBeFalsy();
});
});
182 changes: 182 additions & 0 deletions __tests__/base-request.js
@@ -0,0 +1,182 @@
var Request = require('../src/base-request');

describe('Create Requests', () => {
test('Should create host, port, and scheme', () => {
var request = Request.builder()
.withHost('such.api.wow')
.withPort(1337)
.withScheme('http')
.build();

expect(request.getHost()).toBe('such.api.wow');
expect(request.getPort()).toBe(1337);
expect(request.getScheme()).toBe('http');
});

test('Should add query parameters', () => {
var request = Request.builder()
.withHost('such.api.wow')
.withPort(1337)
.withScheme('http')
.withQueryParameters({
oneParameter: 1,
anotherParameter: true,
thirdParameter: 'hello'
})
.build();

expect(request.getQueryParameters().oneParameter).toBe(1);
expect(request.getQueryParameters().anotherParameter).toBe(true);
expect(request.getQueryParameters().thirdParameter).toBe('hello');
});

test('Should add query parameters (multiple calls)', () => {
var request = Request.builder()
.withHost('such.api.wow')
.withPort(1337)
.withScheme('http')
.withQueryParameters({
oneParameter: 1,
anotherParameter: true
})
.withQueryParameters({
thirdParameter: 'hello'
})
.build();

expect(request.getQueryParameters().oneParameter).toBe(1);
expect(request.getQueryParameters().anotherParameter).toBe(true);
expect(request.getQueryParameters().thirdParameter).toBe('hello');
});

test('Should add query parameters (combine calls)', () => {
var request = Request.builder()
.withHost('such.api.wow')
.withPort(1337)
.withScheme('http')
.withQueryParameters(
{
oneParameter: 1,
anotherParameter: true
},
{
thirdParameter: 'hello'
}
)
.build();

expect(request.getQueryParameters().oneParameter).toBe(1);
expect(request.getQueryParameters().anotherParameter).toBe(true);
expect(request.getQueryParameters().thirdParameter).toBe('hello');
});

test('Should add body parameters', () => {
var request = Request.builder()
.withHost('such.api.wow')
.withPort(1337)
.withScheme('http')
.withBodyParameters({
one: 1,
two: true,
three: 'world'
})
.build();

expect(request.getBodyParameters().one).toBe(1);
expect(request.getBodyParameters().two).toBe(true);
expect(request.getBodyParameters().three).toBe('world');
});

test('Should add array to body parameters', () => {
var request = Request.builder()
.withHost('such.api.wow')
.withPort(1337)
.withScheme('http')
.withBodyParameters(['3VNWq8rTnQG6fM1eldSpZ0'])
.build();

expect(request.getBodyParameters()).toEqual(['3VNWq8rTnQG6fM1eldSpZ0']);
});

test('Should add header parameters', () => {
var request = Request.builder()
.withHost('such.api.wow')
.withPort(1337)
.withScheme('http')
.withHeaders({
Authorization: 'Basic WOOP',
'Content-Type': 'application/lol'
})
.build();

expect(request.getHeaders().Authorization).toBe('Basic WOOP');
expect(request.getHeaders()['Content-Type']).toBe('application/lol');
});

test('Should add path', () => {
var request = Request.builder()
.withHost('such.api.wow')
.withPort(1337)
.withPath('/v1/users/meriosweg')
.build();

expect(request.getPath()).toBe('/v1/users/meriosweg');
});

test('Should build URI', () => {
var request = Request.builder()
.withHost('such.api.wow')
.withScheme('https')
.withPort(1337)
.withPath('/v1/users/meriosweg')
.build();

expect(request.getURI()).toBe(
'https://such.api.wow:1337/v1/users/meriosweg'
);
});

test('Should construct empty query paramaters string', () => {
var request = Request.builder()
.withQueryParameters({})
.build();

expect(request.getQueryParameterString()).toBeFalsy();
});

test('Should construct query paramaters string for one parameter', () => {
var request = Request.builder()
.withQueryParameters({
one: 1
})
.build();

expect(request.getQueryParameterString()).toBe('?one=1');
});

test('Should construct query paramaters string for multiple parameters', () => {
var request = Request.builder()
.withQueryParameters({
one: 1,
two: true,
three: 'world'
})
.build();

expect(request.getQueryParameterString()).toBe(
'?one=1&two=true&three=world'
);
});

test('Should construct query paramaters string and exclude undefined values', () => {
var request = Request.builder()
.withQueryParameters({
one: 1,
two: undefined,
three: 'world'
})
.build();

expect(request.getQueryParameterString()).toBe('?one=1&three=world');
});
});

0 comments on commit d1838c7

Please sign in to comment.