Skip to content
This repository has been archived by the owner on Jan 10, 2018. It is now read-only.

Commit

Permalink
Fix build, bump deps for node4, sync currentTarget.
Browse files Browse the repository at this point in the history
Existing `master` was failing. We specify our jquery dependency with
`>= 1.5` which at some point between the previous PR and now started
picking up a `>2.0.0` release. jQuery2 doesn't work with the old version
of jsdom we use. The latest version of jsdom requires nodejs >= 4.0.

The latest jsdom also introduced some issues with our tests: previously,
`selectionStart` was undefined, but they have since added support for
it. Some tests set values directly e.g. with `.val('4242')` and then try
to format by triggering an event. This once worked when `selectionStart`
was `undefined`, but since jsdom added a `selectionStart` API, our
`formatCardNumber` implementation saw it as `0` (rather than
`undefined`), not matching the end of the input, and returned early.
Those tests were updated to move `selectionStart` to the correct
position before triggering the formatting event.

The latest jsdom + zepto also results in `e.currentTarget` being
undefined in async handling of events, for some reason. In the `format*`
funcs, `e.currentTarget` is saved to a local synchronously. In the
`reFormat*` funcs, it was grabbed async, causing some tests to
fail. The `reFormat*` funcs were updated to grab `e.currentTarget`
synchronously. This was the only change to production code.
  • Loading branch information
jenan-stripe committed Jan 17, 2016
1 parent 5e562dc commit 3981695
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
node_js:
- "0.10"
- "4"
20 changes: 12 additions & 8 deletions lib/jquery.payment.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/jquery.payment.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"devDependencies": {
"cake": "~0.1",
"coffee-script": "~1.7",
"jsdom": "~0.10",
"jsdom": "~7.2",
"mocha": "~1.18",
"uglify-js": "~2.4.24",
"zeptojs": "~1.1"
Expand Down
8 changes: 4 additions & 4 deletions src/jquery.payment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ replaceFullWidthChars = (str = '') ->
# Format Numeric

reFormatNumeric = (e) ->
$target = $(e.currentTarget)
setTimeout ->
$target = $(e.currentTarget)
value = $target.val()
value = replaceFullWidthChars(value)
value = value.replace(/\D/g, '')
Expand All @@ -190,8 +190,8 @@ reFormatNumeric = (e) ->
# Format Card Number

reFormatCardNumber = (e) ->
$target = $(e.currentTarget)
setTimeout ->
$target = $(e.currentTarget)
value = $target.val()
value = replaceFullWidthChars(value)
value = $.payment.formatCardNumber(value)
Expand Down Expand Up @@ -254,8 +254,8 @@ formatBackCardNumber = (e) ->
# Format Expiry

reFormatExpiry = (e) ->
$target = $(e.currentTarget)
setTimeout ->
$target = $(e.currentTarget)
value = $target.val()
value = replaceFullWidthChars(value)
value = $.payment.formatExpiry(value)
Expand Down Expand Up @@ -324,8 +324,8 @@ formatBackExpiry = (e) ->
# Format CVC

reFormatCVC = (e) ->
$target = $(e.currentTarget)
setTimeout ->
$target = $(e.currentTarget)
value = $target.val()
value = replaceFullWidthChars(value)
value = value.replace(/\D/g, '')[0...4]
Expand Down
2 changes: 1 addition & 1 deletion test/jquery.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
window = require('jsdom').jsdom().createWindow()
window = require('jsdom').jsdom().defaultView
global.$ = require('jquery')(window)
global.window = window
global.document = window.document
Expand Down
4 changes: 2 additions & 2 deletions test/specs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe 'jquery.payment', ->
describe 'formatCardNumber', ->
it 'should format cc number correctly', (done) ->
$number = $('<input type=text>').payment('formatCardNumber')
$number.val('4242')
$number.val('4242').prop('selectionStart', 4)

e = $.Event('keypress')
e.which = 52 # '4'
Expand All @@ -296,7 +296,7 @@ describe 'jquery.payment', ->

it 'should format amex cc number correctly', (done) ->
$number = $('<input type=text>').payment('formatCardNumber')
$number.val('3782')
$number.val('3782').prop('selectionStart', 4)

e = $.Event('keypress')
e.which = 56 # '8'
Expand Down
2 changes: 1 addition & 1 deletion test/zepto.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
window = require('jsdom').jsdom().createWindow()
window = require('jsdom').jsdom().defaultView
global.window = window
global.document = window.document
global.getComputedStyle = window.getComputedStyle
Expand Down

0 comments on commit 3981695

Please sign in to comment.