From 9e25349cbb936231382b8e51dc146d4f1961f153 Mon Sep 17 00:00:00 2001 From: Timothy Lindvall Date: Thu, 9 Apr 2015 18:48:17 -0700 Subject: [PATCH 1/4] Append tour class to HopscotchBubble, if tour. Also move RegEx check for tour id in startTour(). --- src/js/hopscotch.js | 16 +++++++++++----- test/js/test.hopscotch.js | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/js/hopscotch.js b/src/js/hopscotch.js index 37f38147..29624dc3 100644 --- a/src/js/hopscotch.js +++ b/src/js/hopscotch.js @@ -1062,6 +1062,7 @@ numChildren, node, i, + currTour, opt; //Register DOM element for this bubble. @@ -1086,6 +1087,9 @@ el.className = 'hopscotch-bubble animated'; if (!opt.isTourBubble) { utils.addClass(el, 'hopscotch-callout no-number'); + } else { + currTour = winHopscotch.getCurrTour(); + utils.addClass(el, 'tour-' + currTour.id); } /** @@ -1823,16 +1827,18 @@ skippedSteps = {}, self = this; - // Check validity of tour ID. If invalid, throw an error. - if(!tour.id || !validIdRegEx.test(tour.id)) { - throw new Error('Tour ID is using an invalid format. Use alphanumeric, underscores, and/or hyphens only. First character must be a letter.'); - } - // loadTour if we are calling startTour directly. (When we call startTour // from window onLoad handler, we'll use currTour) if (!currTour) { + + // Check validity of tour ID. If invalid, throw an error. + if(!tour.id || !validIdRegEx.test(tour.id)) { + throw new Error('Tour ID is using an invalid format. Use alphanumeric, underscores, and/or hyphens only. First character must be a letter.'); + } + currTour = tour; loadTour.call(this, tour); + } if (typeof stepNum !== undefinedStr) { diff --git a/test/js/test.hopscotch.js b/test/js/test.hopscotch.js index 84bdff08..d5c815ab 100644 --- a/test/js/test.hopscotch.js +++ b/test/js/test.hopscotch.js @@ -153,6 +153,7 @@ describe('Hopscotch', function() { ] }, 1); expect(hopscotch.getCurrStepNum()).toBe(1); + hopscotch.endTour(); }); it('should reject tour IDs that include invalid characters', function(){ From cfc71c6b3ea1f8b9fb365c61ea2061863affa6c0 Mon Sep 17 00:00:00 2001 From: Timothy Lindvall Date: Tue, 21 Apr 2015 15:58:06 -0700 Subject: [PATCH 2/4] Add sanity check to ensure tour exists. --- src/js/hopscotch.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/js/hopscotch.js b/src/js/hopscotch.js index 29624dc3..f2962fa3 100644 --- a/src/js/hopscotch.js +++ b/src/js/hopscotch.js @@ -1831,6 +1831,11 @@ // from window onLoad handler, we'll use currTour) if (!currTour) { + // Sanity check! Is there a tour? + if(!tour){ + throw new Error('Tour data is required for startTour.'); + } + // Check validity of tour ID. If invalid, throw an error. if(!tour.id || !validIdRegEx.test(tour.id)) { throw new Error('Tour ID is using an invalid format. Use alphanumeric, underscores, and/or hyphens only. First character must be a letter.'); From b2fd9bfba859ab812616997b8cec128db1a25997 Mon Sep 17 00:00:00 2001 From: Timothy Lindvall Date: Wed, 22 Apr 2015 14:42:32 -0700 Subject: [PATCH 3/4] Add unit test to cover new functionality. Also destroy HopscotchBubble when a tour ends (we should create a new one for each tour). --- src/js/hopscotch.js | 18 +++++++++++++++++- test/js/test.hopscotch.js | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/js/hopscotch.js b/src/js/hopscotch.js index f2962fa3..719e6e7a 100644 --- a/src/js/hopscotch.js +++ b/src/js/hopscotch.js @@ -1089,7 +1089,9 @@ utils.addClass(el, 'hopscotch-callout no-number'); } else { currTour = winHopscotch.getCurrTour(); - utils.addClass(el, 'tour-' + currTour.id); + if(currTour){ + utils.addClass(el, 'tour-' + currTour.id); + } } /** @@ -1323,6 +1325,19 @@ return bubble; }, + /** + * Destroy the bubble currently associated with Hopscotch. + * This is done when we end the current tour. + * + * @private + */ + destroyBubble = function() { + if(bubble){ + bubble.destroy(); + bubble = null; + } + }, + /** * Convenience method for getting an option. Returns custom config option * or the default config option if no custom value exists. @@ -1996,6 +2011,7 @@ this.removeCallbacks(null, true); this.resetDefaultOptions(); + destroyBubble(); currTour = null; diff --git a/test/js/test.hopscotch.js b/test/js/test.hopscotch.js index d5c815ab..a13d4bdc 100644 --- a/test/js/test.hopscotch.js +++ b/test/js/test.hopscotch.js @@ -156,6 +156,12 @@ describe('Hopscotch', function() { hopscotch.endTour(); }); + it('should complain if no tour data is passed in', function(){ + expect(function(){ + hopscotch.startTour(); + }).toThrow(new Error('Tour data is required for startTour.')); + }); + it('should reject tour IDs that include invalid characters', function(){ expect(function(){ hopscotch.startTour({ @@ -1285,6 +1291,22 @@ describe('HopscotchBubble', function() { expect(content).toBe('It\'s a shopping list'); hopscotch.endTour(); }); + + it('Should include tour ID as part of bubble classes', function(){ + hopscotch.startTour({ + id: 'hs-test-tour-class', + steps: [ { + target: 'shopping-list', + placement: 'left', + title: 'Shopping List', + content: 'It\'s a shopping list' + } ] + }); + bubble = document.querySelector('.hopscotch-bubble.tour-hs-test-tour-class'); + console.log(document.querySelector('.hopscotch-bubble').classList); + expect(bubble).not.toBe(null); + hopscotch.endTour(); + }); }); describe('Step Number', function() { From d8025661217c8b9e55e4a80ef98ba89a681fedb1 Mon Sep 17 00:00:00 2001 From: Timothy Lindvall Date: Wed, 22 Apr 2015 14:53:48 -0700 Subject: [PATCH 4/4] Remove console.log from test. --- test/js/test.hopscotch.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/js/test.hopscotch.js b/test/js/test.hopscotch.js index a13d4bdc..1fc8a140 100644 --- a/test/js/test.hopscotch.js +++ b/test/js/test.hopscotch.js @@ -1303,7 +1303,6 @@ describe('HopscotchBubble', function() { } ] }); bubble = document.querySelector('.hopscotch-bubble.tour-hs-test-tour-class'); - console.log(document.querySelector('.hopscotch-bubble').classList); expect(bubble).not.toBe(null); hopscotch.endTour(); });