0xburned
0xburned

Reputation: 2655

JS unit test with Jasmine

I am very new to Jasmine, infact I just started today and don't have prior knowledge writing JS unit test cases. I am trying to write a simple test case in Jasmine in order to test a very basic JS code. I have added all the necessary scripts and libraries in my project.

So here is the piece of JS code. I am fetching the local Australian date and time and arranging them in a particular format.

var date = {

    formatFullDate: function(date) {
        var localDate = this.getLocalTimeFromAustraliaTime(date),
            month = this.addZeroToFront(localDate.getMonth() + 1),
            hour = this.addZeroToFront(localDate.getHours()),
            minute = this.addZeroToFront(localDate.getMinutes());

        return localDate.getDate() + '/' + month + '/' + localDate.getFullYear() + ' ' + hour + ':' + minute;
    },

    formatTime: function(date) {
    var localDate = this.getLocalTimeFromAustraliaTime(date),
        hour = this.addZeroToFront(localDate.getHours()),
        minute = this.addZeroToFront(localDate.getMinutes());

    return hour + ':' + minute;
    },

    addZeroToFront: function(whatever) {
    if (whatever < 10) whatever = "0" + whatever;
    return whatever;
    },

    getUTCtimeOffset: function() {
    var date = new Date();
    return date.getTimezoneOffset();
    },

    getLocalTimeFromAustraliaTime: function (date) {
    var utcTime = new Date(date.getTime() - 11*60*60*1000),
        localDate = new Date(utcTime - this.getUTCtimeOffset()*60*1000);
    return localDate;
    }
}

In the above mentioned code I can test various things for example the function is fetching the correct timezone, adding 0 prior to the time, formatting the date, etc.

I would like to know how do I structure my test case. I could think of the possible structure

describe( "Australian full date format", function () {

    describe( "Time format", function () {
        it("Check if time format is fetched correctly", function () {
            expect(something).toEqual(something);
        });
    });

    describe( "Adding 0 to the front", function () {
        it("Check if 0 is added prior to the time", function () {
            expect(something).toEqual(something);
        });
    });

    describe( "Get local Australian time", function () {
        it("Check if correct Australian time is fetched", function () {
            expect(something).toEqual(something);
        });
    });

    it("Check if the date is formatted correctly", function () {
        expect(something).toEqual(something);
    });
});

If I am in the right direction then how do I move forward from this point onward. I am quite confused how to write the equivalent test case for my JS code.

Upvotes: 0

Views: 904

Answers (2)

Nafeesa
Nafeesa

Reputation: 1

Basic(example) program in jasmine js unit test for beginners

// calculator operations like addition , subraction, multiplication and division

describe("calculator first method", function(){

   it ("can add positive integers", function(){
    expect(add(8,5)).toEqual(13);
   })
   it ("can subtract positive integers", function(){
    expect(sub(8,5)).toEqual(3);
   })
   it ("can multiply positive integers", function(){
    expect(mul(8,5)).toEqual(40);
   })
   it ("can divide positive integers", function(){
    expect(div(16,4)).toEqual(4);
   });
   });

save this in the file called p1.js

function add(a,b){
return a+b;
}
function sub(a,b){
return a-b;
}
function mul(a,b){
return a*b;
}
function div(a,b){
return a/b;
}

save this in the file called p2.js make sure your using vs code. In the Runner html File add both files in the spec directory, click on runner html file and select live server.

Upvotes: 0

phts
phts

Reputation: 3925

I would create tests something like this:

describe("Australian full date format", function() {
  describe("#formatFullDate", function() {
    it("should have format 'date/month/year hour:min'", function() {
      //...
    });
  });

  describe("#formatTime", function() {
    it("should have format 'hour:min'", function() {
      //...
    });
  });

  describe("#addZeroToFront", function() {
    describe("if whatever < 10", function() {
      it("should add 0 to front", function() {
        //...
      });
    });
    describe("if whatever >= 10", function() {
      it("should return original without any changes", function() {
        //...
      });
    });
  });

  //...
});

So I used describe to show what function is being tested. Then used describe for conditions. Used it to test only one feature of a function. All external objects (Date in this case) should be stubbed to test functions getUTCtimeOffset and getLocalTimeFromAustraliaTime.

Upvotes: 1

Related Questions