зеркало из
1
0
Форкнуть 0
braintree-encryption/spec/braintree_form_spec.js

140 строки
5.5 KiB
JavaScript
Исходник Обычный вид История

2013-04-25 23:08:54 +04:00
describe("Braintree#form", function() {
afterEach(function() {
window.jQuery = $;
});
2013-06-27 23:42:40 +04:00
function setFormFixture() {
2013-04-25 23:08:54 +04:00
setFixtures("<form action='' id='braintree_form'>" +
"<input type='text' data-encrypted-name='credit-card-number' value='cc number'/>" +
"<input type='text' data-encrypted-name='credit-card-cvv' value='cvv' />" +
"<input type='text' name='card-holder-first-name' value='bob' />" +
"<input name='expiration-month' value ='May'/>" +
2014-02-05 00:01:01 +04:00
"<svg>" +
"<circle cx='50' cy='50' r='40' stroke='black' fill='red' />" +
"</svg>" +
2013-06-05 18:49:18 +04:00
"<!-- example comment -->"+
2013-05-24 06:30:29 +04:00
"<select data-encrypted-name='expiration-year'><option value='2013'>2013</option></select>" +
2013-04-25 23:08:54 +04:00
"<div id ='foo'>" +
2013-06-05 18:49:18 +04:00
" <!-- example comment -->"+
2013-04-25 23:08:54 +04:00
" <input data-encrypted-name ='credit-card-expiration-date' class='encrypted' value ='May'/>" +
"</div>" +
"<input type=\"submit\" id=\"click_me\" />" +
"</form>");
2013-06-27 23:42:40 +04:00
}
beforeEach(function() {
setFormFixture();
2013-04-25 23:08:54 +04:00
this.braintree = Braintree.create('foo');
2013-06-27 23:42:40 +04:00
2013-04-25 23:08:54 +04:00
spyOn(this.braintree, 'encrypt').andCallFake(
2013-06-27 23:42:40 +04:00
function (data) {
2013-04-25 23:08:54 +04:00
return 'encrypted ' + data;
});
});
describe("encryptForm", function() {
it("can be called on the formEncrypter attribute of the braintree instance", function() {
this.braintree.formEncrypter.encryptForm(document.getElementById('braintree_form'));
expect($('input[type="hidden"][name="credit-card-cvv"]')).toExist();
});
it("works when passed an element", function() {
this.braintree.encryptForm(document.getElementById('braintree_form'));
expect($('input[type="hidden"][name="credit-card-cvv"]')).toExist();
});
it("works when passed a jQuery object", function() {
this.braintree.encryptForm($('#braintree_form'));
expect($('input[type="hidden"][name="credit-card-cvv"]')).toExist();
});
it("encrypts fields with data-encrypted-name attribute", function() {
this.braintree.encryptForm('braintree_form');
expect($('input[name="credit-card-number"]')).toHaveValue('encrypted cc number');
});
it("encrypts nested input fields", function() {
this.braintree.encryptForm('braintree_form');
expect($('input[name="credit-card-expiration-date"]')).toHaveValue('encrypted May');
});
2013-05-24 06:30:29 +04:00
it("encrypts <select> input fields", function() {
this.braintree.encryptForm('braintree_form');
expect($('input[name="expiration-year"]')).toHaveValue('encrypted 2013');
});
2013-04-25 23:08:54 +04:00
it("does not encrypt fields without encrypted class", function() {
this.braintree.encryptForm('braintree_form');
expect($('input[name="card-holder-first-name"]')).toHaveValue("bob");
});
2013-05-22 20:14:48 +04:00
it("removes existing hidden inputs previously injected", function() {
this.braintree.encryptForm('braintree_form');
this.braintree.encryptForm('braintree_form');
expect($('input[type="hidden"][name="credit-card-cvv"]').length).toBe(1);
expect($('input[type="hidden"][name="credit-card-expiration-date"]').length).toBe(1);
expect($('input[type="hidden"][name="credit-card-number"]').length).toBe(1);
});
2013-05-24 21:00:19 +04:00
2013-06-27 23:42:40 +04:00
it("shouldn't throw DOM exceptions when attempting to remove hidden inputs that no longer exist in the document", function() {
this.braintree.encryptForm('braintree_form');
setFormFixture();
this.braintree.encryptForm('braintree_form');
});
2013-05-24 21:00:19 +04:00
it("shouldn't throw DOM exceptions when encrypting multiple times", function() {
this.braintree.encryptForm('braintree_form');
this.braintree.encryptForm('braintree_form');
this.braintree.encryptForm('braintree_form');
});
2013-04-25 23:08:54 +04:00
});
describe("onSubmitEncryptForm", function() {
beforeEach(function() {
$("#braintree_form").submit(function(e) {
e.preventDefault();
});
});
it("can be called on the formEncrypter attribute of the braintree instance", function() {
this.braintree.formEncrypter.onSubmitEncryptForm('braintree_form');
$("#click_me").click();
expect($('input[type="hidden"][name="credit-card-cvv"]')).toExist();
});
it("works when passed an element id", function() {
this.braintree.onSubmitEncryptForm('braintree_form');
$("#click_me").click();
expect($('input[type="hidden"][name="credit-card-cvv"]')).toExist();
});
it("has correct ordering with jQuery", function() {
window.called = false;
this.braintree.onSubmitEncryptForm($("#braintree_form"), function() {
window.called = true;
expect($('input[type="hidden"][name="credit-card-cvv"]')).toExist();
expect($('input[type="hidden"][name="credit-card-expiration-date"]')).toExist();
expect($('input[type="hidden"][name="credit-card-number"]')).toExist();
});
$("#click_me").click();
expect(window.called).toBeTruthy();
});
it("has correct ordering without jQuery", function() {
window.called = false;
window.jQuery = void 0;
this.braintree.onSubmitEncryptForm($("#braintree_form")[0], function() {
window.called = true;
window.jQuery = $;
expect($('input[type="hidden"][name="credit-card-cvv"]')).toExist();
expect($('input[type="hidden"][name="credit-card-expiration-date"]')).toExist();
expect($('input[type="hidden"][name="credit-card-number"]')).toExist();
});
$("#click_me").click();
expect(window.called).toBeTruthy();
});
});
});