Skip to content

Commit

Permalink
feat(client): PAYMENTS-1986 Expose store request methods
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldelcore committed Nov 13, 2017
1 parent 09d1eeb commit 2cb4e8b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
42 changes: 39 additions & 3 deletions src/client/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import objectAssign from 'object-assign';
import OffsitePaymentInitializer from '../payment/offsite-payment-initializer';
import PaymentSubmitter from '../payment/payment-submitter';
import StoreRequestSender from '../store/store-request-sender';
import DEFAULT_CONFIG from './default-config';

export default class Client {
Expand All @@ -10,19 +11,32 @@ export default class Client {
*/
static create(config) {
const clientConfig = objectAssign({}, DEFAULT_CONFIG, config);
const paymentSubmitter = PaymentSubmitter.create(clientConfig);
const offsitePaymentInitializer = OffsitePaymentInitializer.create(clientConfig);
const paymentSubmitter = PaymentSubmitter.create(clientConfig);
const storeRequestSender = StoreRequestSender.create(clientConfig);

return new Client(clientConfig, paymentSubmitter, offsitePaymentInitializer);
return new Client(
clientConfig,
paymentSubmitter,
offsitePaymentInitializer,
storeRequestSender
);
}

/**
* @param {Object} config
* @param {PaymentSubmitter} paymentSubmitter
* @param {OffsitePaymentInitializer} offsitePaymentInitializer
* @param {ClientTokenGenerator} clientTokenGenerator
* @param {StoreRequestSender} storeRequestSender
*/
constructor(config, paymentSubmitter, offsitePaymentInitializer, clientTokenGenerator) {
constructor(
config,
paymentSubmitter,
offsitePaymentInitializer,
clientTokenGenerator,
storeRequestSender
) {
/**
* @private
* @type {Object}
Expand All @@ -46,6 +60,12 @@ export default class Client {
* @type {ClientTokenGenerator}
*/
this.clientTokenGenerator = clientTokenGenerator;

/**
* @private
* @type {StoreRequestSender}
*/
this.storeRequestSender = storeRequestSender;
}

/**
Expand Down Expand Up @@ -74,4 +94,20 @@ export default class Client {
generateClientToken(data, callback) {
this.clientTokenGenerator.generateClientToken(data, callback);
}

getShopperToken(data, callback) {
this.storeRequestSender.getShopperToken(data, callback);
}

getShopperInstruments(data, callback) {
this.storeRequestSender.getShopperInstruments(data, callback);
}

postShopperInstrument(data, callback) {
this.storeRequestSender.postShopperInstrument(data, callback);
}

deleteShopperInstrument(data, callback) {
this.storeRequestSender.deleteShopperInstrument(data, callback);
}
}
55 changes: 53 additions & 2 deletions test/client/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import merge from 'lodash/merge';
import { HOSTED } from '../../src/payment/payment-types';
import Client from '../../src/client/client';
import paymentRequestDataMock from '../mocks/payment-request-data';
import storeIntrumentDataMock from '../mocks/store-instrument-data';

describe('Client', () => {
let client;
let clientTokenGenerator;
let config;
let paymentSubmitter;
let offsitePaymentInitializer;
let paymentSubmitter;
let storeRequestSender;

beforeEach(() => {
config = { host: 'https://bigpay.dev' };
Expand All @@ -25,7 +27,20 @@ describe('Client', () => {
generateClientToken: jasmine.createSpy('generateClientToken'),
};

client = new Client(config, paymentSubmitter, offsitePaymentInitializer, clientTokenGenerator);
storeRequestSender = {
getShopperToken: jasmine.createSpy('getShopperToken'),
getShopperInstruments: jasmine.createSpy('getShopperInstruments'),
postShopperInstrument: jasmine.createSpy('postShopperInstrument'),
deleteShopperInstrument: jasmine.createSpy('deleteShopperInstrument'),
};

client = new Client(
config,
paymentSubmitter,
offsitePaymentInitializer,
clientTokenGenerator,
storeRequestSender
);
});

it('returns an instance of Client', () => {
Expand Down Expand Up @@ -64,4 +79,40 @@ describe('Client', () => {

expect(clientTokenGenerator.generateClientToken).toHaveBeenCalledWith(data, callback);
});

it('requests a shopper token', () => {
const callback = () => {};
const data = storeIntrumentDataMock;

client.getShopperToken(data, callback);

expect(storeRequestSender.getShopperToken).toHaveBeenCalledWith(data, callback);
});

it('requests a shopper token', () => {
const callback = () => {};
const data = storeIntrumentDataMock;

client.getShopperInstruments(data, callback);

expect(storeRequestSender.getShopperInstruments).toHaveBeenCalledWith(data, callback);
});

it('generates a client token', () => {
const callback = () => {};
const data = storeIntrumentDataMock;

client.postShopperInstrument(data, callback);

expect(storeRequestSender.postShopperInstrument).toHaveBeenCalledWith(data, callback);
});

it('generates a client token', () => {
const callback = () => {};
const data = storeIntrumentDataMock;

client.deleteShopperInstrument(data, callback);

expect(storeRequestSender.deleteShopperInstrument).toHaveBeenCalledWith(data, callback);
});
});

0 comments on commit 2cb4e8b

Please sign in to comment.