1

As i am new to unit testing , I want to test my javascript code its very confusing .. i really want some ones help who could tell the solution for my problem .??

if (!Notification) {
 Notification = {};
  else {
    if (typeof Notification != "object") {
      throw new Error(" already exists ");
    }
  }
 Notification.admin = function() {
    var res = {};
    var prevent = "";

    var DefaultValue = function(ans, type, commnon, status) {
      var notify = type.concat(common);
      if ($("#method").val() == "false" && type == "Text") {
        if (status) {
          return data = '<label class="checkbox-label"><input data-role="ux-checkbox" type="checkbox" disabled="disabled" data-type="' + notify + '" class="grid-checkbox">&nbsp;&nbsp</label>';
        } else {
          return data = '<label class="checkbox-label">' + '<span class="no-checkbox-label" >' + "-" + '</span>' + '</label>';
        }
      } else if (status) {
        if (ans) {
          return data = '<label class="checkbox-label"><input data-role="ux-checkbox" checked=checked type="checkbox" data-type="' + notify + '" class="grid-checkbox ">&nbsp;&nbsp</label>';
        } else {
          return data = '<label class="checkbox-label"><input data-role="ux-checkbox" type="checkbox" data-type="' + notify + '" class="grid-checkbox">&nbsp;&nbsp</label>';
        }
      } else {
        return data = '<label class="checkbox-label">' + '<span class="no-checkbox-label" >' + "-" + '</span>' + '</label>';
      }
    };
    this.init = function() {


    };}; Notification.Obj = new Notification.admin();

  $(document).ready(function() {

Notification.Obj.init();

  });

This is my private function which i want to unit test as i am using mocha and chai ..

i am not able to unit test this function

3
  • What is actually the unit you want to test? I misread private function as private method. Your code example is not really shining light on what the snipplet is supposed to do and what you want to test. It appears to me that you may be confusing unit testing (very easy to setup, do not cover integration of services) and functional browser testing (automated browser click through). Your question my hence either be too broad or actually an XY-problem on your attempted solution. Commented Apr 30, 2018 at 12:31
  • @k0pernikus I want to test Defaultvalue function which is inside com.help.Notification.admin function.... Commented Apr 30, 2018 at 13:49
  • Do not define your your DefaultValue functioon as a closure within the admin function, but make it importable. You also have a side-effect via your jquery selector $("#method").va, that should be passed into that function instead so you can mock it. Then it should become a function that expects 5 inputs value and creates a label tag string, for which you then can test. Commented Apr 30, 2018 at 13:59

1 Answer 1

3

You don't write tests for private methods.

Public methods are the public interface of a class. The ones that are called from the outside. Private methods are implementation details that you don't care about.

The scope of a unit test is to the test all the public methods of a class. (And no, making private methods public within the class is not the solution for that.)

Refactor the actual business part of your private method out into a new service. That service will have the functionality provided through a public method. You can test that service. In you current service you can then inject that service and use its functionality.

That being said: You can only unit test method that don't have side-effects and whose dependency you can mock away (dependency injection).

Sign up to request clarification or add additional context in comments.

2 Comments

@ k0pernikus Thanks for the advice.. could you plz elabarote more so that i could be clear what i need to do exactly....
@john You don't need to unit test private methods. They are private. What k0pernikus is saying is that you should only test your public methods since these are the one's visible to external code. If they work fine then you shouldn't really care what goes on in your private methods.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.