/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2013 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*  
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License along with this program.  If not, see http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. 
*/

describe("LocaleController", function () {

    beforeEach(module('openlmis-locale'));

    var messageService, alertService, notificationService;

    beforeEach(inject(function ($rootScope, $q, _messageService_, _alertService_,
                                _notificationService_) {

        messageService = _messageService_;
        alertService = _alertService_;
        notificationService = _notificationService_;

        var mockLocale = undefined;
        spyOn(messageService, 'populate').andCallFake(function(lang){
            var deferred = $q.defer();
            if(lang == 'fail'){
                deferred.reject();
            } else {
                mockLocale = lang;
                $rootScope.$broadcast('openlmis.messages.populated');
                deferred.resolve();
            }
            return deferred.promise;
        });
        spyOn(messageService, 'getCurrentLocale').andCallFake(function(){
            return mockLocale;
        });

        spyOn(notificationService, 'success');
        spyOn(alertService, 'error');
    }));

    describe('on start up', function(){
        var scope, controller;

        beforeEach(inject(function($rootScope){
            scope = $rootScope.$new();
        }))

        it('loads the default locale when messageService locale not set', inject(function($controller){
            controller = $controller('LocaleController', {
                $scope: scope,
                messageService: messageService,
                alertService: alertService,
                notificationService: notificationService
            });

            expect(messageService.populate).toHaveBeenCalled();
        }));

        it("does't load the default locale when the messageService locale is set", inject(function($controller){
            messageService.populate('foo');

            controller = $controller('LocaleController', {
                $scope: scope,
                messageService: messageService,
                alertService: alertService,
                notificationService: notificationService
            });

            // messageService.populate was only called at top
            expect(messageService.populate.calls.length).toBe(1);
        }));
    });

    describe('while running', function(){
        var scope, controller;

        beforeEach(inject(function($rootScope, $controller){
            scope = $rootScope.$new();
            messageService.currentLocale = 'javascript';

            controller = $controller('LocaleController', {
                $scope: scope,
                messageService: messageService,
                alertService: alertService,
                notificationService: notificationService
            });
        }));

        it("can change locale", function () {
            controller.changeLocale('pt');
            scope.$apply();

            expect(messageService.populate).toHaveBeenCalledWith('pt');
            expect(notificationService.success).toHaveBeenCalled();
        });

        it("will throw an error if changing locale is unsuccessful", function () {
            // spy on populate will reject on fail...
            controller.changeLocale('fail');
            scope.$apply();

            expect(alertService.error).toHaveBeenCalled();
        });

        it("updates selectedLocale when messages are populated", function(){
            messageService.populate('foo');
            scope.$apply();
            expect(controller.selectedLocale).toBe('foo');

            messageService.populate('bar');
            scope.$apply();
            expect(controller.selectedLocale).toBe('bar');
        });
    });

});

@@ -971,18 +971,217 @@
 nService
+, openlmisLanguages;%0A%0A    beforeEach(function()%7B%0A        angular.mock.module(%22openlmis-config%22, function($provide)%7B%0A            $provide.constant('OPENLMIS_LANGUAGES', %5B'en', 'fr'%5D)%0A        %7D)%0A    %7D)
 ;%0A
-
 %0A    bef
@@ -1308,16 +1308,38 @@
 Service_
+, _OPENLMIS_LANGUAGES_
 ) %7B%0A%0A   
@@ -1469,16 +1469,66 @@
 ervice_;
+%0A        openlmisLanguages = _OPENLMIS_LANGUAGES_;
 %0A%0A      
@@ -2322,32 +2322,32 @@
 otScope.$new();%0A
-
         %7D))%0A%0A   
@@ -2343,16 +2343,531 @@
    %7D))%0A%0A
+        it(%22loads list of avaliable locales%22, inject(function($controller) %7B%0A            var expectedLocales = %5B'en', 'fr'%5D;%0A            controller = $controller('LocaleController', %7B%0A                $scope: scope,%0A                messageService: messageService,%0A                alertService: alertService,%0A                notificationService: notificationService,%0A                OPENLMIS_LANGUAGES: openlmisLanguages%0A            %7D);%0A%0A            expect(controller.locales).toEqual(expectedLocales);%0A        %7D));%0A%0A
         

Add unit test to locale-controller to test dynamically generated list of languages