Skip to content

Commit

Permalink
Don't start a tour if the first step's attachTo isn't present, resolves
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Abbett committed Jan 8, 2020
1 parent 836a022 commit aa4e32a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions abraham.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'rubocop'
s.add_development_dependency 'listen'
s.add_development_dependency 'web-console'
end
8 changes: 7 additions & 1 deletion app/views/application/_abraham.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@
tour.start = function (start) {
return function () {
// Don't start the tour if the user dismissed it once this session
if (!Cookies.get('<%= abraham_cookie_prefix %>-<%= tour_name %>', {domain: '<%= abraham_domain %>'})) {
var tourMayStart = !Cookies.get('<%= abraham_cookie_prefix %>-<%= tour_name %>', {domain: '<%= abraham_domain %>'});
<% if steps.first[1]['attachTo'] %>
// Don't start the tour if the first step's element is missing
tourMayStart = tourMayStart && document.querySelector("<%= steps.first[1]['attachTo']['element'] %>");
<% end %>

if (tourMayStart) {
start();
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/controllers/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

class DashboardController < ApplicationController
def home; end

def other; end
def missing; end
end
5 changes: 5 additions & 0 deletions test/dummy/app/views/dashboard/missing.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>missing</h1>

This page does not have the first step attachTo element,
so we would expect the tour not to start,
even though the second step could be rendered.
2 changes: 1 addition & 1 deletion test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

Rails.application.routes.draw do
get "dashboard/home"

get "dashboard/other"
get "dashboard/missing"

# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
10 changes: 10 additions & 0 deletions test/dummy/config/tours/dashboard/missing.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tour:
steps:
1:
title: "This step points to a missing element"
text: "This should not skip to the second step"
attachTo:
element: "#i-dont-exist"
placement: "right"
2:
text: "You should not see me!"
3 changes: 3 additions & 0 deletions test/dummy/config/tours/dashboard/other.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ tour_one:
1:
title: "TOUR ONE step one ENGLISH"
text: "we show this on your first visit"
attachTo:
element: "p"
placement: "top"

tour_two:
steps:
Expand Down
6 changes: 6 additions & 0 deletions test/dummy/test/system/tours_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ class ToursTest < ApplicationSystemTestCase
find("a").click
assert_selector ".shepherd-element", visible: true
end

test "tour with missing first step attachTo does not appear" do
visit dashboard_missing_url
# No tour should be visible, since the first step is invalid
refute_selector ".shepherd-element"
end
end

0 comments on commit aa4e32a

Please sign in to comment.