mask.spec.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { haveValue, html, test } from '../../utils'
  2. test('x-mask',
  3. [html`<input x-data x-mask="(999) 999-9999">`],
  4. ({ get }) => {
  5. // Type a phone number:
  6. get('input').type('12').should(haveValue('(12'))
  7. get('input').type('3').should(haveValue('(123) '))
  8. get('input').type('4567890').should(haveValue('(123) 456-7890'))
  9. // Clear it & paste formatted version in:
  10. get('input').type('{selectAll}{backspace}')
  11. get('input').invoke('val', '(123) 456-7890').trigger('blur')
  12. get('input').should(haveValue('(123) 456-7890'))
  13. // Clear it & paste un-formatted version in:
  14. get('input').type('{selectAll}{backspace}')
  15. get('input').invoke('val', '1234567890').trigger('blur')
  16. get('input').should(haveValue('(123) 456-7890'))
  17. // Make sure backspace works.
  18. get('input').type('{backspace}').should(haveValue('(123) 456-789'))
  19. get('input').type('{backspace}').should(haveValue('(123) 456-78'))
  20. get('input').type('{backspace}').should(haveValue('(123) 456-7'))
  21. get('input').type('{backspace}').should(haveValue('(123) 456-'))
  22. get('input').type('{backspace}').should(haveValue('(123) 456'))
  23. get('input').type('{backspace}').should(haveValue('(123) 45'))
  24. // Make sure you can't type other characters.
  25. get('input').type('a').should(haveValue('(123) 45'))
  26. get('input').type('-').should(haveValue('(123) 45'))
  27. // Put cursor in other places in the input and make sure you can type.
  28. get('input').type('67890').should(haveValue('(123) 456-7890'))
  29. get('input').type('{leftArrow}{leftArrow}{leftArrow}{leftArrow}{leftArrow}')
  30. get('input').type('123456').should(haveValue('(123) 456-1234'))
  31. },
  32. )
  33. test('x-mask with x-model',
  34. [html`
  35. <div x-data="{ value: '' }">
  36. <input x-mask="(999) 999-9999" x-model="value" id="1">
  37. <input id="2" x-model="value">
  38. </div>
  39. `],
  40. ({ get }) => {
  41. // Type a phone number:
  42. get('#1').type('12').should(haveValue('(12'))
  43. get('#2').should(haveValue('(12'))
  44. get('#1').type('3').should(haveValue('(123) '))
  45. get('#2').should(haveValue('(123) '))
  46. get('#1').type('4567890').should(haveValue('(123) 456-7890'))
  47. get('#2').should(haveValue('(123) 456-7890'))
  48. // Clear it & paste formatted version in:
  49. get('#1').type('{selectAll}{backspace}')
  50. get('#1').invoke('val', '(123) 456-7890').trigger('input')
  51. get('#1').should(haveValue('(123) 456-7890'))
  52. get('#2').should(haveValue('(123) 456-7890'))
  53. // Clear it & paste un-formatted version in:
  54. get('#1').type('{selectAll}{backspace}')
  55. get('#1').invoke('val', '1234567890').trigger('input')
  56. get('#1').should(haveValue('(123) 456-7890'))
  57. get('#2').should(haveValue('(123) 456-7890'))
  58. },
  59. )
  60. test('x-mask with non wildcard alpha-numeric characters (b)',
  61. [html`<input x-data x-mask="ba9*b">`],
  62. ({ get }) => {
  63. get('input').type('a').should(haveValue('ba'))
  64. get('input').type('a').should(haveValue('ba'))
  65. get('input').type('3').should(haveValue('ba3'))
  66. get('input').type('z').should(haveValue('ba3zb'))
  67. get('input').type('{backspace}{backspace}4').should(haveValue('ba34b'))
  68. },
  69. )
  70. test('x-mask:dynamic',
  71. [html`<input x-data x-mask:dynamic="'(999)'">`],
  72. ({ get }) => {
  73. get('input').type('123').should(haveValue('(123)'))
  74. }
  75. )
  76. test('$money',
  77. [html`<input x-data x-mask:function="$money">`],
  78. ({ get }) => {
  79. get('input').type('30.00').should(haveValue('30.00'))
  80. get('input').type('5').should(haveValue('30.00'))
  81. get('input').type('{backspace}').should(haveValue('30.0'))
  82. get('input').type('5').should(haveValue('30.05'))
  83. get('input').type('{selectAll}{backspace}').should(haveValue(''))
  84. get('input').type('123').should(haveValue('123'))
  85. get('input').type('4').should(haveValue('1,234'))
  86. get('input').type('567').should(haveValue('1,234,567'))
  87. get('input').type('.89').should(haveValue('1,234,567.89'))
  88. get('input').type('{leftArrow}7').should(haveValue('1,234,567.87'))
  89. get('input').type('{leftArrow}{leftArrow}{leftArrow}89').should(haveValue('123,456,789.87'))
  90. get('input').type('{leftArrow}{leftArrow}{leftArrow}{leftArrow}12').should(haveValue('12,345,612,789.87'))
  91. get('input').type('{leftArrow}3').should(haveValue('123,456,123,789.87'))
  92. // Clear it & paste formatted version in:
  93. get('input').type('{selectAll}{backspace}')
  94. get('input').invoke('val', '123,456,132,789.87').trigger('blur')
  95. get('input').should(haveValue('123,456,132,789.87'))
  96. // Clear it & paste un-formatted version in:
  97. get('input').type('{selectAll}{backspace}')
  98. get('input').invoke('val', '123456132789.87').trigger('blur')
  99. get('input').should(haveValue('123,456,132,789.87'))
  100. },
  101. )
  102. test('$money swapping commas and periods',
  103. [html`<input x-data x-mask:function="$money($input, ',')">`],
  104. ({ get }) => {
  105. get('input').type('30,00').should(haveValue('30,00'))
  106. get('input').type('5').should(haveValue('30,00'))
  107. get('input').type('{backspace}').should(haveValue('30,0'))
  108. get('input').type('5').should(haveValue('30,05'))
  109. get('input').type('{selectAll}{backspace}').should(haveValue(''))
  110. get('input').type('123').should(haveValue('123'))
  111. get('input').type('4').should(haveValue('1.234'))
  112. get('input').type('567').should(haveValue('1.234.567'))
  113. get('input').type(',89').should(haveValue('1.234.567,89'))
  114. },
  115. )