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

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

        beforeEach(ngModule('invoiceIn'));

        beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
            $scope = $rootScope.$new();
            $httpBackend = _$httpBackend_;
            $httpParamSerializer = _$httpParamSerializer_;
            const $element = angular.element('<vn-invoice-in-basic-data></vn-invoice-in-basic-data>');
            controller = $componentController('vnInvoiceInBasicData', {$element, $scope});
            controller.$.watcher = watcher;
            $httpBackend.expect('GET', `DmsContainers/allowedContentTypes`).respond({});
        }));

        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();
            });
        });

        describe('checkFileExists()', () => {
            it(`should return false if a file exists`, () => {
                const fileIdExists = 1;
                controller.checkFileExists(fileIdExists);

                expect(controller.editDownloadDisabled).toBe(false);
            });
        });

        describe('onEdit()', () => {
            it(`should perform a POST query to edit the dms properties`, () => {
                jest.spyOn(controller.vnApp, 'showSuccess');

                const dms = {
                    dmsId: 1,
                    reference: 'Ref1',
                    warehouseId: 1,
                    companyId: 442,
                    dmsTypeId: 20,
                    description: 'This is a description',
                    files: []
                };

                controller.dms = dms;
                const serializedParams = $httpParamSerializer(controller.dms);
                const query = `dms/${controller.dms.dmsId}/updateFile?${serializedParams}`;

                $httpBackend.expectPOST(query).respond({});
                controller.onEdit();
                $httpBackend.flush();

                expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            });
        });

        describe('onCreate()', () => {
            it(`should perform a POST query to create a new dms`, () => {
                jest.spyOn(controller.vnApp, 'showSuccess');

                const dms = {
                    reference: 'Ref1',
                    warehouseId: 1,
                    companyId: 442,
                    dmsTypeId: 20,
                    description: 'This is a description',
                    files: [{
                        lastModified: 1668673957761,
                        lastModifiedDate: new Date(),
                        name: 'file-example.png',
                        size: 19653,
                        type: 'image/png',
                        webkitRelativePath: ''
                    }]
                };

                controller.dms = dms;
                const serializedParams = $httpParamSerializer(controller.dms);
                const query = `Dms/uploadFile?${serializedParams}`;

                $httpBackend.expectPOST(query).respond({});
                controller.onCreate();
                $httpBackend.flush();

                expect(controller.vnApp.showSuccess).toHaveBeenCalled();
            });
        });
    });
});