(function () {
    'use strict';
    describe('Checkbox Directive', function () {
        var $compile;
        var $rootScope;
        var scope;
        var element;
        beforeEach(angular.mock.module('ti_common'));
        beforeEach(function () {
            inject(function (_$compile_, _$rootScope_) {
                $compile = _$compile_;
                $rootScope = _$rootScope_;
            });
        });
        beforeEach(function () {
            var rawHtml = '<div>' +
                '<div ng-repeat="person in model.people" class="person">' +
                'test' +
                '<label>' +
                '<tic-checkbox child-list="person.fruits" child-boolean-property="eaten" ng-model="person.allEaten"> {{person.name}} [All eaten: {{person.allEaten}}]' +
                '</tic-checkbox>' +
                '</label>' +
                '<div ng-repeat="fruit in person.fruits" class="child-list">' +
                '<label>' +
                '<tic-checkbox ng-model="fruit.eaten"> {{fruit.type}} : {{fruit.eaten}} </tic-checkbox>' +
                '</label>' +
                '</div>' +
                '</div>' +
                '</div>';
            scope = $rootScope.$new();
            scope.model = {
                allEaten: false,
                people: [{
                        name: 'Bob',
                        fruits: [{
                                type: 'Apple',
                                eaten: false
                            }, {
                                type: 'Banana',
                                eaten: false
                            }, {
                                type: 'Pear',
                                eaten: true
                            }, {
                                type: 'Tomato',
                                eaten: false
                            }, {
                                type: 'Grapefruit',
                                eaten: true
                            },]
                    }, {
                        name: 'Joe',
                        fruits: [{
                                type: 'Apple',
                                eaten: true
                            }, {
                                type: 'Banana',
                                eaten: true
                            }, {
                                type: 'Pear',
                                eaten: true
                            }, {
                                type: 'Tomato',
                                eaten: true
                            }, {
                                type: 'Grapefruit',
                                eaten: true
                            },]
                    }]
            };
            element = $compile(rawHtml)(scope);
            scope.$digest();
            // console.log(element);
            // console.log(element.html());
        });
        it('should render default values correctly', function () {
            var checkboxes = element.find('i');
            var numberOfCheckboxes = checkboxes.length;
            expect(numberOfCheckboxes).toBe(12);
            expect(angular.element(checkboxes[0]).html()).toBe('indeterminate_check_box');
            expect(angular.element(checkboxes[1]).html()).toBe('check_box_outline_blank');
            expect(angular.element(checkboxes[2]).html()).toBe('check_box_outline_blank');
            expect(angular.element(checkboxes[3]).html()).toBe('check_box');
            expect(angular.element(checkboxes[4]).html()).toBe('check_box_outline_blank');
            expect(angular.element(checkboxes[5]).html()).toBe('check_box');
            expect(angular.element(checkboxes[6]).html()).toBe('check_box');
            expect(angular.element(checkboxes[7]).html()).toBe('check_box');
            expect(angular.element(checkboxes[8]).html()).toBe('check_box');
            expect(angular.element(checkboxes[9]).html()).toBe('check_box');
            expect(angular.element(checkboxes[10]).html()).toBe('check_box');
            expect(angular.element(checkboxes[11]).html()).toBe('check_box');
        });
        it('should change master checkbox to indeterminate_check_box when the children contain a mixture of checked and unchecked', function () {
            var checkboxes = element.find('i');
            checkboxes[7].click();
            expect(angular.element(checkboxes[7]).html()).toBe('check_box_outline_blank');
            expect(angular.element(checkboxes[6]).html()).toBe('indeterminate_check_box');
        });
        it('should change master checkbox to indeterminate_check_box when the children contain a mixture of checked and unchecked', function () {
            var checkboxes = element.find('i');
            checkboxes[7].click();
            expect(angular.element(checkboxes[7]).html()).toBe('check_box_outline_blank');
            console.log(angular.element(checkboxes[6]).html());
            expect(angular.element(checkboxes[6]).html()).toBe('indeterminate_check_box');
        });
        it('should change master checkbox to check_box when the sub children changed to all checked', function () {
            var checkboxes = element.find('i');
            checkboxes[1].click();
            checkboxes[2].click();
            checkboxes[4].click();
            expect(angular.element(checkboxes[1]).html()).toBe('check_box');
            expect(angular.element(checkboxes[2]).html()).toBe('check_box');
            expect(angular.element(checkboxes[4]).html()).toBe('check_box');
            console.log(angular.element(checkboxes[0]).html());
            expect(angular.element(checkboxes[0]).html()).toBe('check_box');
        });
        it('should change master checkbox to check_box_outline_blank when the sub children changed to all unchecked', function () {
            var checkboxes = element.find('i');
            checkboxes[3].click();
            checkboxes[5].click();
            expect(angular.element(checkboxes[3]).html()).toBe('check_box_outline_blank');
            expect(angular.element(checkboxes[5]).html()).toBe('check_box_outline_blank');
            console.log(angular.element(checkboxes[0]).html());
            expect(angular.element(checkboxes[0]).html()).toBe('check_box_outline_blank');
        });
        it('should change master checkbox from check_box to indeterminate_check_box to check_box_outline_blank', function () {
            var checkboxes = element.find('i');
            expect(angular.element(checkboxes[6]).html()).toBe('check_box');
            checkboxes[7].click();
            expect(angular.element(checkboxes[6]).html()).toBe('indeterminate_check_box');
            checkboxes[8].click();
            expect(angular.element(checkboxes[6]).html()).toBe('indeterminate_check_box');
            checkboxes[9].click();
            expect(angular.element(checkboxes[6]).html()).toBe('indeterminate_check_box');
            checkboxes[10].click();
            expect(angular.element(checkboxes[6]).html()).toBe('indeterminate_check_box');
            checkboxes[11].click();
            expect(angular.element(checkboxes[7]).html()).toBe('check_box_outline_blank');
            expect(angular.element(checkboxes[8]).html()).toBe('check_box_outline_blank');
            expect(angular.element(checkboxes[9]).html()).toBe('check_box_outline_blank');
            expect(angular.element(checkboxes[10]).html()).toBe('check_box_outline_blank');
            expect(angular.element(checkboxes[11]).html()).toBe('check_box_outline_blank');
            console.log(angular.element(checkboxes[6]).html());
            expect(angular.element(checkboxes[6]).html()).toBe('check_box_outline_blank');
        });
    });
})();
