mercredi 28 novembre 2018

Jest - Testing functions based in other functions

I'm testing my new module:
- It has multiple basic functions, e.g.: addItem(item), rmItem(item), etc.
- But also I have bulk functions which basically receive an array of items and iterate over it calling the basic functions:

addItems = (items) => {
  items.forEach(item => addItem(item))
}

rmItems = (items) => {
  items.forEach(item => rmItem(item))
}

I have, already, tested my basic functions and I'm trying to test my bulk functions.

BUT my problem is: How can I test my bulk function mocking the basic function for this specific case?

I'm trying something like:

import List from '/src/list.js';

it('test basic functions', () => {...}) <- It works cool
it('test bulk function', () => {
  const list = new List();
  list.addItems([1, 2, 3, 4]);

  expect(list.addItem).toHaveBeenCalledTimes(4);
})

My main problem is, I should be mocking addItem for the addItems test. But I can't find a way to do it.

Aucun commentaire:

Enregistrer un commentaire