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

Commit dee3a23

Browse files
authored
Merge pull request #103 from kaimallea/jest
Switch to Jest for testing
2 parents 888c7ba + 770f476 commit dee3a23

21 files changed

+11410
-3302
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
commonjs: true,
44
es2021: true,
55
node: true,
6-
mocha: true,
6+
jest: true,
77
},
88
extends: ['eslint:recommended', 'prettier'],
99
parserOptions: {

__tests__/getAPIUrl.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var imgur = require('../lib/imgur.js');
2+
3+
describe('getAPIUrl()', function () {
4+
test('should return the default API URL, if nothing is set', function () {
5+
var defaultAPIUrl = 'https://api.imgur.com/3/';
6+
return expect(imgur.getAPIUrl()).toBe(defaultAPIUrl);
7+
});
8+
9+
test('should return the same API URL that was set', function () {
10+
var apiUrl = 'https://imgur-apiv3.p.mashape.com/';
11+
imgur.setAPIUrl(apiUrl);
12+
13+
return expect(imgur.getAPIUrl()).toBe(apiUrl);
14+
});
15+
});

__tests__/getClientId.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var imgur = require('../lib/imgur.js');
2+
3+
describe('getClientId()', function () {
4+
test('should return the default client id, if nothing is set', function () {
5+
var defaultClientId = 'f0ea04148a54268';
6+
return expect(imgur.getClientId()).toBe(defaultClientId);
7+
});
8+
9+
test('should return the same client that was set', function () {
10+
var clientId = '123456789abcdef';
11+
imgur.setClientId(clientId);
12+
13+
return expect(imgur.getClientId()).toBe(clientId);
14+
});
15+
});

__tests__/getMashapeKey.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var imgur = require('../lib/imgur.js');
2+
3+
describe('getMashapeKey()', function () {
4+
test('should return the same client that was set', function () {
5+
var mashapeKey = '123456789abcdef';
6+
imgur.setMashapeKey(mashapeKey);
7+
8+
return expect(imgur.getMashapeKey()).toBe(mashapeKey);
9+
});
10+
});

__tests__/imgurRequest.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var imgur = require('../lib/imgur.js'),
2+
imgurTestId1 = 'mbgq7nd'; // Kitten
3+
4+
describe('_imgurRequest()', function () {
5+
test('should fail with no input', function () {
6+
var errMsg = 'Invalid argument';
7+
8+
expect(imgur._imgurRequest()).rejects.toMatch(errMsg);
9+
});
10+
11+
test('should fail with an invalid operation specified', function () {
12+
var errMsg = 'Invalid operation';
13+
14+
expect(imgur._imgurRequest('blah', imgurTestId1)).rejects.toMatch(errMsg);
15+
});
16+
});

__tests__/search.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var imgur = require('../lib/imgur.js'),
2+
Q = require('q');
3+
4+
describe('SEARCH', function () {
5+
describe('search options validations', function () {
6+
test('should fail when query is not passed', function () {
7+
var errMsg =
8+
'Search requires a query. Try searching with a query (e.g cats).';
9+
expect(imgur.search()).rejects.toMatch(errMsg);
10+
});
11+
12+
test('should fail when query is passed a boolean', function () {
13+
var errMsg = 'You did not pass a string as a query.';
14+
expect(imgur.search(true)).rejects.toMatch(errMsg);
15+
});
16+
17+
test('should fail when query is passed a number', function () {
18+
var errMsg = 'You did not pass a string as a query.';
19+
expect(imgur.search(1)).rejects.toMatch(errMsg);
20+
});
21+
22+
test('should fail when query is passed a number', function () {
23+
var errMsg = 'You did not pass a string as a query.';
24+
expect(imgur.search(1)).rejects.toMatch(errMsg);
25+
});
26+
});
27+
28+
describe("delegates to _imgurRequest('search', ...)", function () {
29+
var mockResult = {
30+
data: [],
31+
params: {
32+
page: '1',
33+
dateRange: 'month',
34+
sort: 'viral',
35+
},
36+
};
37+
var payload = '/viral/month/1?q=meme';
38+
var _imgurRequestBackup = imgur._imgurRequest;
39+
40+
beforeEach(function () {
41+
var deferred = Q.defer();
42+
imgur._imgurRequest = jest
43+
.fn()
44+
.mockImplementation(() => deferred.promise);
45+
deferred.resolve(mockResult);
46+
});
47+
48+
afterEach(function () {
49+
imgur._imgurRequest.mockClear();
50+
imgur._imgurRequest = _imgurRequestBackup;
51+
});
52+
53+
it('should delegate', function () {
54+
var promise = imgur.search('meme', {
55+
sort: 'viral',
56+
dateRange: 'month',
57+
page: '1',
58+
});
59+
60+
expect(imgur._imgurRequest).toHaveBeenCalledWith('search', payload);
61+
expect(promise).resolves.toMatchObject(mockResult);
62+
});
63+
});
64+
});

__tests__/setAPIUrl.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var imgur = require('../lib/imgur.js');
2+
3+
describe('setAPIUrl()', function () {
4+
beforeEach(function () {
5+
var defaultImgurAPIUrl = 'https://api.imgur.com/3/';
6+
imgur.setAPIUrl(defaultImgurAPIUrl);
7+
});
8+
9+
test('should return the API Url that was set', function () {
10+
var imgurAPIUrl = 'https://imgur-apiv3.p.mashape.com/';
11+
imgur.setAPIUrl(imgurAPIUrl);
12+
return expect(imgur.getAPIUrl()).toBe(imgurAPIUrl);
13+
});
14+
15+
test('should not set an empty API Url', function () {
16+
var imgurAPIUrl = '';
17+
imgur.setAPIUrl(imgurAPIUrl);
18+
return expect(imgur.getAPIUrl()).not.toBe(imgurAPIUrl);
19+
});
20+
21+
test('should not set a number', function () {
22+
var imgurAPIUrl = 1024;
23+
imgur.setAPIUrl(imgurAPIUrl);
24+
return expect(imgur.getAPIUrl()).not.toBe(imgurAPIUrl);
25+
});
26+
27+
test('should not set a boolean', function () {
28+
var imgurAPIUrl = false;
29+
imgur.setAPIUrl(imgurAPIUrl);
30+
return expect(imgur.getAPIUrl()).not.toBe(imgurAPIUrl);
31+
});
32+
});

__tests__/setClientId.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var imgur = require('../lib/imgur.js');
2+
3+
describe('setClientId()', function () {
4+
beforeEach(function () {
5+
var defaultClientId = '0123456789abcdef';
6+
imgur.setClientId(defaultClientId);
7+
});
8+
9+
test('should return the client id that was set', function () {
10+
var clientId = 'lolololol';
11+
imgur.setClientId(clientId);
12+
return expect(imgur.getClientId()).toBe(clientId);
13+
});
14+
15+
test('should not set an empty client id', function () {
16+
var clientId = '';
17+
imgur.setClientId(clientId);
18+
return expect(imgur.getClientId()).not.toBe(clientId);
19+
});
20+
21+
test('should not set a number', function () {
22+
var clientId = 1024;
23+
imgur.setClientId(clientId);
24+
return expect(imgur.getClientId()).not.toBe(clientId);
25+
});
26+
27+
test('should not set a boolean', function () {
28+
var clientId = false;
29+
imgur.setClientId(clientId);
30+
return expect(imgur.getClientId()).not.toBe(clientId);
31+
});
32+
});

__tests__/setMashapeKey.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var imgur = require('../lib/imgur.js');
2+
3+
describe('setMashapeKey()', function () {
4+
beforeEach(function () {
5+
var defaultMashapeKey = '0123456789abcdef';
6+
imgur.setMashapeKey(defaultMashapeKey);
7+
});
8+
9+
test('should return the Mashape Key that was set', function () {
10+
var mashapeKey = '0123456789abcdef';
11+
imgur.setMashapeKey(mashapeKey);
12+
return expect(imgur.getMashapeKey()).toBe(mashapeKey);
13+
});
14+
15+
test('should not set an empty Mashape Key', function () {
16+
var mashapeKey = '';
17+
imgur.setMashapeKey(mashapeKey);
18+
return expect(imgur.getMashapeKey()).not.toBe(mashapeKey);
19+
});
20+
21+
test('should not set a number', function () {
22+
var mashapeKey = 1024;
23+
imgur.setMashapeKey(mashapeKey);
24+
return expect(imgur.getMashapeKey()).not.toBe(mashapeKey);
25+
});
26+
27+
test('should not set a boolean', function () {
28+
var mashapeKey = false;
29+
imgur.setMashapeKey(mashapeKey);
30+
return expect(imgur.getMashapeKey()).not.toBe(mashapeKey);
31+
});
32+
});

__tests__/uploadUrl.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var imgur = require('../lib/imgur.js'),
2+
Q = require('q');
3+
4+
describe('uploadUrl()', function () {
5+
describe('validation', function () {
6+
test('should fail with no url', function () {
7+
var errMsg = 'Invalid URL';
8+
9+
expect(imgur.uploadUrl()).rejects.toMatch(errMsg);
10+
});
11+
12+
test('should fail with on a malformed url', function () {
13+
var errMsg = 'Invalid URL';
14+
15+
expect(imgur.uploadUrl('blarg')).rejects.toMatch(errMsg);
16+
});
17+
});
18+
19+
describe("delegates to _imgurRequest('upload', ...)", function () {
20+
var mockResult = { foo: 'bar' };
21+
var testUrl = 'https://somewhere/test.png';
22+
23+
var _imgurRequestBackup = imgur._imgurRequest;
24+
25+
beforeEach(function () {
26+
var deferred = Q.defer();
27+
imgur._imgurRequest = jest
28+
.fn()
29+
.mockImplementation(() => deferred.promise);
30+
deferred.resolve(mockResult);
31+
});
32+
33+
afterEach(function () {
34+
imgur._imgurRequest.mockClear();
35+
imgur._imgurRequest = _imgurRequestBackup;
36+
});
37+
38+
test('should delegate', function () {
39+
var promise = imgur.uploadUrl(testUrl);
40+
41+
expect(imgur._imgurRequest).toHaveBeenCalledWith('upload', testUrl, {
42+
type: 'url',
43+
});
44+
expect(promise).resolves.toMatchObject(mockResult);
45+
});
46+
47+
test('should propagate albumId', function () {
48+
var albumId = '123';
49+
var promise = imgur.uploadUrl(testUrl, albumId);
50+
51+
expect(imgur._imgurRequest).toHaveBeenCalledWith('upload', testUrl, {
52+
album: albumId,
53+
type: 'url',
54+
});
55+
expect(promise).resolves.toMatch(mockResult);
56+
});
57+
});
58+
});

0 commit comments

Comments
 (0)