Skip to content

Commit

Permalink
added tests for retrieveOrder, removed unused parameters, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
schaechinger committed Mar 25, 2018
1 parent e1922be commit 47e2b02
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 38 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ internetmarke.checkout()
});
```

#### Retrieve older orders

Every order can be re-downloaded with their order id.

```javascript
internetmarke.retrieveOrder({ orderId: 1234 })
.then(shoppingcart => {
// same structure as checkout
});
```


### Add addresses to a voucher

Expand Down
8 changes: 2 additions & 6 deletions lib/Internetmarke.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ class Internetmarke {

return this._1C4AService.checkout({
order,
user: this[_USER],
outputFormat: outputFormat || this._config.outputFormat,
});
}
Expand All @@ -115,12 +114,9 @@ class Internetmarke {
* archive, the order id and the voucher ids.
*/
retrieveOrder({ orderId = null } = {}) {
let order = { shopOrderId: orderId };
const order = { shopOrderId: orderId };

return this._1C4AService.retrieveOrder({
order,
user: this[_USER],
});
return this._1C4AService.retrieveOrder({ order });
}

/**
Expand Down
57 changes: 26 additions & 31 deletions lib/Service/Soap/OneClickForApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,7 @@ class OneClickForAppService extends SoapService {
.then(response => {
this._user.setBalance(response.walletBallance || response.walletBalance);

const result = {
orderId: response.shoppingCart.shopOrderId,
link: response.link,
vouchers: []
};

response.shoppingCart.voucherList.voucher.forEach(voucher => {
const data = { id: voucher.voucherId };
if (voucher.trackId) {
data.trackingCode = voucher.trackId;
}
result.vouchers.push(data);
});

return result;
return this._processShoppingCart(response);
});
}

Expand All @@ -135,22 +121,7 @@ class OneClickForAppService extends SoapService {
}, order);
return client.retrieveOrderAsync(order);
})
.then(response => {
const result = {
orderId: response.shoppingCart.shopOrderId,
link: response.link,
vouchers: []
};

response.shoppingCart.voucherList.voucher.forEach(voucher => {
const data = { id: voucher.voucherId };
if (voucher.trackId) {
data.trackingCode = voucher.trackId;
}
result.vouchers.push(data);
});
return result;
});
.then(this._processShoppingCart);
}

/**
Expand All @@ -172,6 +143,30 @@ class OneClickForAppService extends SoapService {
});
}

/**
* Processes the data from the soap checkout method.
*
* @param {Object} response - The raw response from the checkout or
* retrieveOrder method of the soap service.
* @returns {Object}
*/
_processShoppingCart(response) {
const result = {
orderId: response.shoppingCart.shopOrderId,
link: response.link,
vouchers: []
};

response.shoppingCart.voucherList.voucher.forEach(voucher => {
const data = { id: voucher.voucherId };
if (voucher.trackId) {
data.trackingCode = voucher.trackId;
}
result.vouchers.push(data);
});
return result;
}

/**
* Initialize the soap client with the partner information headers.
*
Expand Down
41 changes: 40 additions & 1 deletion test/Service/Soap/OneClickForApp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,46 @@ describe('1C4A Service', () => {
.then(result => {
const response = CLIENT_STUB.response.checkoutShoppingCartPNGAsync;
USER_STUB.user._balance.should.equal(response.walletBallance);
result.should.have.properties([ 'orderId', 'link', 'vouchers' ]);

done();
});
});

it('should process shopping cart data', () => {
const result = service._processShoppingCart(CLIENT_STUB.response.checkoutShoppingCartPNGAsync);

result.should.have.properties(['orderId', 'link', 'vouchers']);

result.vouchers.forEach(voucher => {
voucher.should.have.property('id');
});
});
});

describe('retrieveOrder', () => {
const params = {
order: { ORDER_FAKE_STUB: true }
};

it('should call the retrieve order method', done => {
service.retrieveOrder(params)
.then(result => {
CLIENT_STUB.client.retrieveOrderAsync.calledOnce.should.be.true();
CLIENT_STUB.client.retrieveOrderAsync.args[0][0]
.should.containDeep(params.order);

done();
});
});

it('should process the data', done => {
const processMethod = service._processShoppingCart;
service._processShoppingCart = sinon.stub().returns({});

service.retrieveOrder(params)
.then(result => {
service._processShoppingCart.calledOnce.should.be.true();
service._processShoppingCart = processMethod;

done();
});
Expand Down
20 changes: 20 additions & 0 deletions test/stub/soapClient/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ const response = {
},
retrievePreviewVoucherPNGAsync: {
link: 'https://schaechinger.com'
},
retrieveOrderAsync: {
link: 'https://schaechinger.com',
shoppingCart: {
shopOrderId: 1234,
voucherList: {
voucher: [
{
voucherId: 'A1235'
},
{
voucherId: 'B1235',
trackId: 'PQ-1234567890-DE'
}
]
}
}
}
};

Expand All @@ -41,6 +58,9 @@ const client = {
),
retrievePreviewVoucherPNGAsync: sinon.stub().returns(
Promise.resolve(response.retrievePreviewVoucherPNGAsync)
),
retrieveOrderAsync: sinon.stub().returns(
Promise.resolve(response.retrieveOrderAsync)
)
};

Expand Down

0 comments on commit 47e2b02

Please sign in to comment.