salix/modules/invoiceIn/back/methods/invoice-in/specs/exchangeRateUpdate.spec.js

53 lines
1.7 KiB
JavaScript

describe('exchangeRateUpdate functionality', function() {
const axios = require('axios');
const models = require('vn-loopback/server/server').models;
beforeEach(function() {
spyOn(axios, 'get').and.returnValue(Promise.resolve({
data: `<Cube>
<Cube time='2024-04-12'>
<Cube currency='USD' rate='1.1'/>
<Cube currency='CNY' rate='1.2'/>
</Cube>
</Cube>`
}));
});
it('should process XML data and update or create rates in the database', async function() {
spyOn(models.ReferenceRate, 'findOne').and.returnValue(Promise.resolve(null));
spyOn(models.ReferenceRate, 'create').and.returnValue(Promise.resolve());
await models.InvoiceIn.exchangeRateUpdate();
expect(models.ReferenceRate.create).toHaveBeenCalledTimes(2);
});
it('should not create or update rates when no XML data is available', async function() {
axios.get.and.returnValue(Promise.resolve({}));
spyOn(models.ReferenceRate, 'create');
let thrownError = null;
try {
await models.InvoiceIn.exchangeRateUpdate();
} catch (error) {
thrownError = error;
}
expect(thrownError.message).toBe('No cubes found. Exiting the method.');
});
it('should handle errors gracefully', async function() {
axios.get.and.returnValue(Promise.reject(new Error('Network error')));
let error;
try {
await models.InvoiceIn.exchangeRateUpdate();
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.message).toBe('Network error');
});
});