import './index.js';

describe('Entry', () => {
    describe('Component vnEntryBuyImport', () => {
        let controller;
        let $httpParamSerializer;
        let $httpBackend;

        beforeEach(ngModule('entry'));

        beforeEach(angular.mock.inject(($componentController, $compile, $rootScope, _$httpParamSerializer_, _$httpBackend_) => {
            $httpBackend = _$httpBackend_;
            $httpParamSerializer = _$httpParamSerializer_;
            let $element = $compile('<vn-entry-buy-import-buys></vn-entry-latest-buys')($rootScope);
            controller = $componentController('vnEntryBuyImport', {$element});
            controller.entry = {
                id: 1
            };
            controller.$params = {id: 1};
        }));

        describe('fillData()', () => {
            it(`should call to the fillData() method`, () => {
                controller.fetchBuys = jest.fn();

                const rawData = `{
                    "invoices": [
                        {
                            "tx_awb": "123456",
                            "boxes": [
                                {
                                    "id_box": 1,
                                    "nu_length": 1, 
                                    "nu_width": 15,
                                    "nu_height": 80,
                                    "products": [
                                        {
                                            "nm_product": "Bow", 
                                            "nu_length": 1, 
                                            "nu_stems_bunch": 1, 
                                            "nu_bunches": 1,
                                            "mny_rate_stem": 5.77
                                        }

                                    ]
                                },
                                {
                                    "id_box": 2,
                                    "nu_length": 25, 
                                    "nu_width": 1,
                                    "nu_height": 45,
                                    "products": [
                                        {
                                            "nm_product": "Arrow", 
                                            "nu_length": 25,
                                            "nu_stems_bunch": 1, 
                                            "nu_bunches": 1,
                                            "mny_rate_stem": 2.16
                                        }
                                    ]
                                }
                            ]
                        }
                    ]}`;
                const expectedBuys = [
                    {
                        'buyingValue': 5.77,
                        'description': 'Bow',
                        'grouping': 1,
                        'packing': 1,
                        'size': 1,
                        'volume': 1200},

                    {
                        'buyingValue': 2.16,
                        'description': 'Arrow',
                        'grouping': 1,
                        'packing': 1,
                        'size': 25,
                        'volume': 1125}
                ];
                controller.fillData(rawData);
                controller.$.$apply();

                const importData = controller.import;

                expect(importData.observation).toEqual('123456');
                expect(importData.ref).toEqual('1, 2');

                expect(controller.fetchBuys).toHaveBeenCalledWith(expectedBuys);
            });
        });

        describe('fetchBuys()', () => {
            it(`should perform a query to fetch the buys data`, () => {
                const buys = [
                    {
                        'buyingValue': 5.77,
                        'description': 'Bow',
                        'grouping': 1,
                        'packing': 1,
                        'size': 1,
                        'volume': 1200},

                    {
                        'buyingValue': 2.16,
                        'description': 'Arrow',
                        'grouping': 1,
                        'packing': 1,
                        'size': 25,
                        'volume': 1125}
                ];

                const query = `Entries/1/importBuysPreview`;
                $httpBackend.expectPOST(query).respond(200, buys);
                controller.fetchBuys(buys);
                $httpBackend.flush();

                const importData = controller.import;

                expect(importData.buys.length).toEqual(2);
            });
        });

        describe('onSubmit()', () => {
            it(`should throw an error when some of the rows doesn't have an item`, () => {
                jest.spyOn(controller.vnApp, 'showError');

                controller.import = {
                    observation: '123456',
                    ref: '1, 2',
                    buys: [
                        {
                            'buyingValue': 5.77,
                            'description': 'Bow',
                            'grouping': 1,
                            'packing': 1,
                            'size': 1,
                            'volume': 1200},
                        {
                            'buyingValue': 2.16,
                            'description': 'Arrow',
                            'grouping': 1,
                            'packing': 1,
                            'size': 25,
                            'volume': 1125}
                    ]
                };

                controller.onSubmit();

                const message = `Some of the imported buys doesn't have an item`;

                expect(controller.vnApp.showError).toHaveBeenCalledWith(message);
            });

            it(`should now perform a query to update columns`, () => {
                jest.spyOn(controller.vnApp, 'showSuccess');
                controller.$state.go = jest.fn();

                controller.import = {
                    observation: '123456',
                    ref: '1, 2',
                    buys: [
                        {
                            'itemFk': 10,
                            'buyingValue': 5.77,
                            'description': 'Bow',
                            'grouping': 1,
                            'packing': 1,
                            'size': 1,
                            'volume': 1200},
                        {
                            'itemFk': 11,
                            'buyingValue': 2.16,
                            'description': 'Arrow',
                            'grouping': 1,
                            'packing': 1,
                            'size': 25,
                            'volume': 1125}
                    ]
                };
                const params = controller.import;

                const query = `Entries/1/importBuys`;
                $httpBackend.expectPOST(query, params).respond(200, params.buys);
                controller.onSubmit();
                $httpBackend.flush();

                const importData = controller.import;

                expect(importData.buys.length).toEqual(2);

                expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
                expect(controller.$state.go).toHaveBeenCalledWith('entry.card.buy.index');
            });
        });
    });
});