import './index';
import crudModel from 'core/mocks/crud-model';

describe('Client', () => {
    describe('Component vnClientSummary', () => {
        let controller;
        let $httpBackend;
        let $window;

        beforeEach(ngModule('client'));

        beforeEach(inject(($componentController, _$httpBackend_, _$window_) => {
            $window = _$window_;
            $httpBackend = _$httpBackend_;
            const $element = angular.element('<vn-client-summary></vn-client-summary>');
            controller = $componentController('vnClientSummary', {$element});
            controller._client = {id: 1101};
            controller.$.ticketsModel = crudModel;
        }));

        describe('client() setter', () => {
            it('should call to the loadData() and loadTickets() methods', () => {
                controller.loadData = jest.fn();
                controller.loadTickets = jest.fn();

                controller.client = {id: 1102};

                expect(controller.loadData).toHaveBeenCalledWith();
                expect(controller.loadTickets).toHaveBeenCalledWith();
            });
        });

        describe('loadData()', () => {
            it('should perform a GET query and then define the summary property', () => {
                let res = {name: 'Superman', classifications: []};

                jest.spyOn(controller, 'sumRisk').mockReturnThis();
                $httpBackend.expect('GET', `Clients/1101/summary`).respond(200, res);

                controller.loadData();
                $httpBackend.flush();

                expect(controller.summary).toBeDefined();
                expect(controller.summary.name).toEqual('Superman');
            });
        });

        describe('loadTickets()', () => {
            it('should call to the model refresh() method', () => {
                jest.spyOn(controller.$.ticketsModel, 'refresh');

                controller.loadTickets();
                controller.$.$apply();

                expect(controller.$.ticketsModel.refresh).toHaveBeenCalledWith();
            });
        });

        describe('sumRisk()', () => {
            it('should sum property amount of an array', () => {
                controller.summary = {
                    clientRisks: [{
                        companyFk: 442,
                        amount: 100
                    },
                    {
                        companyFk: 567,
                        amount: 200
                    }]};

                let result = controller.sumRisk();

                expect(result).toEqual(300);
            });
        });

        describe('chipColor()', () => {
            it('should return warning when the date is the present', () => {
                let today = new Date();
                let result = controller.chipColor(today);

                expect(result).toEqual('warning');
            });

            it('should return success when the date is in the future', () => {
                let futureDate = new Date();
                futureDate = futureDate.setDate(futureDate.getDate() + 10);
                let result = controller.chipColor(futureDate);

                expect(result).toEqual('success');
            });

            it('should return undefined when the date is in the past', () => {
                let pastDate = new Date();
                pastDate = pastDate.setDate(pastDate.getDate() - 10);
                let result = controller.chipColor(pastDate);

                expect(result).toEqual(undefined);
            });
        });

        describe('stateColor()', () => {
            it('should return "success" when the alertLevelCode property is "OK"', () => {
                const result = controller.stateColor({ticketState: {code: 'OK'}});

                expect(result).toEqual('success');
            });

            it('should return "notice" when the alertLevelCode property is "FREE"', () => {
                const result = controller.stateColor({ticketState: {code: 'FREE'}});

                expect(result).toEqual('notice');
            });

            it('should return "warning" when the alertLevel property is "1', () => {
                const result = controller.stateColor({ticketState: {code: 'PACKING', alertLevel: 1}});

                expect(result).toEqual('warning');
            });

            it('should return "alert" when the alertLevel property is "0"', () => {
                const result = controller.stateColor({ticketState: {code: 'FIXING', alertLevel: 0}});

                expect(result).toEqual('alert');
            });
        });

        describe('totalPriceColor()', () => {
            it('should return "warning" when the ticket amount is less than 50€', () => {
                const result = controller.totalPriceColor({totalWithVat: '8.50'});

                expect(result).toEqual('warning');
            });
        });

        describe('preview()', () => {
            it('should show the dialog summary', () => {
                controller.$.summary = {show: () => {}};
                jest.spyOn(controller.$.summary, 'show');

                const ticket = {id: 1, clientFk: 1101};

                const event = new MouseEvent('click', {
                    view: $window,
                    bubbles: true,
                    cancelable: true
                });
                controller.preview(event, ticket);

                expect(controller.$.summary.show).toHaveBeenCalledWith();
            });
        });
    });
});