salix/modules/entry/front/buy/import/index.spec.js

129 lines
5.3 KiB
JavaScript
Raw Normal View History

2021-03-09 09:01:40 +00:00
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
};
}));
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 serializedParams = $httpParamSerializer({buys});
2021-03-10 09:53:22 +00:00
const query = `Entries/1/importBuysPreview?${serializedParams}`;
2021-03-09 09:01:40 +00:00
$httpBackend.expectGET(query).respond(200, buys);
controller.fetchBuys(buys);
$httpBackend.flush();
const importData = controller.import;
expect(importData.buys.length).toEqual(2);
});
});
describe('onSubmit()', () => {
it(`should perform a query to update columns`, () => {
jest.spyOn(controller.vnApp, 'showSuccess');
2021-03-09 12:39:22 +00:00
controller.$state.go = jest.fn();
2021-03-09 09:01:40 +00:00
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}
]
};
const params = controller.import;
2021-03-10 09:53:22 +00:00
const query = `Entries/1/importBuys`;
2021-03-09 09:01:40 +00:00
$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!');
2021-03-09 12:39:22 +00:00
expect(controller.$state.go).toHaveBeenCalledWith('entry.card.buy.index');
2021-03-09 09:01:40 +00:00
});
});
});
});