Skip to content

Instantly share code, notes, and snippets.

@zoltanarvai
Created January 23, 2015 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoltanarvai/7a50c0cfec187e892436 to your computer and use it in GitHub Desktop.
Save zoltanarvai/7a50c0cfec187e892436 to your computer and use it in GitHub Desktop.
Test code that uses Rx throttle() internally
(function () {
'use strict';
angular.module('adbrain.app.reporting').controller('ReportingV2Controller', ReportingV2Controller);
ReportingV2Controller.$inject= ['$scope', 'logger', 'advertisers', 'reportingV2Service'];
function ReportingV2Controller($scope, logger, advertisers, reportingV2Service) {
var vm = this;
init();
function init() {
logger.log(ReportingV2Controller.name, init.name, 'Initializing');
vm.dateFormat = 'DD/MM/YYYY';
vm.startDate = moment().subtract(30, 'days').format(vm.dateFormat);
vm.endDate = moment().format(vm.dateFormat);
vm.advertisers = _.map(advertisers, function(advertiser){
return {
id: advertiser.id,
text: advertiser.name
};
});
//Make sure it is sorted alphabetically
vm.advertisers = _.sortBy(vm.advertisers, 'text');
vm.selectedAdvertiser = vm.advertisers[0].id;
vm.selectedPrimaryContents = [];
//Specify refresh logic
var refreshStream = Rx.Observable.fromWatchGroup($scope, [
'vm.dimension1',
'vm.dimension2',
'vm.selectedAdvertiser',
'vm.selectedPrimaryContents',
'vm.startDate',
'vm.endDate'
]);
//THIS ARE THE IMPORTANT BITS!
var resultStream = refreshStream
.throttle(350, !!window.jasmine ? Rx.Scheduler.immediate : null) //==> This is the critical bit!
.map(function(){
var queryBuilder = new Adbrain.Model.ReportingQueryBuilder();
var query = queryBuilder.buildChartQuery(vm.selectedAdvertiser);
return query;
})
.distinctUntilChanged()
.select(reportingV2Service.getReportStream)
.switchLatest();
resultStream.forEach(function(reportData){
vm.result = reportData;
});
}
}
})();
//THIS IS THE TEST
it('should run query when dimension1 is changed', function(){
//Arrange
$http.when('POST', api.route(api.app.reports.report)).respond([]);
var controller = createController();
//Act
controller.dimension1 = 'test';
$scope.$digest();
$http.flush();
//Assert
expect(controller.result).toBeDefined();
});
@zoltanarvai
Copy link
Author

So the questions is how can you specify the scheduler behaviour without injecting it explicitly?
I want to remove this .throttle(350, !!window.jasmine ? Rx.Scheduler.immediate : null) and use just throttle(350).

It should be the testing environment's concern to specify this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment