milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. JavaScript

Change the value of drop down list

Details
Written by: Stanko Milosev
Category: Knockout
Published: 06 September 2013
Last Updated: 06 September 2013
Hits: 3692

Example you can see here. My problem was in this line:

a.notificationRules()[0].notificationRule("Position")

Few examples

Details
Written by: Stanko Milosev
Category: Knockout
Published: 20 August 2013
Last Updated: 01 October 2013
Hits: 3862

I have made few of my examples like:

selectBox

onClick

onChange

htmlBind

template

addSelectBox

addSelectBoxWthOutFunction

Important thing is line:

ko.applyBindings(new ViewModel());

Where ViewModel must be like:

var ViewModel = function () {
	this.myClick = function () {
		alert('Yo! This is a test!');
	}
};

Testing private callbacks

Details
Written by: Stanko Milosev
Category: Jasmine
Published: 17 May 2015
Last Updated: 19 March 2022
Hits: 6354

Recently I had a problem, I couldn't find a way to test private function which was called as a callback from another file.

Let's assume that we have one library like this:

/*global window*/
var executeFunctionToBeCalledAsCallBack;
(function (ns) {
	"use strict";
	var myLocalFunctionToBeCalledAsCallBack;

	function myCallBack(functionToBeCalledAsCallBack) {
		myLocalFunctionToBeCalledAsCallBack = functionToBeCalledAsCallBack;
	}

	function executeFunctionToBeCalledAsCallBack(){
		myLocalFunctionToBeCalledAsCallBack();
	}

	function MyCallbackTest() {
		return {
			myCallBack: myCallBack,
			executeFunctionToBeCalledAsCallBack: executeFunctionToBeCalledAsCallBack
		};
	}
	
	ns.MyCallbackTest = MyCallbackTest;
	ns.myTest = new MyCallbackTest();
}(window));

Here notice first private variable myLocalFunctionToBeCalledAsCallBack, then notice method myCallback that it is assigning his parameter to local variable, and then execution of that variable which is expected to be function, in method executeFunctionToBeCalledAsCallBack. With the line ns.myTest = new MyCallbackTest(); I just created global variable which later I will use to execute my callbacks.

Now, let's use that my "library":

/*global document, window*/
(function (ns) {
    "use strict";

    function myPrivateFunction() {
        document.querySelector("#myTest").innerHTML = "test";
    }

    function ExecuteMyTest (myTest) {
        myTest.myCallBack(myPrivateFunction);
        myTest.executeFunctionToBeCalledAsCallBack();
    }

    ns.ExecuteMyTest = ExecuteMyTest;
}(window));

Here notice two lines:
  myTest.myCallBack(myPrivateFunction);
  myTest.executeFunctionToBeCalledAsCallBack();

With first line I will assign function to local, private, variable, with second I will execute that function.

HTML looks like this:

<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src="/sm.namespaces.js"></script>
    <script type="text/javascript" src="/sm.functions.js" defer></script>
    <script type="text/javascript" src="/sm.index.js" defer></script>
</head>

<body>
<div id="myTest"></div>
<button id="myButton" onClick="window.ExecuteMyTest(window.myTest)">Click Me!</button>
</body>

</html>

Now, we should write a test. Problem is that we can't access prvate function so easy. This is my test:

/*global describe, beforeEach, document, expect, it, window, myTest, jasmine*/
describe("call fake test", function () {
	"use strict";
	var sut,
		myTest;
	beforeEach(function () {
		var myDiv = document.createElement('div'),
			functionToBeCalledAsCallBackTest;
		myDiv.setAttribute("id", "myTest");
		document.body.appendChild(myDiv);
		sut = window.ExecuteMyTest;
		myTest = jasmine.createSpyObj("myTest", ["myCallBack", "executeFunctionToBeCalledAsCallBack"]);
		myTest.myCallBack.and.callFake(function (functionToBeCalledAsCallBack) {
			functionToBeCalledAsCallBackTest = functionToBeCalledAsCallBack;
		});
		sut(myTest);
		functionToBeCalledAsCallBackTest();
	});


	it("div text should be test", function () {
		expect(document.querySelector("#myTest").innerHTML).toBe("test");
	});

	it("expect to be called", function () {
		expect(myTest.myCallBack).toHaveBeenCalled();
	});
});

Here notice first part of code:

