salix/modules/supplier/front/consumption/index.spec.js

111 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-12-16 07:05:41 +00:00
import './index.js';
import crudModel from 'core/mocks/crud-model';
describe('Supplier', () => {
describe('Component vnSupplierConsumption', () => {
let $scope;
let controller;
let $httpParamSerializer;
let $httpBackend;
2021-04-30 12:27:50 +00:00
const supplierId = 2;
2020-12-16 07:05:41 +00:00
beforeEach(ngModule('supplier'));
beforeEach(inject(($componentController, $rootScope, _$httpParamSerializer_, _$httpBackend_) => {
$scope = $rootScope.$new();
$httpParamSerializer = _$httpParamSerializer_;
$httpBackend = _$httpBackend_;
const $element = angular.element('<vn-supplier-consumption></vn-supplier-consumption');
controller = $componentController('vnSupplierConsumption', {$element, $scope});
controller.$.model = crudModel;
2021-04-30 12:27:50 +00:00
controller.$params = {id: supplierId};
2020-12-16 07:05:41 +00:00
controller.supplier = {
2021-04-30 12:27:50 +00:00
id: supplierId
2020-12-16 07:05:41 +00:00
};
}));
describe('showReport()', () => {
it('should call the window.open function', () => {
jest.spyOn(window, 'open').mockReturnThis();
2023-01-16 14:18:24 +00:00
const now = Date.vnNew();
2020-12-16 07:05:41 +00:00
controller.$.model.userParams = {
from: now,
to: now
};
controller.showReport();
const expectedParams = {
recipientId: 2,
from: now,
to: now
};
const serializedParams = $httpParamSerializer(expectedParams);
2022-10-04 05:41:40 +00:00
const path = `api/Suppliers/${supplierId}/campaign-metrics-pdf?${serializedParams}`;
2020-12-16 07:05:41 +00:00
expect(window.open).toHaveBeenCalledWith(path);
});
});
describe('sendEmail()', () => {
2021-04-30 12:27:50 +00:00
it('should throw an error', () => {
jest.spyOn(controller.vnApp, 'showError');
const expectedParams = {
filter: {
where: {
supplierFk: supplierId,
email: {neq: null}
},
limit: 1
}
};
const serializedParams = $httpParamSerializer(expectedParams);
$httpBackend.expectGET(`SupplierContacts?${serializedParams}`).respond({});
controller.sendEmail();
$httpBackend.flush();
2022-10-04 05:41:40 +00:00
expect(controller.vnApp.showError)
.toHaveBeenCalledWith(`This supplier doesn't have a contact with an email address`);
2021-04-30 12:27:50 +00:00
});
2020-12-16 07:05:41 +00:00
it('should make a GET query sending the report', () => {
2021-04-30 12:27:50 +00:00
let serializedParams;
const params = {
filter: {
where: {
supplierFk: supplierId,
email: {neq: null}
},
limit: 1
}
};
serializedParams = $httpParamSerializer(params);
$httpBackend.whenGET(`SupplierContacts?${serializedParams}`).respond([
{id: 1, email: 'batman@gothamcity.com'}
]);
2023-01-16 14:18:24 +00:00
const now = Date.vnNew();
2020-12-16 07:05:41 +00:00
controller.$.model.userParams = {
from: now,
to: now
};
const expectedParams = {
2021-04-30 12:27:50 +00:00
recipient: 'batman@gothamcity.com',
2020-12-16 07:05:41 +00:00
from: now,
to: now
};
2021-04-30 12:27:50 +00:00
serializedParams = $httpParamSerializer(expectedParams);
2022-10-04 05:41:40 +00:00
const path = `Suppliers/${supplierId}/campaign-metrics-email`;
2020-12-16 07:05:41 +00:00
2022-10-04 05:41:40 +00:00
$httpBackend.expect('POST', path).respond({});
2020-12-16 07:05:41 +00:00
controller.sendEmail();
$httpBackend.flush();
});
});
});
});