Skip to content

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

@DavidHe1127

Description

@DavidHe1127

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions