import test from 'ava'; import sinon from 'sinon'; import { createTestMessageBroker } from './message-broker/test-message-broker'; import { initializePayments } from './payments'; test('charge success', async (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const payments = initializePayments(broker); // Send a charge request message. const promise = payments.charge({ orderId: '123', subTotal: 100, tip: 10, tax: 1, total: 111, items: [ { name: 'Product', description: 'Description', sku: 'product-sku', price: 100, quantity: 2, tax: 1, total: 201, }, ], }); // Verify the payments-charge-request message was sent. t.is(sendMessage.callCount, 1); const call = sendMessage.getCall(0); t.is(call.args[0], 'payments-charge-request'); t.is(call.args[1].orderId, '123'); t.is(call.args[1].subTotal, 100); t.is(call.args[1].tip, 10); t.is(call.args[1].tax, 1); t.is(call.args[1].total, 111); t.is(call.args[1].items.length, 1); t.is(call.args[1].items[0].name, 'Product'); t.is(call.args[1].items[0].description, 'Description'); t.is(call.args[1].items[0].sku, 'product-sku'); t.is(call.args[1].items[0].price, 100); t.is(call.args[1].items[0].quantity, 2); t.is(call.args[1].items[0].tax, 1); t.is(call.args[1].items[0].total, 201); const requestId = call.args[1].requestId; t.truthy(requestId); // Send a charge response message. broker.runListeners('payments-charge-response', { requestId, charge: { id: '123', status: 'pending' }, }); // Verify the promise was resolved with the charge response. const response = await promise; t.is(response.id, '123'); t.is(response.status, 'pending'); }); test('charge failure', async (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const payments = initializePayments(broker); // Send a charge request message. const promise = payments.charge({ subTotal: 100, tip: 10, tax: 1, total: 111, items: [], }); // Get requestId from the message. const call = sendMessage.getCall(0); const requestId = call.args[1].requestId; // Send a charge response message. broker.runListeners('payments-charge-response', { requestId, error: { message: 'Something went wrong' }, }); // Verify the promise was resolved with the charge response. const error = await t.throwsAsync(promise); t.is(error.message, 'Something went wrong'); }); test('subscribe to terminal', (t) => { const sendMessage = sinon.fake(); const broker = createTestMessageBroker(sendMessage); const payments = initializePayments(broker); const listener = sinon.fake(); const unsubscribe = payments.subscribe('terminal', listener); // Test that the `payments-subscribe` message was sent. t.is(sendMessage.callCount, 1); t.truthy(sendMessage.calledWith('payments-subscribe', 'terminal')); const anotherListener = sinon.fake(); const anotherUnsubscribe = payments.subscribe('terminal', anotherListener); // Test that multiple subscribes only send a single `'payments-subscribe` message. t.is(sendMessage.callCount, 1); // Test that the listeners were called. broker.runListeners('payments-terminal-event', { terminal: { status: 'open' }, }); t.is(listener.callCount, 1); t.is(anotherListener.callCount, 1); t.truthy(listener.calledWith({ status: 'open' })); t.truthy(anotherListener.calledWith({ status: 'open' })); broker.runListeners('payments-terminal-event', { terminal: { status: 'closed' }, }); t.is(listener.callCount, 2); t.is(anotherListener.callCount, 2); t.truthy(listener.calledWith({ status: 'closed' })); t.truthy(anotherListener.calledWith({ status: 'closed' })); broker.runListeners('payments-terminal-event', { terminal: null, }); t.is(listener.callCount, 3); t.is(anotherListener.callCount, 3); t.truthy(listener.calledWith(null)); t.truthy(anotherListener.calledWith(null)); // Test that the `payments-subscribe` message is sent when all subscribes are destroyed. unsubscribe(); anotherUnsubscribe(); t.truthy(sendMessage.calledWith('payments-unsubscribe', 'terminal')); // Test that the listeners were not called after unsubscribing. broker.runListeners('payments-unsubscribe', { terminal: { status: 'open' }, }); t.is(listener.callCount, 3); t.is(anotherListener.callCount, 3); });