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

Commit 2d22801

Browse files
committed
test: refactor tests to work with jest
1 parent f509738 commit 2d22801

File tree

9 files changed

+92
-138
lines changed

9 files changed

+92
-138
lines changed

__tests__/getAPIUrl.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
var imgur = require('../lib/imgur.js'),
2-
chai = require('chai'),
3-
chaiAsPromised = require('chai-as-promised'),
4-
expect = chai.expect;
1+
var imgur = require('../lib/imgur.js');
52

6-
chai.use(chaiAsPromised);
7-
8-
describe('#getAPIUrl()', function () {
9-
it('should return the default API URL, if nothing is set', function () {
3+
describe('getAPIUrl()', function () {
4+
test('should return the default API URL, if nothing is set', function () {
105
var defaultAPIUrl = 'https://api.imgur.com/3/';
11-
return expect(imgur.getAPIUrl()).to.equal(defaultAPIUrl);
6+
return expect(imgur.getAPIUrl()).toBe(defaultAPIUrl);
127
});
138

14-
it('should return the same API URL that was set', function () {
9+
test('should return the same API URL that was set', function () {
1510
var apiUrl = 'https://imgur-apiv3.p.mashape.com/';
1611
imgur.setAPIUrl(apiUrl);
1712

18-
return expect(imgur.getAPIUrl()).to.equal(apiUrl);
13+
return expect(imgur.getAPIUrl()).toBe(apiUrl);
1914
});
2015
});

__tests__/getClientId.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
var imgur = require('../lib/imgur.js'),
2-
chai = require('chai'),
3-
chaiAsPromised = require('chai-as-promised'),
4-
expect = chai.expect;
1+
var imgur = require('../lib/imgur.js');
52

6-
chai.use(chaiAsPromised);
7-
8-
describe('#getClientId()', function () {
9-
it('should return the default client id, if nothing is set', function () {
3+
describe('getClientId()', function () {
4+
test('should return the default client id, if nothing is set', function () {
105
var defaultClientId = 'f0ea04148a54268';
11-
return expect(imgur.getClientId()).to.equal(defaultClientId);
6+
return expect(imgur.getClientId()).toBe(defaultClientId);
127
});
138

14-
it('should return the same client that was set', function () {
9+
test('should return the same client that was set', function () {
1510
var clientId = '123456789abcdef';
1611
imgur.setClientId(clientId);
1712

18-
return expect(imgur.getClientId()).to.equal(clientId);
13+
return expect(imgur.getClientId()).toBe(clientId);
1914
});
2015
});

__tests__/getMashapeKey.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
var imgur = require('../lib/imgur.js'),
2-
chai = require('chai'),
3-
chaiAsPromised = require('chai-as-promised'),
4-
expect = chai.expect;
1+
var imgur = require('../lib/imgur.js');
52

6-
chai.use(chaiAsPromised);
7-
8-
describe('#getMashapeKey()', function () {
9-
it('should return the same client that was set', function () {
3+
describe('getMashapeKey()', function () {
4+
test('should return the same client that was set', function () {
105
var mashapeKey = '123456789abcdef';
116
imgur.setMashapeKey(mashapeKey);
127

13-
return expect(imgur.getMashapeKey()).to.equal(mashapeKey);
8+
return expect(imgur.getMashapeKey()).toBe(mashapeKey);
149
});
1510
});

__tests__/imgurRequest.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
var imgur = require('../lib/imgur.js'),
2-
chai = require('chai'),
3-
chaiAsPromised = require('chai-as-promised'),
4-
expect = chai.expect,
52
imgurTestId1 = 'mbgq7nd'; // Kitten
63

7-
chai.use(chaiAsPromised);
8-
9-
describe('#_imgurRequest()', function () {
10-
beforeEach(function () {});
11-
12-
it('should fail with no input', function (done) {
4+
describe('_imgurRequest()', function () {
5+
test('should fail with no input', function () {
136
var errMsg = 'Invalid argument';
147

15-
expect(imgur._imgurRequest()).to.be.rejectedWith(errMsg).and.notify(done);
8+
expect(imgur._imgurRequest()).rejects.toMatch(errMsg);
169
});
1710

18-
it('should fail with an invalid operation specified', function (done) {
11+
test('should fail with an invalid operation specified', function () {
1912
var errMsg = 'Invalid operation';
2013

21-
expect(imgur._imgurRequest('blah', imgurTestId1))
22-
.to.be.rejectedWith(errMsg)
23-
.and.notify(done);
14+
expect(imgur._imgurRequest('blah', imgurTestId1)).rejects.toMatch(errMsg);
2415
});
2516
});

__tests__/search.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
var imgur = require('../lib/imgur.js'),
2-
chai = require('chai'),
3-
chaiAsPromised = require('chai-as-promised'),
4-
expect = chai.expect,
5-
Q = require('q'),
6-
sinon = require('sinon'),
7-
sinonChai = require('sinon-chai');
8-
9-
chai.use(sinonChai);
10-
chai.use(chaiAsPromised);
2+
Q = require('q');
113

124
describe('SEARCH', function () {
135
describe('search options validations', function () {
14-
it('should fail when query is not passed', function (done) {
6+
test('should fail when query is not passed', function () {
157
var errMsg =
168
'Search requires a query. Try searching with a query (e.g cats).';
17-
expect(imgur.search()).to.be.rejectedWith(errMsg).and.notify(done);
9+
expect(imgur.search()).rejects.toMatch(errMsg);
1810
});
1911

20-
it('should fail when query is passed a boolean', function (done) {
12+
test('should fail when query is passed a boolean', function () {
2113
var errMsg = 'You did not pass a string as a query.';
22-
expect(imgur.search(true)).to.be.rejectedWith(errMsg).and.notify(done);
14+
expect(imgur.search(true)).rejects.toMatch(errMsg);
2315
});
2416

25-
it('should fail when query is passed a number', function (done) {
17+
test('should fail when query is passed a number', function () {
2618
var errMsg = 'You did not pass a string as a query.';
27-
expect(imgur.search(1)).to.be.rejectedWith(errMsg).and.notify(done);
19+
expect(imgur.search(1)).rejects.toMatch(errMsg);
2820
});
2921

30-
it('should fail when query is passed a number', function (done) {
22+
test('should fail when query is passed a number', function () {
3123
var errMsg = 'You did not pass a string as a query.';
32-
expect(imgur.search(1)).to.be.rejectedWith(errMsg).and.notify(done);
24+
expect(imgur.search(1)).rejects.toMatch(errMsg);
3325
});
3426
});
3527

36-
describe("delegates to #_imgurRequest('search', ...)", function () {
28+
describe("delegates to _imgurRequest('search', ...)", function () {
3729
var mockResult = {
3830
data: [],
3931
params: {
@@ -43,26 +35,30 @@ describe('SEARCH', function () {
4335
},
4436
};
4537
var payload = '/viral/month/1?q=meme';
38+
var _imgurRequestBackup = imgur._imgurRequest;
4639

4740
beforeEach(function () {
4841
var deferred = Q.defer();
49-
sinon.stub(imgur, '_imgurRequest').returns(deferred.promise);
42+
imgur._imgurRequest = jest
43+
.fn()
44+
.mockImplementation(() => deferred.promise);
5045
deferred.resolve(mockResult);
5146
});
5247

5348
afterEach(function () {
54-
imgur._imgurRequest.restore();
49+
imgur._imgurRequest.mockClear();
50+
imgur._imgurRequest = _imgurRequestBackup;
5551
});
5652

57-
it('should delegate', function (done) {
53+
it('should delegate', function () {
5854
var promise = imgur.search('meme', {
5955
sort: 'viral',
6056
dateRange: 'month',
6157
page: '1',
6258
});
6359

64-
expect(imgur._imgurRequest).to.have.been.calledWith('search', payload);
65-
expect(promise).to.eventually.deep.equal(mockResult).and.notify(done);
60+
expect(imgur._imgurRequest).toHaveBeenCalledWith('search', payload);
61+
expect(promise).resolves.toMatchObject(mockResult);
6662
});
6763
});
6864
});

__tests__/setAPIUrl.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
1-
var imgur = require('../lib/imgur.js'),
2-
chai = require('chai'),
3-
chaiAsPromised = require('chai-as-promised'),
4-
expect = chai.expect;
1+
var imgur = require('../lib/imgur.js');
52

6-
chai.use(chaiAsPromised);
7-
8-
describe('#setAPIUrl()', function () {
3+
describe('setAPIUrl()', function () {
94
beforeEach(function () {
105
var defaultImgurAPIUrl = 'https://api.imgur.com/3/';
116
imgur.setAPIUrl(defaultImgurAPIUrl);
127
});
138

14-
it('should return the API Url that was set', function () {
9+
test('should return the API Url that was set', function () {
1510
var imgurAPIUrl = 'https://imgur-apiv3.p.mashape.com/';
1611
imgur.setAPIUrl(imgurAPIUrl);
17-
return expect(imgur.getAPIUrl()).to.equal(imgurAPIUrl);
12+
return expect(imgur.getAPIUrl()).toBe(imgurAPIUrl);
1813
});
1914

20-
it('should not set an empty API Url', function () {
15+
test('should not set an empty API Url', function () {
2116
var imgurAPIUrl = '';
2217
imgur.setAPIUrl(imgurAPIUrl);
23-
return expect(imgur.getAPIUrl()).to.not.equal(imgurAPIUrl);
18+
return expect(imgur.getAPIUrl()).not.toBe(imgurAPIUrl);
2419
});
2520

26-
it('should not set a number', function () {
21+
test('should not set a number', function () {
2722
var imgurAPIUrl = 1024;
2823
imgur.setAPIUrl(imgurAPIUrl);
29-
return expect(imgur.getAPIUrl()).to.not.equal(imgurAPIUrl);
24+
return expect(imgur.getAPIUrl()).not.toBe(imgurAPIUrl);
3025
});
3126

32-
it('should not set a boolean', function () {
27+
test('should not set a boolean', function () {
3328
var imgurAPIUrl = false;
3429
imgur.setAPIUrl(imgurAPIUrl);
35-
return expect(imgur.getAPIUrl()).to.not.equal(imgurAPIUrl);
30+
return expect(imgur.getAPIUrl()).not.toBe(imgurAPIUrl);
3631
});
3732
});

__tests__/setClientId.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
1-
var imgur = require('../lib/imgur.js'),
2-
chai = require('chai'),
3-
chaiAsPromised = require('chai-as-promised'),
4-
expect = chai.expect;
1+
var imgur = require('../lib/imgur.js');
52

6-
chai.use(chaiAsPromised);
7-
8-
describe('#setClientId()', function () {
3+
describe('setClientId()', function () {
94
beforeEach(function () {
105
var defaultClientId = '0123456789abcdef';
116
imgur.setClientId(defaultClientId);
127
});
138

14-
it('should return the client id that was set', function () {
9+
test('should return the client id that was set', function () {
1510
var clientId = 'lolololol';
1611
imgur.setClientId(clientId);
17-
return expect(imgur.getClientId()).to.equal(clientId);
12+
return expect(imgur.getClientId()).toBe(clientId);
1813
});
1914

20-
it('should not set an empty client id', function () {
15+
test('should not set an empty client id', function () {
2116
var clientId = '';
2217
imgur.setClientId(clientId);
23-
return expect(imgur.getClientId()).to.not.equal(clientId);
18+
return expect(imgur.getClientId()).not.toBe(clientId);
2419
});
2520

26-
it('should not set a number', function () {
21+
test('should not set a number', function () {
2722
var clientId = 1024;
2823
imgur.setClientId(clientId);
29-
return expect(imgur.getClientId()).to.not.equal(clientId);
24+
return expect(imgur.getClientId()).not.toBe(clientId);
3025
});
3126

32-
it('should not set a boolean', function () {
27+
test('should not set a boolean', function () {
3328
var clientId = false;
3429
imgur.setClientId(clientId);
35-
return expect(imgur.getClientId()).to.not.equal(clientId);
30+
return expect(imgur.getClientId()).not.toBe(clientId);
3631
});
3732
});

__tests__/setMashapeKey.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
1-
var imgur = require('../lib/imgur.js'),
2-
chai = require('chai'),
3-
chaiAsPromised = require('chai-as-promised'),
4-
expect = chai.expect;
1+
var imgur = require('../lib/imgur.js');
52

6-
chai.use(chaiAsPromised);
7-
8-
describe('#setMashapeKey()', function () {
3+
describe('setMashapeKey()', function () {
94
beforeEach(function () {
105
var defaultMashapeKey = '0123456789abcdef';
116
imgur.setMashapeKey(defaultMashapeKey);
127
});
138

14-
it('should return the Mashape Key that was set', function () {
9+
test('should return the Mashape Key that was set', function () {
1510
var mashapeKey = '0123456789abcdef';
1611
imgur.setMashapeKey(mashapeKey);
17-
return expect(imgur.getMashapeKey()).to.equal(mashapeKey);
12+
return expect(imgur.getMashapeKey()).toBe(mashapeKey);
1813
});
1914

20-
it('should not set an empty Mashape Key', function () {
15+
test('should not set an empty Mashape Key', function () {
2116
var mashapeKey = '';
2217
imgur.setMashapeKey(mashapeKey);
23-
return expect(imgur.getMashapeKey()).to.not.equal(mashapeKey);
18+
return expect(imgur.getMashapeKey()).not.toBe(mashapeKey);
2419
});
2520

26-
it('should not set a number', function () {
21+
test('should not set a number', function () {
2722
var mashapeKey = 1024;
2823
imgur.setMashapeKey(mashapeKey);
29-
return expect(imgur.getMashapeKey()).to.not.equal(mashapeKey);
24+
return expect(imgur.getMashapeKey()).not.toBe(mashapeKey);
3025
});
3126

32-
it('should not set a boolean', function () {
27+
test('should not set a boolean', function () {
3328
var mashapeKey = false;
3429
imgur.setMashapeKey(mashapeKey);
35-
return expect(imgur.getMashapeKey()).to.not.equal(mashapeKey);
30+
return expect(imgur.getMashapeKey()).not.toBe(mashapeKey);
3631
});
3732
});

0 commit comments

Comments
 (0)