jhanink
10/2/2013 - 5:15 PM

reportDashboardUnitTests.js

describe('Controller tests', function() {

    beforeEach(module('myApp.controllers'));
    beforeEach(module('myApp.services'));
    beforeEach(module('myApp.filters'));
    beforeEach(module('ui.bootstrap'));
    beforeEach(module('ngCookies'));
    beforeEach(module('ngRoute'));
    beforeEach(module('MacAppServiceMocks'));

    // disable gLog debug messages
    gDebugLevel = DEBUG_LEVEL_OFF;
    var mls;

    describe('Report Dashboard Controller', function() {

        var scope, http, ctrl, appUtils, goalService, rand;

        KM_DATA = angular.copy(MOCK_DATA_KEY_METRICS);
        CATEGORY_DATA = angular.copy(MOCK_DATA_CATEGORIES);

        beforeEach(inject(function(_$httpBackend_, $rootScope, $controller, MacAppUtils, KeyMetricsGoalService, $filter) {
            KM_DATA = angular.copy(KM_DATA);
            http = _$httpBackend_;

            scope = MOCK_getReportDashboardScope($rootScope);
            mls = $filter('mls');

            appUtils = MacAppUtils;
            goalService = KeyMetricsGoalService;

            var globalCtrl = $controller('GlobalCtrl', {$scope:scope, MacAppUtils:appUtils});
            ctrl = $controller('ReportDashboardCtrl', {$scope:scope, MacAppUtils:appUtils, KeyMetricsGoalService:goalService});

            rdpCtrl = ctrl;
            scope.gState.sessionId = "12345";
            rand = appUtils.getNextRandom();
            scope.init();
        }));

        afterEach(function(){
            scope.init();
        });


        it('should load categories', loadCategories = function() {
            expect(appUtils.getServiceName()).toBe("MacAppUtilsMock");
            http.expectGET( MOCK_GET_URL_CATEGORIES+"?r="+ rand +'&callingContext=' ).respond( CATEGORY_DATA );
            scope.loadCategories();
            http.flush();
            expect('0').toBe(scope.rdData.categories[0].id);
            expect(scope.rdData.categories[1].name).toBe('MOCK Large Kitchen Appliances');
            // be a good citizen and clean up
            scope.rdData.categories.shift();
        });


        it('should set key metrics range filter', setKeyMetricsRangeFilter = function() {
            // set dummy values
            http.expectGET( MOCK_GET_URL_KEY_METRICS_L30D+"&r="+ rand +'&callingContext=').respond( KM_DATA );
            scope.rdHelper.setActiveKeyMetricsDateRange(Const.L30D);
            expect(Const.L30D).toBe(scope.queryFilters.keyMetricsDateRange);

            http.expectGET( MOCK_GET_URL_KEY_METRICS_MTD+"&r="+rand+'&callingContext=' ).respond( KM_DATA );
            scope.rdHelper.setActiveKeyMetricsDateRange(Const.MTD);
            expect(Const.MTD).toBe(scope.queryFilters.keyMetricsDateRange);

            http.expectGET( MOCK_GET_URL_KEY_METRICS_TODAY+"&r="+rand +'&callingContext=' ).respond( KM_DATA );
            scope.rdHelper.setActiveKeyMetricsDateRange(Const.TODAY);
            expect(Const.TODAY).toBe(scope.queryFilters.keyMetricsDateRange);
        });

        it('should load key metrics', loadKeyMetrics = function() {
            // make sure this is not cached -
            scope.gDataCache.reportDashboard.keyMetrics.mtd = null;
            http.expectGET( MOCK_GET_URL_KEY_METRICS_L30D+"&r="+rand +'&callingContext=').respond( KM_DATA );
            scope.loadKeyMetrics();
            http.flush();
            expect(4).toBe(scope.rdData.keyMetrics.clicks.metricId)
        });

        it('should load trend metrics', loadTrendMetrics = function() {
            loadKeyMetrics();

            http.expectGET( MOCK_GET_URL_TREND_METRICS+"&r="+scope.gState.sessionId +'&callingContext=').respond( MOCK_DATA_TREND_METRICS );
            scope.loadTrendMetrics();
            http.flush();
            expect(2790).toBe(scope.rdData.trendMetricsRawData.totalClicks[0].currentYearsValue);
        });

        it('should reload trend metrics', function() {
            loadKeyMetrics();

            spyOn(scope, 'loadTrendMetrics');
            scope.rdHelper.reloadAllCharts();
            expect(scope.loadTrendMetrics).toHaveBeenCalled();
        });

        it ('should be able to reload chart tables', function() {
            var chart1 = scope.rdState.charts[0];
            var chart2 = scope.rdState.charts[1];

            expect(Const.Charts.Type.GRAPH).toBe(chart1.chartType);
            expect(Const.Charts.Type.GRAPH).toBe(chart2.chartType);

            chart1.chartType = Const.Charts.Type.TABLE;

            var chart1RedrawValue = chart1.forceRedrawChart;
            var chart2RedrawValue = chart2.forceRedrawChart;

            scope.rdHelper.forceRedrawAllTables();

            expect(chart1RedrawValue).not.toBe(chart1.forceRedrawChart)
            expect(chart2.forceRedrawChart).toBeUndefined()
        })

        it('should handle basic key metrics goal-related operations', function() {
            loadKeyMetrics();
            scope.rdData.keyMetrics = KM_DATA.campaignSummary;

            var goalState = {okToValidate: {}};
            goalState.okToValidate[Const.Metrics.COS] = true;
            goalState.okToValidate[Const.Metrics.CTS] = true;
            scope.kmGoals._goalState = goalState;

            expect(scope.kmGoals.getGoalValue(Const.Metrics.CTS)).toBe(2);
            expect('%').toBe(scope.kmGoals.getGoalValueSign(Const.Metrics.COS));

            expect(false).toBe(scope.kmGoals.isEditingGoal(Const.Metrics.CTS));
            expect(false).toBe(scope.kmGoals.isEditingGoal(Const.Metrics.COS));

            scope.kmGoals.toggleEditGoal(Const.Metrics.CTS);
            expect(true).toBe(scope.kmGoals.isEditingGoal(Const.Metrics.CTS));
            expect(false).toBe(scope.kmGoals.isEditingGoal(Const.Metrics.COS));

            scope.kmGoals.toggleEditGoal(Const.Metrics.CTS);
            expect(false).toBe(scope.kmGoals.isEditingGoal(Const.Metrics.CTS));
            expect(false).toBe(scope.kmGoals.isEditingGoal(Const.Metrics.COS));

            expect(true).toBe(scope.kmGoals.validateGoal(Const.Metrics.COS,1));
            expect(true).toBe(scope.kmGoals.validateGoal(Const.Metrics.COS,50.5));
            expect(true).toBe(scope.kmGoals.validateGoal(Const.Metrics.COS,100));
            expect(true).toBe(scope.kmGoals.validateGoal(Const.Metrics.COS,100.0));
            expect(false).toBe(scope.kmGoals.validateGoal(Const.Metrics.COS,99.99999));
            expect(false).toBe(scope.kmGoals.validateGoal(Const.Metrics.CTS,-1));
            expect(false).toBe(scope.kmGoals.validateGoal(Const.Metrics.CTS,0));
            expect(false).toBe(scope.kmGoals.validateGoal(Const.Metrics.CTS,101));
            expect(false).toBe(scope.kmGoals.validateGoal(Const.Metrics.CTS,'a'));
        });

        it('should exercise deeper key metrics goal operations', function() {
            loadKeyMetrics();

            spyOn(scope.kmGoals, 'canHaveGoal');
            scope.kmGoals.hasGoal(Const.Metrics.CTS);
            expect(scope.kmGoals.canHaveGoal).toHaveBeenCalled();
        });

        it('should handle key metrics save goal', function() {
            loadKeyMetrics();

            // for save goal
            http.expectPOST( MOCK_GET_URL_SAVE_KEY_METRIC_GOAL).respond('');
            // for subsequent chart refresh
            http.expectGET(MOCK_GET_URL_TREND_METRICS+"&r="+scope.gState.sessionId).respond(MOCK_DATA_TREND_METRICS);
            scope.gDataCache.reportDashboard.keyMetrics.mtd = null;

            scope.rdData.keyMetrics[Const.Metrics.CTS].goalValue = 1.99;
            scope.kmGoals.saveGoal(Const.Metrics.CTS);
            //http.flush();
        });

        it('should handle key metrics delete goal', function() {
            loadKeyMetrics();

            spyOn(scope.kmGoals, 'saveGoal');
            scope.kmGoals.deleteGoal(Const.Metrics.CTS);
            expect(scope.kmGoals.saveGoal).toHaveBeenCalled();
        });

        it('should know the difference between ROI enabled and disabled', function() {
            scope.gState.user.roiEnabled = false;
            expect(scope.rdHelper.getKeyMetricsClassName()).toBe('gHorizontalListHalfWidth');
            scope.gState.user.roiEnabled = true;
            expect(scope.rdHelper.getKeyMetricsClassName()).toBe('gHorizontalList');

            scope.gState.user.roiEnabled = false;
            scope.init();
            expect(scope.rdState.charts.length).toBe(3);

            scope.gState.user.roiEnabled = true;
            scope.init();
            expect(scope.rdState.charts.length).toBe(7);

            scope.gState.user.roiEnabled = false;
            scope.rdState.goals = {};
            scope.init();
            loadKeyMetrics();
            expect(scope.rdState.goals.clicks).not.toBe(null);
            expect(scope.rdState.goals.cts).toBeUndefined();

            scope.gState.user.roiEnabled = true;
            scope.init();
            loadKeyMetrics();
            expect(scope.rdState.goals.clicks).not.toBe(null);
            expect(scope.rdState.goals.cts).not.toBe(null);


            spyOn(scope.rdHelper, 'setMetricDataForChart');

            scope.gState.user.roiEnabled = false;
            scope.rdState.goals = {};
            scope.init();
            loadTrendMetrics();

            expect(scope.rdHelper.setMetricDataForChart).toHaveBeenCalledWith(Const.Metrics.TOTAL_CLICKS, scope.rdData.trendMetricsRawData);
            expect(scope.rdHelper.setMetricDataForChart).toHaveBeenCalledWith(Const.Metrics.TOTAL_COST, scope.rdData.trendMetricsRawData);
            expect(scope.rdHelper.setMetricDataForChart).toHaveBeenCalledWith(Const.Metrics.CPC, scope.rdData.trendMetricsRawData);

            expect(scope.rdHelper.setMetricDataForChart).not.toHaveBeenCalledWith(Const.Metrics.COS, scope.rdData.trendMetricsRawData);
            expect(scope.rdHelper.setMetricDataForChart).not.toHaveBeenCalledWith(Const.Metrics.CTS, scope.rdData.trendMetricsRawData);
            expect(scope.rdHelper.setMetricDataForChart).not.toHaveBeenCalledWith(Const.Metrics.SALES, scope.rdData.trendMetricsRawData);
            expect(scope.rdHelper.setMetricDataForChart).not.toHaveBeenCalledWith(Const.Metrics.ORDERS, scope.rdData.trendMetricsRawData);
        });

        it('should handle daily, weekly, monthly trend views', function() {
            scope.init();
            loadKeyMetrics();

            http.expectGET( MOCK_GET_URL_TREND_METRICS_WEEKLY+"&r="+scope.gState.sessionId +'&callingContext=').respond( MOCK_DATA_TREND_METRICS );
            scope.rdHelper.setTrendMetricsDateRange('WEEKLY');
            expect(scope.rdHelper.isTrendView('WEEKLY')).toBe(true);

            http.expectGET( MOCK_GET_URL_TREND_METRICS_MONTHLY+"&r="+scope.gState.sessionId +'&callingContext=').respond( MOCK_DATA_TREND_METRICS );
            scope.rdHelper.setTrendMetricsDateRange('MONTHLY');
            expect(scope.rdHelper.isTrendView('MONTHLY')).toBe(true);

            http.expectGET( MOCK_GET_URL_TREND_METRICS+"&r="+scope.gState.sessionId +'&callingContext=').respond( MOCK_DATA_TREND_METRICS );
            scope.rdHelper.setTrendMetricsDateRange('DAILY');
            expect(scope.rdHelper.isTrendView('DAILY')).toBe(true);
        });

        it('should load category-specific trend data when selecting a category', function() {
            scope.init();
            loadKeyMetrics();

            expect(scope.queryFilters.categoryId).toBe('0');
            scope.queryFilters.categoryId = 1728;

            http.expectGET( MOCK_GET_URL_TREND_METRICS_CATEGORY_ID_1728+"&r="+scope.gState.sessionId + '&callingContext=').respond( MOCK_DATA_TREND_METRICS );
            scope.rdHelper.reloadAllCharts();
        });

        it('should load year over year trend data when selecting compare to last year', function() {
            scope.init();
            loadKeyMetrics();

            expect(scope.rdState.trendMetricsShowLastYear).toBe(false);
            scope.rdState.trendMetricsShowLastYear = true;

            http.expectGET( MOCK_GET_URL_TREND_METRICS+"&r="+scope.gState.sessionId +'&callingContext=').respond( MOCK_DATA_TREND_METRICS );
            scope.rdHelper.reloadAllCharts();
        });

        it('should build a good localData instance object to supply to the directive-scoped HighCharts config', function() {
            scope.init();
            loadKeyMetrics();
            loadTrendMetrics();
            expect(scope.rdHelper.isTrendView(Const.DAILY)).toBe(true);

            var localData = scope.rdHelper.buildLocalDataForHighChartsConfig('totalClicks', scope.rdData.trendMetricsRawData);

            expect(localData.currentYearSeries.length).toBe(30);
            expect(localData.yMin).not.toBe(9999999);
            expect(localData.tableData.length).not.toBe(0);
        });
    });
});