QUnit Test

    ◄ BACK

    ASYNCHRONOUS CALLBACKS TEST CASES

    "use strict";
    var personsArr = [
    	{
    		"firstName": "John",
    		"lastName": "Steward"
    	},
        {
    		"firstName": "Steven",
    		"lastName": "Bombardier"
    	}
    ];
    	
    function getPersons(callback) { 
    	$.ajax(
    	    {
    	        type: "POST",
    	        dataType: "json",
    	        data: "{}",
    	        contentType: "application/json",
    	        url: "persons.json",
    	        success: function(response) {            
    	            callback(response); // The callback parameter is a pointer to a function 
    				//and will be fired once the Ajax request is successfully completed. 
    	        }
    	    }
    	 );   
    }
    
    
    /*********** ACTUAL QUNIT TEST CASES FOR ASYNCHRONOUS CALLBACKS  **************/
    
    module( "JSON syntax test" );
    test("getPerson1", function() {	
    	stop(); // stop progression until Ajax
    	// call above getPersons function
    	getPersons(	 
    		function(response) {
    			try {
    				var persons = response["persons"];
    				var person = persons[1].firstName;
    			}
    			catch(e) {	
    				console.log("check error " + e.message);
    			}
    			finally {
    				equal(person, "Steven","check firstName equality");
    				start();
    			}
    		}
    	);
    });
    test("getPerson2", function() {		
    	stop(); // stop progression until Ajax
    	// call above getPersons function
    	getPersons(	 
    		function(response) {
    			try {
    				var persons = response["persons"];
    				var person = persons[0].lastName;
    			}
    			catch(e) {	
    				console.log("check error " + e.message);
    			}
    			finally {
    				equal(person, "Steward","check lastName equality");
    				start();
    			}
    		}
    	);
    });
    
    module( "JSON local/remote test" );
    test("getPerson3", function() {	
    	stop(); // stop progression until Ajax
    	// call above getPersons function
    	getPersons(	 
    		function(response) {
    			try {
    				var persons = response["persons"];
    				var person = persons[0].lastName;
    			}
    			catch(e) {	
    				console.log("check error " + e.message);
    			}
    			finally {
    				deepEqual(persons, personsArr, "compare JSON from file
                    		with locally declared JSON");
    				start();
    			}
    		}
    	);
    });
    
    test("getPerson4", function() {	
    	stop(); // stop progression until Ajax
        
    	// add additional entry to the local JSON
    	var obj = new Object();
    	obj.firstName = "Johny";
    	obj.lastName = "James";
    	personsArr[2]=obj;
        
    	// call above getPersons function
    	getPersons(	 
    		function(response) {
    			try {
    				var persons = response["persons"];
    				var person = persons[0].lastName;
    			}
    			catch(e) {	
    				console.log("check error " + e.message);
    			}
    			finally {
    				notDeepEqual(persons, personsArr, "compare JSON from file with 
                    		locally declared JSON with locally added entry, not equal");
    				start();
    			}
    		}
    	);
    });
     test("getPerson5", function() {	
    	stop(); // stop progression until Ajax
    	// call above getPersons function
    	getPersons(	 
    		function(response) {
    			// filter only last name keys
    			var str = JSON.stringify(response,function(key,value) {
    				switch(key) {
    					case "firstName":
    						return undefined;
    					case "lastName":
    						return value;
    					default:
    						return value;		
    				}
    			});
    			var obj = JSON.parse(str);
    			try {
    				var persons = obj["persons"];
    				var personLast = persons[0].lastName;
    			}
    			catch(e) {	
    				console.log("check error " + e.message);
    			}
    			finally {
    				equal(personLast, personsArr[0].lastName, "compare JSON lastName 
                    		entry from file with locally declared JSON, filtered method");
    				start();
    			}
    		}
    	);
    });