I have code that imports library easysoap to get a request from WSDL. And I'm trying to refactor my unit test.
I've written the unit test with jest and mock the return value but it's called the true endpoint
my function requestConverter.helper.ts
import * as EasySoap from 'easysoap';
export async function requestConverter(params, method, body) {
const opts = {
secure: false
};
const easySoap = await EasySoap(params, opts);
try {
const result = easySoap.call({
method,
params: body
});
return result[`data`];
} catch (err) {
return err;
}
}
my test file
import { requestConverter } from '../helpers/requestConverter.helper';
import * as EasySoap from 'easysoap';
describe('covert http request to wsdl request', () => {
let params: object;
let method: string;
let body: object;
let expectedResponse: object;
let opts: object;
beforeEach(() => {
params = {
host: '223.27.147.38',
path: '/~kabgianyar/ppobwebservice/develop/webservice-pdam/CustomerBillingPelInfo3Tagihan.php',
wsdl: '/~kabgianyar/ppobwebservice/develop/webservice-pdam/CustomerBillingPelInfo3Tagihan.php?wsdl'
};
method = 'billinfo';
body = {
namapengguna: 'value',
katasandi: 'abs'
};
expectedResponse = {
billinfoResponse: {
'xmlns:ns1': 'http://schemas.xmlsoap.org/soap/envelope/',
"return": 'mocked'
}
};
opts = {
secure: false
};
});
afterEach(() => jest.restoreAllMocks());
it('should return response from wsdl', async () => {
jest.spyOn(EasySoap, 'call').mockImplementation(jest.fn().mockReturnValue(expectedResponse));
const result = await requestConverter(params, method, body);
expect(EasySoap.call).toBeCalled();
console.log(JSON.stringify(result), '====');
});
});
I want the response to be
expectedResponse = {
billinfoResponse: {
'xmlns:ns1': 'http://schemas.xmlsoap.org/soap/envelope/',
"return": 'mocked'
}
};