jeudi 25 mai 2017

Test a simple function in jasmine karma - Angular

Basically i have this function

function getDP() {
    if (someFunctionOutsideAngular()) {       
        var str = vm.dp;                
        var selectedDate = '';
        if (vm.dp == null) {
            selectedDate = new Date();
        } else {
            selectedDate = vm.dp;
        }
        var options = {
            date: selectedDate,
            mode: 'date'
        };
        function onSuccess(date) {            
            vm.dt = new Date(date);            
        }
        function onError(error) { // Android only
            console.log('Error: ' + error);
        }
        datePicker.show(options, onSuccess, onError);
    } else {
        return false;
    }
}

I'm writing Unit test for this function. This is what i have written till now.

(function(){

describe('test', function(){

    var sut,
        dataContext,
        $q,
        deferred,
        $location;

        beforeEach(module('app'));

        beforeEach(inject(function(_$controller_, _dataContext_, _$q_,$location) {        
             inject(function($injector) {
                sut = contFactory("mods", { $scope: $scope });
             });
         }));

        describe('Definition tests', function(){
            it('Should define the controller', function(){
                expect(sut).toBeDefined();
            });

            var testCases = ['getDatePicker'];

            _.forEach(testCases, function(testCase){
                it(testCase + ' should be defined', function(){
                    expect(sut[testCase]).toBeDefined();
                })
            });
        });


        describe('and calling the getDatePicker function', function () {
            it('and should define getDatePicker',function(){
                expect(sut.getDatePicker).toBeDefined();
            }); 

            var lastCalibration;
            it('return null lastCalibration', function(){
                lastCalibration = sut.lastCalibration(null);   
            });

            it('return true for lastCalibration', function(){

            });


        });

I'm stuck here.. i need to check if else condition, success, and onError.. i have declared varialble.. and stuck if i have to do spyOn. How to write condition for these?

Aucun commentaire:

Enregistrer un commentaire