Skip to content

Commit

Permalink
Merge pull request #15 from transmart/sascha-developments
Browse files Browse the repository at this point in the history
lots of tests for fetchButton
  • Loading branch information
rnugraha committed Apr 15, 2016
2 parents 67aae7f + 9c4aa85 commit 3d30a2f
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 38 deletions.
98 changes: 92 additions & 6 deletions test/unit/javascript/fetchButtonDirectiveTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,112 @@ describe('fetchButton', function() {
var $compile,
$rootScope,
$httpBackend,
element;
$q,
element,
smartRUtils,
rServeService;

beforeEach(module('smartRApp'));
beforeEach(module('smartRTemplates'));

beforeEach(inject(function(_$compile_, _$rootScope_, _$httpBackend_) {
beforeEach(inject(function(_$compile_, _$rootScope_, _$httpBackend_, _$q_, _smartRUtils_, _rServeService_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
$q = _$q_;
smartRUtils = _smartRUtils_;
rServeService = _rServeService_;

element = $compile(angular.element('<fetch-button></fetch-button>'))($rootScope);
$rootScope.conceptMap = {};

element = $compile(angular.element('<fetch-button concept-map="conceptMap"></fetch-button>'))($rootScope);
$rootScope.$digest();
}));

it('replaces element with content', function() {
var _clickButton = function(loadDataIntoSessionReturn, executeSummaryStatsReturn) {
var defer1 = $q.defer();
var defer2 = $q.defer();
spyOn(rServeService, 'loadDataIntoSession').and.returnValue(defer1.promise);
spyOn(rServeService, 'executeSummaryStats').and.returnValue(defer2.promise);

element.find('input').click();

if (loadDataIntoSessionReturn) {
eval('defer1.' + loadDataIntoSessionReturn);
$rootScope.$digest();
}


if (executeSummaryStatsReturn) {
eval('defer2.' + executeSummaryStatsReturn);
$rootScope.$digest();
}
};

var _prepareScope = function(cohorts, showSummaryStats, conceptMap) {
spyOn(smartRUtils, 'countCohorts').and.returnValue(cohorts);
element.isolateScope().showSummaryStats = showSummaryStats;
element.isolateScope().conceptMap = conceptMap;
$rootScope.$digest();
};

it('replaces element with content', function() {
expect(element.find('input')).toBeDefined();
expect(element.find('span')).toBeDefined();
});

it('should contain initially no text', function() {
expect(element.find('span').text()).toEqual('');
it('should have the correct scope when clicked', function() {
try { // we just want to see if the progress message is there
element.find('input').click();
} catch(e) {}
expect(element.find('span').text()).toContain('Fetching data');
expect(element.isolateScope().running).toBe(true);
expect(element.isolateScope().loaded).toBe(false);
expect(element.isolateScope().subsets).toEqual(0);
expect(element.isolateScope().allSamples).toEqual(0);
});

it('should show another text after data is loaded if showSummaryStats is enabled', function() {
_prepareScope(1, true, {foo: ['concept']});
try { // we just want to see if the progress message is there
_clickButton('resolve()');
} catch (e) {}
expect(element.find('span').text()).toContain('Execute summary statistics');
expect(element.isolateScope().running).toBe(true);
expect(element.isolateScope().loaded).toBe(false);
expect(element.isolateScope().subsets).toEqual(0);
expect(element.isolateScope().allSamples).toEqual(0);
});

it('should have the correct scope when successful without showSummaryStats', function() {
_prepareScope(2, false, {foo: ['concept']});
_clickButton('resolve()');
expect(element.find('span').text()).toBe('Task complete!');
expect(element.isolateScope().running).toBe(false);
expect(element.isolateScope().loaded).toBe(true);
expect(element.isolateScope().subsets).toEqual(2);
expect(element.isolateScope().allSamples).toEqual(0);
});

it('should have the correct scope when successful with showSummaryStats', function() {
_prepareScope(1, true, {foo: ['concept']});
_clickButton('resolve()', 'resolve({result: {allSamples: 1337, subsets: null}})');
expect(element.find('span').text()).toBe('Task complete!');
expect(element.isolateScope().running).toBe(false);
expect(element.isolateScope().loaded).toBe(true);
expect(element.isolateScope().subsets).toEqual(1);
expect(element.isolateScope().allSamples).toEqual(1337);
});

it('should show error due to missing cohorts', function() {
_prepareScope(0, false, {foo: ['concept']});
_clickButton();
expect(element.find('span').text()).toBe('Error: No cohorts selected!');
});

it('should show error due to missing concepts', function() {
_prepareScope(1, false, {});
_clickButton();
expect(element.find('span').text()).toBe('Error: No concepts selected!');
});
});
74 changes: 42 additions & 32 deletions web-app/js/smartR/_angular/directives/fetchButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ window.smartRApp.directive('fetchButton', [
return {
restrict: 'E',
scope: {
loaded: '=',
running: '=',
conceptMap: '=',
loaded: '=?',
running: '=?',
biomarkers: '=?',
showSummaryStats: '=?',
summaryData: '=?',
Expand All @@ -25,25 +25,16 @@ window.smartRApp.directive('fetchButton', [
var template_btn = element.children()[0],
template_msg = element.children()[1];

var _init = function () {
template_btn.disabled = true;
scope.summaryData = {}; // reset
scope.allSamples = 0;
scope.subsets = 0;
scope.running = true;
template_msg.innerHTML = 'Fetching data, please wait <span class="blink_me">_</span>';
};

var _onSuccess = function() {
template_msg.innerHTML = 'Task completed!';
template_msg.innerHTML = 'Task complete!';
scope.subsets = smartRUtils.countCohorts();
scope.loaded = true;
template_btn.disabled = false;
scope.running = false;
};

var _onFailure = function(msg) {
template_msg.innerHTML = 'Failure: ' + msg;
template_msg.innerHTML = 'Error: ' + msg;
scope.loaded = false;
template_btn.disabled = false;
scope.running = false;
Expand All @@ -52,15 +43,15 @@ window.smartRApp.directive('fetchButton', [
// we add this conditional $watch because there is some crazy promise resolving for allSamples
// going on. This is a workaround which observes allSamples and uses it as criteria for successful
// completion.
if (scope.showSummaryStats) {
scope.$watch('summaryData', function(newValue) {
// prevents initial firing
if (scope.running && Object.keys(newValue).indexOf('subsets') !== -1) {
scope.allSamples = scope.summaryData.allSamples;
_onSuccess();
}
}, true);
}
scope.$watch('summaryData', function(newValue) {
if (scope.summaryData &&
scope.showSummaryStats &&
scope.running &&
Object.keys(newValue).indexOf('subsets') !== -1) {
scope.allSamples = newValue.allSamples;
_onSuccess();
}
}, true);

var _getDataConstraints = function (biomarkers) {
if (typeof biomarkers !== 'undefined' && biomarkers.length > 0) {
Expand All @@ -78,22 +69,41 @@ window.smartRApp.directive('fetchButton', [
var _afterDataFetched = function() {
if (!scope.showSummaryStats) {
_onSuccess();
return;
}
} else {
template_msg.innerHTML =
'Execute summary statistics, please wait <span class="blink_me">_</span>';

template_msg.innerHTML =
'Execute summary statistics, please wait <span class="blink_me">_</span>';

return rServeService.executeSummaryStats('fetch')
.then(function(data) {
scope.summaryData = data.result;
}, _onFailure);
rServeService.executeSummaryStats('fetch')
.then(
function(data) { scope.summaryData = data.result; }, // this will trigger $watch
_onFailure
);
}
};

template_btn.onclick = function() {
_init();
scope.summaryData = {};
scope.allSamples = 0;
scope.subsets = 0;
scope.loaded = false;
scope.running = true;

template_btn.disabled = true;
template_msg.innerHTML = 'Fetching data, please wait <span class="blink_me">_</span>';

if (smartRUtils.countCohorts() === 0) {
_onFailure('No cohorts selected!');
return;
}

var conceptKeys = smartRUtils.conceptBoxMapToConceptKeys(scope.conceptMap);
if ($.isEmptyObject(conceptKeys)) {
_onFailure('No concepts selected!');
return;
}

var dataConstraints = _getDataConstraints(scope.biomarkers);

rServeService.loadDataIntoSession(conceptKeys, dataConstraints)
.then(_afterDataFetched, _onFailure);
};
Expand Down

0 comments on commit 3d30a2f

Please sign in to comment.