diff options
author | Mike Greiling <mike@pixelcog.com> | 2017-10-18 05:30:36 +0300 |
---|---|---|
committer | Mike Greiling <mike@pixelcog.com> | 2017-10-18 05:30:36 +0300 |
commit | 5e63566bf1fde65de8894f3fd588846853d139d9 (patch) | |
tree | 6e45a94edb47f731915d9b073b15bee22c5012f2 | |
parent | 585a2ab577bc5714817f643ce4afd72ae8d832c3 (diff) |
add InputHelper rspec helper to simulate non-BMP character input
-rw-r--r-- | app/assets/javascripts/test_utils/index.js | 2 | ||||
-rw-r--r-- | app/assets/javascripts/test_utils/simulate_input.js | 23 | ||||
-rw-r--r-- | spec/features/issues/gfm_autocomplete_spec.rb | 17 | ||||
-rw-r--r-- | spec/support/input_helper.rb | 8 |
4 files changed, 45 insertions, 5 deletions
diff --git a/app/assets/javascripts/test_utils/index.js b/app/assets/javascripts/test_utils/index.js index 8875590f0f2..a55a338eea8 100644 --- a/app/assets/javascripts/test_utils/index.js +++ b/app/assets/javascripts/test_utils/index.js @@ -1,6 +1,8 @@ import 'core-js/es6/map'; import 'core-js/es6/set'; import simulateDrag from './simulate_drag'; +import simulateInput from './simulate_input'; // Export to global space for rspec to use window.simulateDrag = simulateDrag; +window.simulateInput = simulateInput; diff --git a/app/assets/javascripts/test_utils/simulate_input.js b/app/assets/javascripts/test_utils/simulate_input.js new file mode 100644 index 00000000000..2aa8c99cfe8 --- /dev/null +++ b/app/assets/javascripts/test_utils/simulate_input.js @@ -0,0 +1,23 @@ +function triggerEvents(input) { + input.dispatchEvent(new Event('keydown')); + input.dispatchEvent(new Event('keypress')); + input.dispatchEvent(new Event('input')); + input.dispatchEvent(new Event('keyup')); +} + +export default function simulateInput(target, text) { + const input = document.querySelector(target); + if (!input || !input.matches('textarea, input')) { + return false; + } + + if (text.length > 0) { + Array.prototype.forEach.call(text, (char) => { + input.value += char; + triggerEvents(input); + }); + } else { + triggerEvents(); + } + return true; +} diff --git a/spec/features/issues/gfm_autocomplete_spec.rb b/spec/features/issues/gfm_autocomplete_spec.rb index c00631d9c8f..640e29183be 100644 --- a/spec/features/issues/gfm_autocomplete_spec.rb +++ b/spec/features/issues/gfm_autocomplete_spec.rb @@ -1,6 +1,8 @@ require 'rails_helper' feature 'GFM autocomplete', :js do + include InputHelper + let(:user) { create(:user, name: '💃speciąl someone💃', username: 'someone.special') } let(:project) { create(:project) } let(:label) { create(:label, project: project, title: 'special+') } @@ -14,10 +16,14 @@ feature 'GFM autocomplete', :js do wait_for_requests end + after do + execute_script("localStorage.clear();"); + end + it 'updates issue descripton with GFM reference' do find('.issuable-edit').click - find('#issue-description').native.send_keys("@#{user.name[0...3]}") + simulateInput('#issue-description', "@#{user.name[0...3]}") find('.atwho-view .cur').click @@ -100,7 +106,7 @@ feature 'GFM autocomplete', :js do it 'includes items for assignee dropdowns with non-ASCII characters in name' do page.within '.timeline-content-form' do find('#note-body').native.send_keys('') - find('#note-body').native.send_keys("@#{user.name[0...8]}") + simulateInput('#note-body', "@#{user.name[0...8]}"); end expect(page).to have_selector('.atwho-container') @@ -128,7 +134,7 @@ feature 'GFM autocomplete', :js do note = find('#note-body') page.within '.timeline-content-form' do note.native.send_keys('') - note.native.send_keys("~#{label.title[0]}") + simulateInput('#note-body', "~#{label.title[0]}") note.click end @@ -195,7 +201,7 @@ feature 'GFM autocomplete', :js do note = find('#note-body') page.within '.timeline-content-form' do note.native.send_keys('') - note.native.send_keys(":cartwheel") + note.native.send_keys(":cartwheel_") note.click end @@ -228,7 +234,8 @@ feature 'GFM autocomplete', :js do note.click end - find('.atwho-view li', text: '/assign').native.send_keys(:tab) + find('.atwho-view li', text: '/assign') + note.native.send_keys(:tab) user_item = find('.atwho-view li', text: user.username) expect(user_item).to have_content(user.username) diff --git a/spec/support/input_helper.rb b/spec/support/input_helper.rb new file mode 100644 index 00000000000..d51c4fa3970 --- /dev/null +++ b/spec/support/input_helper.rb @@ -0,0 +1,8 @@ +# see app/assets/javascripts/test_utils/simulate_input.js + +module InputHelper + def simulateInput(selector, input = '') + evaluate_script("window.simulateInput(#{selector.to_json}, #{input.to_json});") + end +end +
\ No newline at end of file |