From da840160c3f9379d35099340abb68ecffaa8dead Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 14 Mar 2012 08:36:10 -0400 Subject: [PATCH] more work on features --- .gitignore | 1 + js-features/presentation_timer.feature | 4 ++- ....js.coffee => i_start_the_timer.js.coffee} | 0 .../steps/when/i_stop_the_timer.js.coffee | 3 ++ js-features/steps/when/i_wait_time.js.coffee | 3 -- .../attentive/presentation_timer.js.coffee | 18 ++++++++---- .../presentation_timer_spec.js.coffee | 28 ++++++++++++++++++- 7 files changed, 47 insertions(+), 10 deletions(-) rename js-features/steps/when/{i_start_the_counter.js.coffee => i_start_the_timer.js.coffee} (100%) create mode 100644 js-features/steps/when/i_stop_the_timer.js.coffee delete mode 100644 js-features/steps/when/i_wait_time.js.coffee diff --git a/.gitignore b/.gitignore index 11e079c..c00f833 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ spec/reports test/tmp test/version_tmp tmp +.node-* diff --git a/js-features/presentation_timer.feature b/js-features/presentation_timer.feature index 5d202c0..901d972 100644 --- a/js-features/presentation_timer.feature +++ b/js-features/presentation_timer.feature @@ -7,7 +7,9 @@ Feature: Presentation Timer Then the counter should show the time "00:00" And the counter should not be running - Scenario: Start the timer + Scenario: Manage the timer When I start the timer Then the counter should be running + When I stop the timer + Then the counter should not be running diff --git a/js-features/steps/when/i_start_the_counter.js.coffee b/js-features/steps/when/i_start_the_timer.js.coffee similarity index 100% rename from js-features/steps/when/i_start_the_counter.js.coffee rename to js-features/steps/when/i_start_the_timer.js.coffee diff --git a/js-features/steps/when/i_stop_the_timer.js.coffee b/js-features/steps/when/i_stop_the_timer.js.coffee new file mode 100644 index 0000000..9117221 --- /dev/null +++ b/js-features/steps/when/i_stop_the_timer.js.coffee @@ -0,0 +1,3 @@ +Flowerbox.When /^I stop the timer$/, -> + @timer.stop() + diff --git a/js-features/steps/when/i_wait_time.js.coffee b/js-features/steps/when/i_wait_time.js.coffee deleted file mode 100644 index 33e0228..0000000 --- a/js-features/steps/when/i_wait_time.js.coffee +++ /dev/null @@ -1,3 +0,0 @@ -Flowerbox.When /^I wait (\d+) seconds?$/, (secs) -> - Flowerbox.pause(Number(secs) * 1000) - diff --git a/lib/assets/javascripts/attentive/presentation_timer.js.coffee b/lib/assets/javascripts/attentive/presentation_timer.js.coffee index b6eab2a..ec76012 100644 --- a/lib/assets/javascripts/attentive/presentation_timer.js.coffee +++ b/lib/assets/javascripts/attentive/presentation_timer.js.coffee @@ -10,23 +10,31 @@ class Attentive.PresentationTimer ensureEl: -> if !@el - @el = document.createElement('div') + @el = this._createDiv() @el.classList.add('timer') @el + _createDiv: -> document.createElement('div') + + addClass: (className) -> + @ensureEl().classList.add(className) + start: -> @_runner = this.runner() - @ensureEl().classList.add('running') + this.addClass('running') runner: -> setTimeout( => - this.render() - @time += 1 - this.runner() if @_runner? + this.handleRunner() , 1000 ) + handleRunner: -> + this.render() + @time += 1 + this.runner() if @_runner? + stop: -> clearTimeout(@_runner) @ensureEl().classList.remove('running') diff --git a/spec/javascripts/attentive/presentation_timer_spec.js.coffee b/spec/javascripts/attentive/presentation_timer_spec.js.coffee index 3e16bd4..7cc80fc 100644 --- a/spec/javascripts/attentive/presentation_timer_spec.js.coffee +++ b/spec/javascripts/attentive/presentation_timer_spec.js.coffee @@ -1,5 +1,31 @@ #= require attentive/presentation_timer describe 'Attentive.PresentationTimer', -> + beforeEach -> + @timer = new Attentive.PresentationTimer() + describe '#render', -> - it 'should do something', -> + time = 'time' + elStub = null + + beforeEach -> + elStub = {} + @timer.ensureEl = -> elStub + @timer.formattedTime = -> time + + it 'should render', -> + @timer.render() + expect(elStub.innerHTML).toEqual(time) + + describe '#ensureEl', -> + context 'with el', -> + el = 'el' + + beforeEach -> + @timer.el = el + + it 'should return the existing value', -> + expect(@timer.ensureEl()).toEqual(el) + + context 'without el', -> +