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

describe('Client notification', () => {
    describe('Component vnClientNotification', () => {
        let controller;
        let $httpBackend;

        beforeEach(ngModule('client'));

        beforeEach(inject(($componentController, _$httpBackend_) => {
            $httpBackend = _$httpBackend_;
            const $element = angular.element('<vn-client-notification></vn-client-notification>');
            controller = $componentController('vnClientNotification', {$element});
            controller.$.model = crudModel;
            controller.$.model.data = [
                {id: 1101},
                {id: 1102},
                {id: 1103}
            ];
            $httpBackend.expect('GET', `Campaigns/upcoming`).respond(200, {id: 1});
        }));

        describe('checked() getter', () => {
            it('should return the checked lines', () => {
                const data = controller.$.model.data;
                data[1].$checked = true;
                data[2].$checked = true;

                const checkedRows = controller.checked;

                const firstCheckedRow = checkedRows[0];
                const secondCheckedRow = checkedRows[1];

                expect(firstCheckedRow.id).toEqual(1102);
                expect(secondCheckedRow.id).toEqual(1103);
            });
        });

        describe('campaignSelection() setter', () => {
            it('should set the campaign from and to properties', () => {
                const dated = Date.vnNew();
                controller.campaignSelection = {
                    dated: dated,
                    scopeDays: 14
                };

                const expectedDateTo = new Date(dated);
                expectedDateTo.setDate(expectedDateTo.getDate() - 14);

                const campaign = controller.campaign;

                expect(campaign.from).toEqual(expectedDateTo);
                expect(campaign.to).toEqual(dated);
            });
        });

        describe('onSendClientConsumption()', () => {
            it('should return saved message', () => {
                jest.spyOn(controller.vnApp, 'showSuccess');

                controller.$.filters = {hide: () => {}};
                controller.campaign = {
                    from: Date.vnNew(),
                    to: Date.vnNew()
                };

                const data = controller.$.model.data;
                data[0].$checked = true;
                data[1].$checked = true;

                const args = Object.assign({
                    clients: [1101, 1102]
                }, controller.campaign);
                const params = JSON.stringify(args);

                $httpBackend.expect('POST', `ClientConsumptionQueues`, {params}).respond(200, params);
                controller.onSendClientConsumption();
                $httpBackend.flush();

                expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Notification sent!');
            });
        });

        describe('exprBuilder()', () => {
            it('should search by sales person', () => {
                let expr = controller.exprBuilder('salesPersonFk', '5');

                expect(expr).toEqual({'salesPersonFk': '5'});
            });

            it('should search by client social name', () => {
                let expr = controller.exprBuilder('socialName', '1foo');

                expect(expr).toEqual({'socialName': {like: '%1foo%'}});
            });
        });
    });
});