diff --git a/__tests__/getContractStartDate.spec.js b/__tests__/getContractStartDate.spec.js new file mode 100644 index 0000000000000000000000000000000000000000..61fde69b12d74a3256fc7d832e82f223017935b9 --- /dev/null +++ b/__tests__/getContractStartDate.spec.js @@ -0,0 +1,50 @@ +const { errors } = require('cozy-konnector-libs') +const index = require('../src') + +const mockSoapRequest = jest.fn() +jest.mock('easy-soap-request', () => async () => mockSoapRequest()) + +const responseMock = { + response: { + body: 'mockedBody', + }, +} + +/** Theses tests are not working + * the function getContractStartDate needs to be in a seperate file + * OR the konnector.start needs to be mocked ? + */ +describe('getContractStartDate', () => { + it('should return void when successfully got contract start date ✅', async () => { + mockSoapRequest.mockResolvedValue(responseMock) + expect.assertions(1) + try { + await index.getContractStartDate() + expect(true).toBeTruthy() + } catch (error) { + expect(true).toBe(false) + } + }) + + it('should throw VENDOR_DOWN when failing request 🚫', async () => { + mockSoapRequest.mockRejectedValueOnce('error') + + try { + await index.getContractStartDate() + expect(true).toBe(false) + } catch (error) { + expect(error).toBe(errors.VENDOR_DOWN) + } + }) + + it('should throw NOT_EXISTING_DIRECTORY when failing parsing 🚫', async () => { + //mock processStartDate + // processStartDate.mockRejectedValueOnce('error') + try { + await index.getContractStartDate() + expect(true).toBe(false) + } catch (error) { + expect(error).toBe(errors.NOT_EXISTING_DIRECTORY) + } + }) +}) diff --git a/src/index.js b/src/index.js index c41e253945bd7bca0c790fe64aff014ef1b46784..69887a4a806470d2d2982767261e6a756cf49210 100644 --- a/src/index.js +++ b/src/index.js @@ -49,6 +49,7 @@ const endDate = moment() const endDateString = endDate.format('YYYY-MM-DD') module.exports = new BaseKonnector(start) +module.exports = { getContractStartDate } // The start function is run by the BaseKonnector instance only when it got all the account // information (fields). When you run this connector yourself in "standalone" mode or "dev" mode, @@ -172,9 +173,8 @@ async function getContractStartDate(url, apiAuthKey, userLogin, pointId) { headers: sgeHeaders, xml: consulterDonneesTechniquesContractuelles(pointId, userLogin), }).catch(err => { - log('error', 'technicalDataResponse') - log('error', err) - return err + log('error', 'Error while fetching contract start date : ' + err) + throw errors.VENDOR_DOWN }) xml2js.parseString(