asked    Otis     2018-10-22       javascript       28 view        2 Answers

[SOLVED] how to test private function outside react component

I have a private function outside component. It sounds more convenient to do test for function alone better than test it inside MyComponent, but it's private not exported.

function func(){
 return something;

export default class MyComponent extends Component {

render <SomeComponent someProp={func()} />

I use mocha and enzyme for test and I wonder if there is anyway to test private function rather than test it inside the component.

  2 Answers  

        answered    Ruby     2018-10-22      

func is defined in module scope. It's not possible to reach variables outside the scope they were defined in JavaScript.

Even if func were exported, it would be impossible to provide proper unit tests for it, i.e. test func in one test, then test a unit depends on func (MyComponent) in another test with func being mocked/spied. func is referred within the module, its calls cannot be spied or stubbed.

In order to be fully testable, func needs to be either class method, this makes sense if func and MyComponent are related. Or move func to another module. In this case it's possible to mock the module, or as explained in this answer, due to how ES modules work, func import could be mocked/spied on module * import.

        answered    Nat     2018-10-22      

It's hard to get any value from unit tests for private methods.

Why do you may want that?

  1. To increase test coverage KPI. But once private methods are called by public - they will be taken into account anyway.
  2. To ensure everything is working... But making tests for private methods may lead to:

    a. tests are broken while component as a part of system works fine(say, private method has been changed, renamed or removed... but not tests for it) - false negative

    b. tests for private methods are fine, but component is broken(say, private method is needed to be called but it is not) - false positive

Your Answer

 2018-10-22         Omar

File.Exists returns true on file name only

I am currently debugging an application, where the following code returns true on the file name only. If Not IO.File.Exists(out) Then '' WorkEnd IfThe watches for this execution is as follows:How is this statement validated as true, when the actual file is located fairly deep on a network drive? I'd assume that this could be true, if the file was located in the same directory as my execution. Documentation: File.Exists the path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the curren...
 .net                     1 answers                     102 view
 2018-10-22         Clement

Scrolling in ListView loses text color and typeface

I'm not sure why it's not working, but what my code should do is very simple.I have a ListView with default text color (BLACK) and typeface (MONOSPACE) that I declared in getView(), so everything works well when the view is created.When I select an item from the list it should change the text color to RED and the typeface to MONOSPACE and BOLD, and if I click another item, it will change that item to RED and BOLD and return the previous text to BLACK and NORMAL. This works just fine, but if I scroll away from the RED and BOLD text until it's not visible, and then scroll bac...
 java                     1 answers                     102 view
 2018-10-22         Chloe

PHP continues loop inside loop

I have a repeated row which have 5 columns. I want when every time row is looped column data is looped number but continues after every loop. Sample code:$list = 0;$list++;for ($i = 0; $i < 5; $i++ ){<div class="row"> <div class="col-2">$list</div> <div class="col-2">$list</div> <div class="col-2">$list</div> <div class="col-2">$list</div> <div class="col-2">$list</div></div>}Sample result what i want:<div class="row"> <div class="col-2">1</div> <div class="col-2">...
 php                     3 answers                     107 view