Browse Source

d4 ex2

master
Ganesh 5 years ago
parent
commit
5574b9e8ec
8 changed files with 2583 additions and 0 deletions
  1. 1
      SinonStubs-1/.gitignore
  2. 15
      SinonStubs-1/README.md
  3. 16
      SinonStubs-1/index.js
  4. 76
      SinonStubs-1/index.test.js
  5. 17
      SinonStubs-1/index2.js
  6. 1379
      SinonStubs-1/package-lock.json
  7. 17
      SinonStubs-1/package.json
  8. 1062
      SinonStubs-1/yarn.lock

1
SinonStubs-1/.gitignore

@ -0,0 +1 @@
node_modules/

15
SinonStubs-1/README.md

@ -0,0 +1,15 @@
# SinonStubs-1
A repo for my article on stubbing http requests with sinon
### Getting started
Install dependencies:
```bash
~ yarn install
```
Run the test:
```bash
~ yarn test
```

16
SinonStubs-1/index.js

@ -0,0 +1,16 @@
const request = require('request');
module.exports = {
getAlbumById: async function(id) {
const requestUrl = `https://jsonplaceholder.typicode.com/albums/${id}/photos?_limit=5`;
return new Promise((resolve, reject) => {
request.get(requestUrl, (err, res, body) => {
if(err) {
return reject(err);
}
resolve(JSON.parse(body));
});
});
}
};

76
SinonStubs-1/index.test.js

@ -0,0 +1,76 @@
const expect = require('chai').expect;
const request = require('request');
const sinon = require('sinon');
const index = require('./index');
const index2 = require('./index2');
describe('withoutStub: getPhotosByAlbumId', () => {
it('should getPhotosByAlbumId', (done) => {
index2.getPhotosByAlbumId(1).then((photos) => {
expect(photos.length).to.equal(3);
photos.forEach(photo => {
expect(photo).to.have.property('id');
expect(photo).to.have.property('title');
expect(photo).to.have.property('url');
});
done();
});
});
});
describe('with Stub: getPhotosByAlbumId', () => {
before(() => {
sinon.stub(request, 'get')
.yields(null, null, JSON.stringify([
{
"albumId": 1,
"id": 1,
"title": "accusamus beatae ad facilis cum similique qui sunt",
"url": "https://via.placeholder.com/600/92c952",
"thumbnailUrl": "https://via.placeholder.com/150/92c952"
},
{
"albumId": 1,
"id": 2,
"title": "reprehenderit est deserunt velit ipsam",
"url": "https://via.placeholder.com/600/771796",
"thumbnailUrl": "https://via.placeholder.com/150/771796"
},
{
"albumId": 1,
"id": 3,
"title": "officia porro iure quia iusto qui ipsa ut modi",
"url": "https://via.placeholder.com/600/24f355",
"thumbnailUrl": "https://via.placeholder.com/150/24f355"
},
{
"albumId": 1,
"id": 4,
"title": "culpa odio esse rerum omnis laboriosam voluptate repudiandae",
"url": "https://via.placeholder.com/600/d32776",
"thumbnailUrl": "https://via.placeholder.com/150/d32776"
},
{
"albumId": 1,
"id": 5,
"title": "natus nisi omnis corporis facere molestiae rerum in",
"url": "https://via.placeholder.com/600/f66b97",
"thumbnailUrl": "https://via.placeholder.com/150/f66b97"
}
]));
});
after(() => {
request.get.restore();
});
it('should getPhotosByAlbumId', (done) => {
index.getAlbumById(1).then((photos) =>{
expect(photos.length).to.equal(5);
expect(photos[0]).to.have.property('id');
done();
});
});
})

17
SinonStubs-1/index2.js

@ -0,0 +1,17 @@
const request = require('request');
module.exports = {
getPhotosByAlbumId: async function(id) {
const requestUrl = `https://jsonplaceholder.typicode.com/albums/${id}/photos?_limit=3`;
return new Promise((resolve, reject) => {
request.get(requestUrl, (err, res, body) => {
if (err) {
return reject(err);
}
resolve(JSON.parse(body));
});
});
}
};

1379
SinonStubs-1/package-lock.json
File diff suppressed because it is too large
View File

17
SinonStubs-1/package.json

@ -0,0 +1,17 @@
{
"name": "PhotoAlbum",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"request": "^2.88.0"
},
"scripts": {
"test": "mocha index.test.js"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.2.2",
"sinon": "^8.0.2"
}
}

1062
SinonStubs-1/yarn.lock
File diff suppressed because it is too large
View File

Loading…
Cancel
Save