compose-payload.js 2.29 KB
/* eslint-disable no-console */
/* global describe it */

const should = require('should');
const xmlParser = require('fast-xml-parser');

const composePayload = require('../../lib/generic-xmlrpc/compose-payload');

describe('#generic-xmlrpc', () => {
    describe('#compose-payload', () => {
        it('should compose payload correctly (params is an array)', () => {
            const resultStr = composePayload('topUpRequest', [
                {
                    name: 'MSISDN',
                    value: '62818181818',
                },
                {
                    name: 'REQUESTID',
                    value: '1234',
                },
            ]);

            // console.log(result);
            should.exists(resultStr, '5033DD9C');

            const resultObj = xmlParser.parse(resultStr);

            // console.log(JSON.stringify(resultObj, null, 2));

            resultObj.methodCall.params.param.value.struct.member.find((item) => item.name === 'MSISDN').value.string.should.equal(62818181818, '06C60D8C');
        });

        it('should compose payload correctly (params is an object)', () => {
            const resultStr = composePayload('topUpRequest', {
                MSISDN: { value: '62818181818' },
                REQUESTID: { value: '1234' },
            });

            // console.log(result);
            should.exists(resultStr, '13E0279E');

            const resultObj = xmlParser.parse(resultStr);

            // console.log(JSON.stringify(resultObj, null, 2));

            resultObj.methodCall.params.param.value.struct.member.find((item) => item.name === 'MSISDN').value.string.should.equal(62818181818, '3399A280');
        });

        it('should compose payload correctly (params is an object, and value is not an object)', () => {
            const resultStr = composePayload('topUpRequest', {
                MSISDN: '62818181818',
                REQUESTID: '1234',
            });

            // console.log(resultStr);
            should.exists(resultStr, '99413C25');

            const resultObj = xmlParser.parse(resultStr);

            // console.log(JSON.stringify(resultObj, null, 2));

            resultObj.methodCall.params.param.value.struct.member.find((item) => item.name === 'MSISDN').value.string.should.equal(62818181818, 'C49C657D');
        });
    });
});