4

Here I have a function generateBlocks that takes in an array blocks and a function onBlockClick. It returns an array of object where each object has two properties label and onClick.

function generateBlocks(blocks, onBlockClick){
   return blocks.map(block => (
     {
        label: block.label,
        onClick: ()=>onBlockClick(block.name)
     }
 ))
}

I'm unable test it's return value. Here's the test case:

const blocks = [{label: "A", name: "a"}, {label: "B", name: "b"}];
const onBlockClick = someFunction(){};

expect(generateBlocks(blocks, onBlockClick)).to.deep.equal(expected)

I cannot create expected as [[{label: "A", onClick: ()=>onBlockClick("A")},...] because function reference will be different.

So how do I refactor generateBlocks function to make it testable?

Frozen Crayon
  • 5,172
  • 8
  • 36
  • 71
  • The first step you've taken is to ask **"How do I test it?"**. The next thing you should ask yourself is **"What do I want to test here?"**. It's hard to refactor existing code to make it testable if you don't actually know what you want to test. – byxor Aug 19 '16 at 11:12
  • 4
    Well, don't use `deep.equal`? Just assert that every object has a `.label` and that its `.onclick()` returns the name when you pass in the identity function for `onBlockClick`. – Bergi Aug 19 '16 at 11:25
  • @Brandon Please see the test case which **clearly explains** what I want to test which is **the return value**. – Frozen Crayon Aug 19 '16 at 11:45
  • 1
    @Bergi I didn't get you, what do you mean returns the name? onBlockClick does not return anything. – Frozen Crayon Aug 19 '16 at 12:12
  • @ArjunU.: But if it did, `onClick` would as well, and you could easily test that return value. Or you use a library like sinon to spy on the invocations. – Bergi Aug 19 '16 at 12:30
  • @Bergi I will test the onClick when I test the View component but in this case consider it's just another function. – Frozen Crayon Aug 21 '16 at 04:27

1 Answers1

1

You can use .toString() method on the function ref and compare to expected string result.

Martin Chaov
  • 824
  • 7
  • 17
  • I first thought so, but this will only return the function code, which might change. You want to test for results, hence you need to call the function: https://stackoverflow.com/a/56837070/5037146 – Aerodynamic Nov 01 '22 at 08:08
  • Function that is returned might not be callable in all contexts... The only right answer to many of these question is "it depends". Sometimes the simplest solution that could take you 80% there is the best thing to do. – Martin Chaov Nov 01 '22 at 08:39