Skip to content

Commit

Permalink
fix: remove CvvElement, impl other review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cbarton committed Oct 3, 2024
1 parent a0c681a commit 2c337a7
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lib/recurly.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export class Recurly extends Emitter {
deepAssign(this.config.fields, options.fields);
}

if (typeof options.required === 'object') {
if (Array.isArray(options.required)) {
this.config.required = uniq([...this.config.required, ...options.required]);
}

Expand Down
1 change: 1 addition & 0 deletions lib/recurly/element/card-cvv.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export function factory (options) {
export class CardCvvElement extends Element {
static type = 'cvv';
static elementClassName = 'CardCvvElement';
static supportsTokenization = true;
}
11 changes: 0 additions & 11 deletions lib/recurly/element/cvv.js

This file was deleted.

4 changes: 1 addition & 3 deletions lib/recurly/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { factory as cardNumberElementFactory, CardNumberElement } from './elemen
import { factory as cardMonthElementFactory, CardMonthElement } from './element/card-month';
import { factory as cardYearElementFactory, CardYearElement } from './element/card-year';
import { factory as cardCvvElementFactory, CardCvvElement } from './element/card-cvv';
import { factory as cvvElementFactory, CvvElement } from './element/cvv';
import uid from '../util/uid';

const debug = require('debug')('recurly:elements');
Expand All @@ -30,11 +29,10 @@ export default class Elements extends Emitter {
CardMonthElement = cardMonthElementFactory;
CardYearElement = cardYearElementFactory;
CardCvvElement = cardCvvElementFactory;
CvvElement = cvvElementFactory;

static VALID_SETS = [
[ CardElement ],
[ CvvElement ],
[ CardCvvElement ],
[ CardNumberElement, CardMonthElement, CardYearElement, CardCvvElement ],
];

Expand Down
2 changes: 1 addition & 1 deletion lib/recurly/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function token (customerData, bus, done) {
.then(() => this.request.post({ route: '/token', data: inputs, done: complete }))
.done();
} else {
this.request.post({ route: '/token', data: inputs, done: complete.bind(this) });
this.request.post({ route: '/token', data: inputs, done: complete });
}
}

Expand Down
14 changes: 8 additions & 6 deletions packages/public-api-fixture-server/fixtures/field.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
}
// Stub broker behavior
var tokenizeElementType = config().recurly.fields.tokenizeElement || 'number';
if (config().type === tokenizeElementType) {
window.addEventListener('message', receivePostMessage, false);
function setStubTokenizationElementName (name) {
window.stubTokenizationElementName = name;
if (config().type === name) {
window.addEventListener('message', receivePostMessage, false);
}
}
setStubTokenizationElementName('number');
sendMessage(prefix + ':ready', { type: config().type });
Expand All @@ -39,9 +42,8 @@
// Event handlers
function onToken (body) {
const recurlyConfig = getRecurlyConfig();
var recurly = new parent.recurly.Recurly(recurlyConfig);
if (recurlyConfig.fields.tokenizeElement === 'cvv') {
var recurly = new parent.recurly.Recurly(getRecurlyConfig());
if (stubTokenizationElementName === 'cvv') {
recurly.config.required = ['cvv'];
}
var inputs = body.inputs;
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ maybeDescribe('Display', () => {

describe('distinct elements', async function () {
it('matches distinct elements baseline', async function () {
const { CardElement, CvvElement, ...distinctElements } = ELEMENT_TYPES;
const { CardElement, ...distinctElements } = ELEMENT_TYPES;
for (const element in distinctElements) {
await createElement(element, { style: { fontFamily: 'Pacifico' } });
}
Expand All @@ -44,9 +44,9 @@ maybeDescribe('Display', () => {
});
});

describe('CvvElement', async function () {
it('matches CvvElement baseline', async function () {
await createElement(ELEMENT_TYPES.CvvElement, { style: { fontFamily: 'Pacifico' } });
describe('distinct CardCvvElement', async function () {
it('matches CardCvvElement baseline', async function () {
await createElement(ELEMENT_TYPES.CardCvvElement, { style: { fontFamily: 'Pacifico' } });
await fillCvvElement();
await clickFirstName();

Expand Down
5 changes: 2 additions & 3 deletions test/e2e/support/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const ELEMENT_TYPES = {
CardMonthElement: 'CardMonthElement',
CardYearElement: 'CardYearElement',
CardCvvElement: 'CardCvvElement',
CvvElement: 'CvvElement',
};

const FIELD_TYPES = {
Expand Down Expand Up @@ -138,9 +137,9 @@ function elementAndFieldSuite ({
maybeRun(distinctCardElements);
});

describe('CvvElement', function () {
describe('distinct CardCvvElement', function () {
beforeEach(async function () {
await createElement(ELEMENT_TYPES.CvvElement);
await createElement(ELEMENT_TYPES.CardCvvElement);
});
maybeRun(cvvElement);
});
Expand Down
3 changes: 0 additions & 3 deletions test/types/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ export default function elements () {
}
};

const cvvElement = elements.CvvElement(elementOptions);

const el = document.querySelector('div');
if (el) {
cardElement.attach(el).configure({}).focus().remove();
cvvElement.attach(el).configure({}).focus().remove();
}

[
Expand Down
3 changes: 0 additions & 3 deletions test/unit/elements.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('Elements', function () {
'CardMonthElement',
'CardYearElement',
'CardCvvElement',
'CvvElement',
].forEach(elementName => {
const elements = new Elements({ recurly: this.recurly });
const element = elements[elementName]();
Expand Down Expand Up @@ -90,8 +89,6 @@ describe('Elements', function () {
const invalidSets = [
['CardElement', 'CardNumberElement'],
['CardElement', 'CardCvvElement'],
['CardElement', 'CvvElement'],
['CardNumberElement', 'CvvElement'],
['CardNumberElement', 'CardElement'],
['CardNumberElement', 'CardMonthElement', 'CardYearElement', 'CardCvvElement', 'CardElement']
];
Expand Down
48 changes: 31 additions & 17 deletions test/unit/token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Recurly.token', function () {
last_name: 'bar'
};

const ELEMENTS_MAP = {
const elementsMap = {
card: 'CardElement',
number: 'CardNumberElement',
month: 'CardMonthElement',
Expand Down Expand Up @@ -128,22 +128,27 @@ describe('Recurly.token', function () {
});

describe('Cvv standalone', function () {
this.ctx.fixture = () => `
<form action="#" id="test-form">
<div data-recurly="cvv"></div>
<input type="hidden" data-recurly="token" name="recurly-token">
</form>
`;

applyFixtures();
buildRecurly({ fields: { tokenizeElement: 'cvv' } });
buildRecurly();

describe('when called with a plain object', function () {
cvvSuite(plainObjectBuilder);
});
describe('when using a HostedField', function () {
this.ctx.fixture = () => `
<form action="#" id="test-form">
<div data-recurly="cvv"></div>
<input type="hidden" data-recurly="token" name="recurly-token">
</form>
`;
beforeEach(function () {
this.recurly.hostedFields.fields[0].iframe.contentWindow.setStubTokenizationElementName('cvv');
});

describe('when called with an HTMLFormElement', function () {
cvvSuite(formBuilder);
describe('when called with a plain object', function () {
cvvSuite(plainObjectBuilder);
});

describe('when called with an HTMLFormElement', function () {
cvvSuite(formBuilder);
});
});

describe('when called with an Elements instance', function () {
Expand All @@ -154,10 +159,19 @@ describe('Recurly.token', function () {
</form>
`;

cvvSuite(elementsBuilder, { elementsMap: { cvv: 'CvvElement' } });
function setupStub (builder) {
return function (...args) {
return builder.call(this, ...args).then(res => {
this.elements.elements[0].iframe.contentWindow.setStubTokenizationElementName('cvv');
return res;
});
};
}

cvvSuite(setupStub(elementsBuilder));

describe('when called with an HTMLFormElement', function () {
cvvSuite(elementsFormOnlyBuilder, { elementsMap: { cvv: 'CvvElement' } });
cvvSuite(setupStub(elementsFormOnlyBuilder));
});
});
});
Expand Down Expand Up @@ -214,7 +228,7 @@ describe('Recurly.token', function () {
/**
* For each example, updates corresponding input field or Element
*/
function elementsBuilder ({ elementsMap = ELEMENTS_MAP, ...example }) {
function elementsBuilder (example) {
const form = window.document.querySelector('#test-form');
const container = form.querySelector('#recurly-elements');
const elements = this.elements = this.recurly.Elements();
Expand Down
2 changes: 0 additions & 2 deletions types/lib/elements.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ export interface ElementsInstance extends Emitter<ElementsInstanceEvents> {
CardMonthElement: (cardMonthElementOptions?: IndividualElementOptions) => IndividualElement;
CardYearElement: (cardYearElementOptions?: IndividualElementOptions) => IndividualElement;
CardCvvElement: (cardCvvElementOptions?: IndividualElementOptions) => IndividualElement;

CvvElement: (cvvElementOptions?: IndividualElementOptions) => IndividualElement;
}

export type Elements = () => ElementsInstance;

0 comments on commit 2c337a7

Please sign in to comment.