From e14c60c1c17e2b246e7c762a661bebfca06f179f Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Sun, 10 Feb 2019 18:59:36 +0800 Subject: [PATCH 1/3] Add RSpec matchers for flash messages --- .../matchers/flash_message_matchers.rb | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 spec/support/matchers/flash_message_matchers.rb diff --git a/spec/support/matchers/flash_message_matchers.rb b/spec/support/matchers/flash_message_matchers.rb new file mode 100644 index 0000000000..b781fc1811 --- /dev/null +++ b/spec/support/matchers/flash_message_matchers.rb @@ -0,0 +1,31 @@ +RSpec::Matchers.define :have_flash_message do |message| + match do |node| + @message, @node = message, node + + # Ignore leading and trailing whitespace. Later versions of Capybara have :exact_text option. + # The :exact option is not supported in has_selector?. + message_substring_regex = substring_match_regex(message) + node.has_selector?(".flash", text: message_substring_regex, visible: false) + end + + failure_message do |actual| + "expected to find flash message ##{@message}" + end + + match_when_negated do |node| + @message, @node = message, node + + # Ignore leading and trailing whitespace. Later versions of Capybara have :exact_text option. + # The :exact option is not supported in has_selector?. + message_substring_regex = substring_match_regex(message) + node.has_no_selector?(".flash", text: message_substring_regex, visible: false) + end + + failure_message_when_negated do |actual| + "expected not to find flash message ##{@message}" + end + + def substring_match_regex(text) + /\A\s*#{Regexp.escape(text)}\s*\Z/ + end +end From 175264f41f8ffea35d5c41afa3680809ecbe3004 Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Sun, 10 Feb 2019 19:03:02 +0800 Subject: [PATCH 2/3] Use flash matcher in shipping method feature specs --- spec/features/admin/shipping_methods_spec.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spec/features/admin/shipping_methods_spec.rb b/spec/features/admin/shipping_methods_spec.rb index f6d4935a55..f8763a3c6b 100644 --- a/spec/features/admin/shipping_methods_spec.rb +++ b/spec/features/admin/shipping_methods_spec.rb @@ -32,7 +32,8 @@ feature 'shipping methods' do click_button 'Create' # Then the shipping method should have its distributor set - flash_message.should == 'Shipping method "Carrier Pidgeon" has been successfully created!' + message = "Shipping method \"Carrier Pidgeon\" has been successfully created!" + expect(page).to have_flash_message message sm = Spree::ShippingMethod.last sm.name.should == 'Carrier Pidgeon' @@ -100,7 +101,9 @@ feature 'shipping methods' do click_button 'Create' - flash_message.should == 'Shipping method "Teleport" has been successfully created!' + message = "Shipping method \"Teleport\" has been successfully created!" + expect(page).to have_flash_message message + expect(first('tags-input .tag-list ti-tag-item')).to have_content "local" shipping_method = Spree::ShippingMethod.find_by_name('Teleport') From 0032a237a284e05b11e921d41892db625dd77602 Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Sun, 10 Feb 2019 22:06:35 +0800 Subject: [PATCH 3/3] Wait for button to disappear before checking flash --- spec/features/admin/shipping_methods_spec.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spec/features/admin/shipping_methods_spec.rb b/spec/features/admin/shipping_methods_spec.rb index f8763a3c6b..bb65e70255 100644 --- a/spec/features/admin/shipping_methods_spec.rb +++ b/spec/features/admin/shipping_methods_spec.rb @@ -29,7 +29,9 @@ feature 'shipping methods' do fill_in 'shipping_method_name', with: 'Carrier Pidgeon' check "shipping_method_distributor_ids_#{d1.id}" check "shipping_method_distributor_ids_#{d2.id}" - click_button 'Create' + click_button I18n.t("actions.create") + + expect(page).to have_no_button I18n.t("actions.create") # Then the shipping method should have its distributor set message = "Shipping method \"Carrier Pidgeon\" has been successfully created!" @@ -99,8 +101,9 @@ feature 'shipping methods' do expect(page).to have_css '.tag-item' end - click_button 'Create' + click_button I18n.t("actions.create") + expect(page).to have_no_button I18n.t("actions.create") message = "Shipping method \"Teleport\" has been successfully created!" expect(page).to have_flash_message message