myTest = jasmine.createSpyObj("myTest", ["myCallBack", "executeFunctionToBeCalledAsCallBack"]);
myTest.myCallBack.and.callFake(function (functionToBeCalledAsCallBack) {
  functionToBeCalledAsCallBackTest = functionToBeCalledAsCallBack;
}); 

First we created spy object, then we assigned parameter to our local variable, and then we executed that variable, expecting to be a function, with line:

functionToBeCalledAsCallBackTest();

Also, notice code:

myDiv = document.createElement('div'),

and then:

myDiv.setAttribute("id", "myTest");
document.body.appendChild(myDiv);

With part of code I created a div, which will be filled after execution of private function, I need that part to be able to test functionality...

Best would be if we look our tests with Karma coverage report to see how much we covered our code, just comment out line functionToBeCalledAsCallBackTest();, and check karma. 

Here is how my package looks like:

{
  "name": "jasmineExample",
  "version": "0.0.1",
  "devDependencies": {
    "karma": "^0.12.31",
    "karma-chrome-launcher": "^0.1.7",
    "karma-cli": "0.0.4",
    "karma-coverage": "^0.2.7",
    "karma-jasmine": "^0.3.5"
  }
}

Karma config looks like this:

/*global module*/
module.exports = function(config) {
    "use strict";
    config.set({
        basePath: "..",
        plugins: ['karma-chrome-launcher', 'karma-coverage', 'karma-jasmine'],
        frameworks: ['jasmine'],
        files: [
            'sm.namespaces.js',
            'sm.functions.js',
            'sm.index.js',
            'tests/spec/index.spec.js'
        ],

        // coverage reporter generates the coverage
        reporters: ['progress', 'coverage'],
        browsers: ['Chrome'],
        singleRun: true,

        preprocessors: {
            // source files, that you wanna generate coverage for
            // do not include tests or libraries
            // (these files will be instrumented by Istanbul)
            'sm.index.js': ['coverage'],
            'sm.functions.js': ['coverage']
        },

        // optionally, configure the reporter
        coverageReporter: {
            type : 'html',
            dir : 'coverage/'
        }
    });
};

Here notice line:

basePath: "..",

I had to add this line, otherwise if in files section I write something like:

'../sm.namespaces.js',

Karma will loose his path, and it will not be able to load css files anymore.

This my little example, you can see here, jasmine tests are here, everything together you can download from here, but you will have to install karma, go to subfolder tests (in my case that is C:\Users\pera.virtualOne\Downloads\jasmine\tests) execute following command:

npm install

and start Karma with:

karma start karma.conf.js

Now in upper folder you should see coverage folder, in my case here C:\Users\pera.virtualOne\Downloads\jasmine\coverage, go there and execute index.html.

callFake

Details
Written by: Stanko Milosev
Category: Jasmine
Published: 02 February 2015
Last Updated: 03 February 2015
Hits: 4444

JS:

(function(ns) {
	var bar;
	
	function getBar() {
		return bar;
	}
	
	function setBar(value) {
		bar = value;
	}
	
	function MyTest() {
		bar = 5;
		return {
			getBar: getBar,
			setBar: setBar
		}
	}
	
	window.MyTest = MyTest;
	
}(window.MyTest));

Then spec:

describe("call fake test", function () {
	var sut;
	beforeEach(function () {
		sut = new window.MyTest();
		spyOn(sut, "getBar").and.callFake(function() {
			return 1001;
		});
	});
	it("should return 1001", function () {
		expect(sut.getBar()).toBe(1001);
	})
});
describe("create spy then force a call", function () {
	var sut,
	fetchedBar;
	beforeEach(function () {
		sut = new window.MyTest();
		spyOn(sut, "getBar").and.callFake(function() {
			return 1001;
		});
		sut.setBar(123);
		fetchedBar = sut.getBar();
	});
	
	it("tracks that the spy was called", function () {
		expect(sut.getBar).toHaveBeenCalled();
	})	
	
	it("should return 1001", function () {
		expect(sut.getBar()).toBe(1001);
	})
});

With line:

fetchedBar = sut.getBar();

I am executing getBar method, then with line:

expect(sut.getBar).toHaveBeenCalled();

I am sure that getBar method is executed, and with line:

expect(sut.getBar()).toBe(1001);

we can see that fake value is returned.

Example download from here.

  1. One example of Jasmine clock
  2. window.onload
  3. To have been called with
  4. Knockout

Subcategories

Ajax

JavaScript

ExtJS


jQuery

Bing Maps AJAX Control, Version 7.0

Knockout

Jasmine

Moment

node.js

Page 20 of 24

  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24