UNPKG

gaf-mobile

Version:

GAF mobile Web site

93 lines (73 loc) 2.26 kB
'use strict'; describe('Controller: StatsCtrl', function() { // Vars var $scope, $interval, $q, $httpBackend, createCtrl; // Mocks var StatsMock; // Load the controller's module beforeEach(module('gafMobileApp')); // Initialize the controller beforeEach(inject(function($controller, _$rootScope_, _$interval_, _$q_, _$httpBackend_) { $scope = _$rootScope_.$new(); $interval = _$interval_; $q = _$q_; $httpBackend = _$httpBackend_; // Controller constructer to enable to tweak the // mock ups before instantiate it createCtrl = function() { return $controller('StatsCtrl', { $scope: $scope, $interval: $interval, Stats: StatsMock }); }; })); // Create the mock ups beforeEach(function() { var generalStats = { getTotalUsers: function() { return 17126988; }, getTotalProjects: function() { return 8840657; } }; var general24hStats = { get24hUsers: function() { return 10343; }, get24hProjects: function() { return 5511; } }; StatsMock = jasmine.createSpyObj('Stats', ['getGeneralStats', 'get24hStats']); spyOn($interval, 'cancel'); StatsMock.getGeneralStats.and.returnValue($q.when(generalStats)); StatsMock.get24hStats.and.returnValue($q.when(general24hStats)); $httpBackend.when('GET', '/views/main.html').respond(); }); it('should initialize current general stats', function() { var ctrl = createCtrl(); $scope.$digest(); expect(ctrl.totalUsers).toEqual(17126988); expect(ctrl.millionUsers).toEqual(17); expect(ctrl.totalProjects).toEqual(8840657); }); it('should update total users and proj every 5 seconds', function() { var ctrl = createCtrl(); $scope.$digest(); expect(ctrl.totalUsers).toEqual(17126988); expect(ctrl.totalProjects).toEqual(8840657); $interval.flush(50000); expect(ctrl.totalUsers).not.toEqual(17126988); expect(ctrl.totalProjects).not.toEqual(8840657); }); it('should destroy interval once scope is destroyed', function() { createCtrl(); $scope.$digest(); $scope.$destroy(); expect($interval.cancel).toHaveBeenCalled(); }); });