Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I write a test for such situation? #199

Closed
DavidHe1127 opened this issue Mar 7, 2017 · 1 comment
Closed

How can I write a test for such situation? #199

DavidHe1127 opened this issue Mar 7, 2017 · 1 comment

Comments

@DavidHe1127
Copy link

DavidHe1127 commented Mar 7, 2017

I am not sure this is a correct place to ask a question. But I see git issues tab is really active and hope someone can help me out ASAP.

category.js to be tested

'use strict';

module.exports = (client) => {
  const filter = (done) => {
    client.getEntries({
      content_type: 'category',
      select: 'fields'
    }).then(res => {
      const categories = res.items.map((x) => {
        return {
          id: x.sys.id,
          name: x.fields.name
        };
      });
      done(null, categories);
    }).catch(err => {
      console.log(err);
      done(err, null);
    });
  };

  return {
    filter
  };
};

My current test - client is an instance created from a constructor provided by 3rd library.

'use strict'

const test = require('tap').test;
const td = require('testdouble');
const fs = require('fs');
const category = require('../category');

const setup = () => {
  return JSON.parse(fs.readFileSync(`${__dirname}/entries.sample.json`, 'utf-8'));
};

test('get all categories', (t) => {
  const content = setup();
  let client = {
    getEntires: () => {
      return content;
    }
  };
  client = td.object(client);
  const _category = category(client);
  const filter =  _category.filter;
  // client.getEntires = function() {
  //   return content;
  // };
  const _filter = td.function('filter');
  td.when(_filter).thenCallback(null, {
    id: 'xxx',
    name: 'yyy'
  });
  // td.verify(_done(content));
  // const _category = category(client);
  // td.when(client.getEntires()).thenCallback(null, {
  //   id: 'xxx',
  //   name: 'xxx'
  // });
  // _category.filter(_done);
  t.end();
});

My question is as follow

How do I test the case that given the response from client.getEntries promise resolve callback, the callback function done is executed with second argument filled up with correct value like {id: 'xxx', name: 'yyyy'}

Also, is my design too difficult to write a test?

Many thanks in advance.

@searls
Copy link
Member

searls commented Mar 9, 2017

You could do something like this to cover both cases:

it('is happy', function(done) {
  var client = td.object(['getEntries'])
  td.when(client.getEntries({
    content_type: 'category', 
    select: 'fields'
  })).thenResolve({items: [{sys: {id: 'lolid'}, {fields: {name: 'lolname'}}}]})

  subject(client).filter(function(er, result) {
    expect(er).toBeFalsy()
    expect(result).toEqual([{id: 'lolid', name: 'lolname'}])
    done()
  })
})

it('is sad', function(done) {
  var client = td.object(['getEntries'])
  td.when(client.getEntries({
    content_type: 'category', 
    select: 'fields'
  })).thenReject('lolerror')

  subject(client).filter(function(er, result) {
    expect(result).toBeFalsy()
    expect(er).toEqual('lolerror')
    done()
  })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants