import './index';
import watcher from 'core/mocks/watcher.js';

describe('AgencyTerm', () => {
    describe('Component vnAgencyTermCreateInvoiceIn', () => {
        let controller;
        let $scope;
        let $httpBackend;
        let $httpParamSerializer;

        beforeEach(ngModule('route'));

        beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
            $scope = $rootScope.$new();
            $httpBackend = _$httpBackend_;
            $httpParamSerializer = _$httpParamSerializer_;
            const $element = angular.element('<vn-agency-term-create-invoice-in></vn-agency-term-create-invoice-in>');
            controller = $componentController('vnAgencyTermCreateInvoiceIn', {$element});
            controller._route = {
                id: 1
            };
        }));

        describe('$onChanges()', () => {
            it('should update the params data when $params.q is defined', () => {
                controller.$params = {q: '{"supplierName": "Plants SL","rows": null}'};

                const params = {q: '{"supplierName": "Plants SL", "rows": null}'};
                const json = JSON.parse(params.q);

                controller.$onChanges();

                expect(controller.params).toEqual(json);
            });
        });

        describe('route() setter', () => {
            it('should set the ticket data and then call setDefaultParams() and getAllowedContentTypes()', () => {
                jest.spyOn(controller, 'setDefaultParams');
                jest.spyOn(controller, 'getAllowedContentTypes');
                controller.route = {
                    id: 1
                };

                expect(controller.route).toBeDefined();
                expect(controller.setDefaultParams).toHaveBeenCalledWith();
                expect(controller.getAllowedContentTypes).toHaveBeenCalledWith();
            });
        });

        describe('getAllowedContentTypes()', () => {
            it('should make an HTTP GET request to get the allowed content types', () => {
                const expectedResponse = ['image/png', 'image/jpg'];
                $httpBackend.expect('GET', `DmsContainers/allowedContentTypes`).respond(expectedResponse);
                controller.getAllowedContentTypes();
                $httpBackend.flush();

                expect(controller.allowedContentTypes).toBeDefined();
                expect(controller.allowedContentTypes).toEqual('image/png, image/jpg');
            });
        });

        describe('setDefaultParams()', () => {
            it('should perform a GET query and define the dms property on controller', () => {
                const params = {filter: {
                    where: {code: 'invoiceIn'}
                }};
                const serializedParams = $httpParamSerializer(params);
                $httpBackend.expect('GET', `DmsTypes/findOne?${serializedParams}`).respond({id: 1, code: 'invoiceIn'});
                controller.params = {supplierName: 'Plants SL'};
                controller.setDefaultParams();
                $httpBackend.flush();

                expect(controller.dms).toBeDefined();
                expect(controller.dms.dmsTypeId).toEqual(1);
            });
        });

        describe('onSubmit()', () => {
            it('should make an HTTP POST request to save the form data', () => {
                controller.$.watcher = watcher;

                jest.spyOn(controller.$.watcher, 'updateOriginalData');
                const files = [{id: 1, name: 'MyFile'}];
                controller.dms = {files};
                const serializedParams = $httpParamSerializer(controller.dms);
                const query = `dms/uploadFile?${serializedParams}`;
                controller.params = {rows: null};

                $httpBackend.expect('POST', query).respond({});
                $httpBackend.expect('POST', 'AgencyTerms/createInvoiceIn').respond({});
                controller.onSubmit();
                $httpBackend.flush();
            });
        });

        describe('onFileChange()', () => {
            it('should set dms hasFileAttached property to true if has any files', () => {
                const files = [{id: 1, name: 'MyFile'}];
                controller.onFileChange(files);
                $scope.$apply();

                expect(controller.dms.hasFileAttached).toBeTruthy();
            });
        });
    });
});