me.nkr
me.nkr

Reputation: 108

How to test a function in jest?

const updateSession = async (emitter, dbModel, center_id, session_id, data) => {

    if (await dbModel.update('session', {center_id, session_id, data})) {
        const result = {
            center_id: `k${center_id}`,
            session_id: session_id,
            ...data
        } ;
        emitter.emit('updateSession', {
            operationType: 'update',
            operationOn: 'session',
            data: result
        }) ;
    } else throw new Error('500 Internal Server Error') ;
}

emitter is an EventEmitter

dbModel is a module to interact with database

center_id is a number

session_id is a string

All of the above are same in all cases

I need to test this fucntion in jest with 3 different cases where

case 1 : data = {available_capacity1: 4, available_capacity2: 6}

case 2 : data = {available_capacity1: 4}

case 3 : data = {available_capacity2: 6}

Conditions :

In case 1 the test should pass if data passed to callback of event listener, that is :emitter.on('updateSession', callback), has both available_capacity1 and available_capacity2 properties in data.result object

In case 2 the test should pass if data passed to callback of event listener has only available_capacity1 property in data.result object

In case 3 the test should pass if data passed to callback of event listener has only available_capacity2 property in data.result object

I would like to know how can I write a test for the above mentioned cases in jest

any help is much appreciated, Thanks in advance.

Upvotes: 0

Views: 106

Answers (1)

Adrian Klasson
Adrian Klasson

Reputation: 36

To test a callback function in jest you should use the alternate form of its test function where you consume a callback argument you invoke when you determine the test has ended. More info here: https://jestjs.io/docs/asynchronous#callbacks

Here is an example on how to implement your specific test cases, these are of course not perfect but should get you an idea of how to do them yourself.

test('callback data has available_capacity1 and available_capacity2', done => {
  const data = { available_capacity1: 4, available_capacity2: 6 };
  const emitter = new EventEmitter();
  emitter.once('updateSession', arg => {
    try {
      expect(arg.data).toHaveProperty('available_capacity1');
      expect(arg.data).toHaveProperty('available_capacity2');
      done();
    } catch (error) {
      done(error);
    }
  });
  updateSession(emitter, dbModel, center_id, session_id, data);
});

test('callback data has only available_capacity1', done => {
  const data = { available_capacity1: 4 };
  const emitter = new EventEmitter();
  emitter.once('updateSession', arg => {
    try {
      expect(Object.keys(arg.data).length).toBe(1);
      expect(arg.data).toHaveProperty('available_capacity1');
      done();
    } catch (error) {
      done(error);
    }
  });
  updateSession(emitter, dbModel, center_id, session_id, data);
});

test('callback data has only available_capacity2', done => {
  const data = { available_capacity2: 6 };
  const emitter = new EventEmitter();
  emitter.once('updateSession', arg => {
    try {
      expect(Object.keys(arg.data).length).toBe(1);
      expect(arg.data).toHaveProperty('available_capacity2');
      done();
    } catch (error) {
      done(error);
    }
  });
  updateSession(emitter, dbModel, center_id, session_id, data);
});

Upvotes: 2

Related Questions