pyc-website

main website for pyc inc.

git clone https://9o.is/git/pyc-website.git

Forms.spec.js

(2653B)


      1 // simulate Lift objects
      2 window.lift_page = "F1002111409932BP3JRX";
      3 var ajaxhandler = "F1002111409938WP5FBR";
      4 
      5 describe('Forms', function() {
      6 	var $httpBackend, $rootScope, createController;
      7 	
      8 	
      9 	
     10 
     11 
     12 	beforeEach(function() {
     13 		module('Forms');
     14 		
     15 		inject(function($injector) {	
     16 			$httpBackend = $injector.get('$httpBackend');
     17 
     18 			// if key is correct, responds with success (for Lift Promise requests)
     19 			$httpBackend.when('POST', '/ajax_request/'+window.lift_page).respond({
     20 				msg_type : 'success', msg: '<span>You Got It!</span>'});
     21 	    });
     22 	});
     23 	
     24 	describe('FormCtrl', function() {
     25 		
     26 		beforeEach(inject(function($injector) {
     27 			
     28 			$rootScope = $injector.get('$rootScope');
     29 			var $controller = $injector.get('$controller');
     30 			
     31 			createController = function() {
     32 				return $controller('FormCtrl', {
     33 					'$scope' : $rootScope
     34 				});
     35 			};
     36 		}));
     37 		
     38 		it('works as is with stateSuccess', function() {
     39 			createController();
     40 			expect($rootScope.stateSuccess('email')).toBe(
     41 				"{'state-success':form.email.$valid && !form.email.$pristine}"	
     42 			);
     43 		});
     44 		
     45 		it('works as is with stateSuccessError', function() {
     46 			createController();
     47 			expect($rootScope.stateSuccessError('email')).toBe(
     48 				"{'state-error':form.email.$invalid && !form.email.$pristine,"+
     49 				"'state-success':form.email.$valid && !form.email.$pristine}"	
     50 			);
     51 		});
     52 		
     53 		it('submit function sets loading variable', function() {
     54 			createController();
     55 			expect($rootScope.loading !== true);
     56 			$rootScope.submit('TestPromise', 'testSubmit', function(){}, function(){});
     57 			expect($rootScope.loading).toBe(true);
     58 		});
     59 		
     60 		// TODO: not receiving response from test lift promise
     61 		it('submit function with Lift Promise sends and receives', function() {
     62 			createController();
     63 			
     64 			$rootScope.key = 'secret';
     65 			
     66 			$rootScope.submit('TestPromise', 'testSubmit', function(){
     67 				expect(false); // success func is called, that's correct
     68 			}, function(){
     69 				expect(false); // failure func is called, that's incorrect
     70 			});
     71 		});
     72 	});
     73 
     74 	afterEach(function() {
     75 		$httpBackend.verifyNoOutstandingExpectation();
     76 		$httpBackend.verifyNoOutstandingRequest();
     77 	});
     78 	
     79 	// url: /ajax_request/{lift_page}
     80 	// form data: {ajaxhandler} : {guid, funcName, payload}
     81 	window.TestPromise = {
     82 		"_call_server": function(v) {
     83 			window.liftAjax.lift_ajaxHandler(ajaxhandler + '=' + 
     84 					encodeURIComponent(JSON.stringify(v)), null, null, null);
     85 		}, 
     86 		"testSubmit": function(param) {
     87 		  var promise = new window.liftAjax.Promise();
     88 		  window.liftAjax.associate(promise);
     89 		  this._call_server({guid: promise.guid, name: "testSubmit", payload: param});
     90 		  return promise;
     91 		}
     92 	};
     93 });