concept of stub in node js

Solutions on MaxInterview for concept of stub in node js by the best coders in the world

showing results for - "concept of stub in node js"
Aubrey
06 Jul 2016
1const request = require('request');
2const getPhotosByAlbumId = (id) => {
3const requestUrl = `https://jsonplaceholder.typicode.com/albums/${id}/photos?_limit=3`;
4return new Promise((resolve, reject) => {
5    request.get(requestUrl, (err, res, body) => {
6        if (err) {
7            return reject(err);
8        }
9        resolve(JSON.parse(body));
10    });
11});
12};
13module.exports = getPhotosByAlbumId;
14To test this function this is the stub
15const expect = require('chai').expect;
16const request = require('request');
17const sinon = require('sinon');
18const getPhotosByAlbumId = require('./index');
19describe('with Stub: getPhotosByAlbumId', () => {
20before(() => {
21    sinon.stub(request, 'get')
22        .yields(null, null, JSON.stringify([
23            {
24                "albumId": 1,
25                "id": 1,
26                "title": "A real photo 1",
27                "url": "https://via.placeholder.com/600/92c952",
28                "thumbnailUrl": "https://via.placeholder.com/150/92c952"
29            },
30            {
31                "albumId": 1,
32                "id": 2,
33                "title": "A real photo 2",
34                "url": "https://via.placeholder.com/600/771796",
35                "thumbnailUrl": "https://via.placeholder.com/150/771796"
36            },
37            {
38                "albumId": 1,
39                "id": 3,
40                "title": "A real photo 3",
41                "url": "https://via.placeholder.com/600/24f355",
42                "thumbnailUrl": "https://via.placeholder.com/150/24f355"
43            }
44        ]));
45});
46after(() => {
47    request.get.restore();
48});
49it('should getPhotosByAlbumId', (done) => {
50    getPhotosByAlbumId(1).then((photos) => {
51        expect(photos.length).to.equal(3);
52        photos.forEach(photo => {
53            expect(photo).to.have.property('id');
54            expect(photo).to.have.property('title');
55            expect(photo).to.have.property('url');
56        });
57        done();
58    });
59});
60});