Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/stripe intent duplicates #158

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 31 additions & 62 deletions src/card.test.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,75 @@
global.fetch = require('jest-fetch-mock');
global.window = {};

import api from './api';

let VAULT_RESPONSE;

describe('card', () => {
let script;

beforeEach(() => {
api.init('test', 'pk_test');
VAULT_RESPONSE = null;
// Mock document
global.document = {
createElement: (tag) => {
script = {
parentNode: {
removeChild: () => {},
},
};
setTimeout(() => {
window[`swell_vault_response_${window.__swell_vault_request_id}`]({
$data: VAULT_RESPONSE || { token: 't_test' },
});
}, 100);
return script;
},
getElementsByTagName: (tag) => {
return [{ appendChild: (el) => {} }];
},
};
});

describe('init', () => {
describe('createToken', () => {
it('should make request to POST /tokens', async () => {
fetch.mockResponse(JSON.stringify({ $data: { token: 't_test' } }));

await api.card.createToken({
number: '4242 4242 4242 4242',
exp_month: 1,
exp_year: 2099,
cvc: 123,
});

expect(script.src).toEqual(
'https://vault.schema.io/tokens?%24jsonp%5Bmethod%5D=post&%24jsonp%5Bcallback%5D=swell_vault_response_1&%24data%5Bnumber%5D=4242%204242%204242%204242&%24data%5Bexp_month%5D=1&%24data%5Bexp_year%5D=2099&%24data%5Bcvc%5D=123&%24key=pk_test',
expect(fetch).toHaveBeenCalledWith(
'https://vault.schema.io/tokens?%24jsonp%5Bmethod%5D=post&%24jsonp%5Bcallback%5D=none&%24data%5Bnumber%5D=4242%204242%204242%204242&%24data%5Bexp_month%5D=1&%24data%5Bexp_year%5D=2099&%24data%5Bcvc%5D=123&%24key=pk_test',
expect.objectContaining({
signal: expect.any(Object),
}),
);
});

it('should throw an error if card cvc code is invalid', async () => {
try {
await api.card.createToken({
await expect(
api.card.createToken({
cvc: 1,
});
throw new Error();
} catch (err) {
expect(err.message).toEqual('Card CVC code appears to be invalid');
}
}),
).rejects.toThrow('Card CVC code appears to be invalid');
});

it('should throw an error if card expiry is invalid', async () => {
try {
await api.card.createToken({
await expect(
api.card.createToken({
cvc: 123,
exp_month: 100,
});
throw new Error();
} catch (err) {
expect(err.message).toEqual('Card expiry appears to be invalid');
}
}),
).rejects.toThrow('Card expiry appears to be invalid');
});

it('should throw an error if card number is invalid', async () => {
try {
await api.card.createToken({
await expect(
api.card.createToken({
cvc: 123,
exp_month: 1,
exp_year: 2099,
number: '1',
});
throw new Error();
} catch (err) {
expect(err.message).toEqual('Card number appears to be invalid');
}
}),
).rejects.toThrow('Card number appears to be invalid');
});

it('should throw an of vault request returns errors', async () => {
try {
VAULT_RESPONSE = {
errors: { gateway: { code: 'INVALID', message: 'Test error' } },
};
await api.card.createToken({
fetch.mockResponse(
JSON.stringify({
$data: {
errors: { gateway: { code: 'INVALID', message: 'Test error' } },
},
}),
);

await expect(
api.card.createToken({
cvc: 123,
exp_month: 1,
exp_year: 2099,
number: '4242 4242 4242 4242',
});
throw new Error();
} catch (err) {
expect(err.message).toEqual('Test error');
expect(err.param).toEqual('gateway');
expect(err.status).toEqual(402);
}
}),
).rejects.toThrow('Test error');
});
});

Expand Down
9 changes: 6 additions & 3 deletions src/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ function methods(request, options) {

const { handler, resolve, reject } = this.pendingRequests.shift();

return Promise.resolve().then(handler).then(resolve, reject).finally(() => {
this.nextRequest();
});
return Promise.resolve()
.then(handler)
.then(resolve, reject)
.finally(() => {
this.nextRequest();
});
},

async requestStateSync(handler) {
Expand Down
56 changes: 34 additions & 22 deletions src/cart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,23 @@ describe('cart', () => {
it('should execute requests sequentially', async () => {
fetch
.mockResponseOnce(
() => new Promise((resolve) => {
setTimeout(() => resolve({ body: JSON.stringify({
...api.cart.state,
grand_total: 1000,
index: 1
})
}), 100);
})
() =>
new Promise((resolve) => {
setTimeout(
() =>
resolve({
body: JSON.stringify({
...api.cart.state,
grand_total: 1000,
index: 1,
}),
}),
100,
);
}),
)
.mockResponseOnce(
() => Promise.resolve({
.mockResponseOnce(() =>
Promise.resolve({
body: JSON.stringify({
...api.cart.state,
waited: true,
Expand All @@ -124,28 +130,34 @@ describe('cart', () => {
}),
)
.mockResponseOnce(
() => new Promise((resolve) => {
setTimeout(() => resolve({ body: JSON.stringify({
...api.cart.state,
index: 3
})
}), 100);
})
() =>
new Promise((resolve) => {
setTimeout(
() =>
resolve({
body: JSON.stringify({
...api.cart.state,
index: 3,
}),
}),
100,
);
}),
)
.mockResponseOnce(
() => Promise.resolve({
.mockResponseOnce(() =>
Promise.resolve({
body: JSON.stringify({
...api.cart.state,
index: 4,
}),
}),
)
.mockResponseOnce(
() => Promise.resolve({
.mockResponseOnce(() =>
Promise.resolve({
body: JSON.stringify({
...api.cart.state,
again: true,
index: 5
index: 5,
}),
}),
);
Expand Down
10 changes: 5 additions & 5 deletions src/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function methods(request, opt) {
selected() {
if (!this.code) {
this.set(
opt.getCookie('swell-currency') || opt.api.settings.get('store.currency'),
opt.getCookie('swell-currency') ||
opt.api.settings.get('store.currency'),
);
}

Expand Down Expand Up @@ -65,8 +66,7 @@ function methods(request, opt) {
const formatCode = params.code || code;
const formatRate = params.rate || rate;
const formatLocale = params.locale || this.locale;
const formatDecimals =
'decimals' in params ? params.decimals : decimals;
const formatDecimals = 'decimals' in params ? params.decimals : decimals;
const { convert = true } = params;

let formatAmount = amount;
Expand Down Expand Up @@ -167,8 +167,8 @@ function methods(request, opt) {
? 'up'
: 'down'
: diff <= -0.5
? 'down'
: 'up'
? 'down'
: 'up'
: config.round;

switch (direction) {
Expand Down
5 changes: 2 additions & 3 deletions src/payment/google/braintree.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ export default class BraintreeGooglePayment extends Payment {

async _onClick(googlePayment, paymentDataRequest) {
try {
const paymentData = await this.googleClient.loadPaymentData(
paymentDataRequest,
);
const paymentData =
await this.googleClient.loadPaymentData(paymentDataRequest);

if (paymentData) {
await this._submitPayment(googlePayment, paymentData);
Expand Down
4 changes: 2 additions & 2 deletions src/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ function findPurchaseOption(product, purchaseOption) {
typeof purchaseOption === 'string'
? purchaseOption
: plan !== undefined
? 'subscription'
: 'standard',
? 'subscription'
: 'standard',
);
let option = get(product, `purchase_options.${type}`);
if (!option && type !== 'standard') {
Expand Down
38 changes: 1 addition & 37 deletions src/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
global.fetch = require('jest-fetch-mock');
global.window = {};

import api from './api';
import { vaultRequest, toCamel, toSnake, stringifyQuery } from './utils';
import { toCamel, toSnake, stringifyQuery } from './utils';

describe('utils', () => {
let script;

beforeEach(() => {
api.init('test', 'pk_test');
// Mock document
global.document = {
createElement: (tag) => {
script = {
parentNode: {
removeChild: () => {},
},
};
setTimeout(() => {
window[`swell_vault_response_${window.__swell_vault_request_id}`]({
$data: { ok: 1 },
});
}, 100);
return script;
},
getElementsByTagName: (tag) => {
return [{ appendChild: (el) => {} }];
},
};
});

describe('init', () => {
describe('vaultRequest', () => {
it('should make a fetch request with vaultUrl', async () => {
await vaultRequest('post', '/tokens');

expect(script.src).toEqual(
'https://vault.schema.io/tokens?%24jsonp%5Bmethod%5D=post&%24jsonp%5Bcallback%5D=swell_vault_response_1&%24data=&%24key=pk_test',
);
});
});
});

describe('toCamel', () => {
Expand Down
Loading
Loading