Skip to content

Commit

Permalink
Updated decorator to use section aggregate counts and updated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Mendiola committed Dec 13, 2015
1 parent 506c8b5 commit 835ae58
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
22 changes: 8 additions & 14 deletions lib/todo-decorator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,12 @@ module.exports = todoDecorator =
editor.decorateMarker(marker, type: 'overlay', item: overlayElement)

decorateSection: (editor, section, selectedUnit) ->

if selectedUnit == null
return
else if selectedUnit == 'time'
overlay = @createSectionHoursOverlay(section)
else if selectedUnit == 'pts'
overlay = @createSectionPointsOverlay(section)
else if selectedUnit == 'cal'
overlay = @createSectionCaloriesOverlay(section)
else if selectedUnit in ['pts', 'cal']
overlay = @createSectionUnitsOverlay(section, selectedUnit)

if overlay != null
marker = @createMarker(editor, section.textRange)
Expand All @@ -114,15 +111,12 @@ module.exports = todoDecorator =
percentage = section.estimateDoneDuration.asSeconds() / section.estimateTotalDuration.asSeconds()
overlay = @createSectionOverlayElement(content, percentage)

createSectionPointsOverlay: (section) ->


createSectionCaloriesOverlay: (section) ->
totalCalories = section.getTotalCalories()
completedCalories = section.getCompletedCalories()
if(totalCalories != 0 && completedCalories != 0)
content = "#{completedCalories}cal / #{totalCalories}cal"
percentage = completedCalories / totalCalories
createSectionUnitsOverlay: (section, unit) ->
total = section.getTotalAmount(unit)
completed = section.getCompletedAmount(unit)
if(total != 0 && completed != 0)
content = "#{completed}#{unit} / #{total}#{unit}"
percentage = total / completed
overlay = @createSectionOverlayElement(content, percentage)

decorateItem: (editor, item, isFirstWeek, todayString, highlightedDay) ->
Expand Down
47 changes: 23 additions & 24 deletions spec/decorator-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe 'TodoDecorator', ->
decorateMarker: ->
mockSection =
textRange: [[0, 1], [0,10]]
getTotalCalories: ->
getCompletedCalories: ->
getTotalAmount: ->
getCompletedAmount: ->

spyOn(mockEditor, 'decorateMarker')

Expand All @@ -28,8 +28,7 @@ describe 'TodoDecorator', ->
describe 'decorateSection', ->
beforeEach ->
spyOn(decorator, 'createSectionHoursOverlay')
spyOn(decorator, 'createSectionPointsOverlay')
spyOn(decorator, 'createSectionCaloriesOverlay')
spyOn(decorator, 'createSectionUnitsOverlay')

it 'does not create or decorate a marker when selected unit is null', ->
decorator.decorateSection(mockEditor, mockSection, null)
Expand All @@ -40,35 +39,35 @@ describe 'TodoDecorator', ->
decorator.decorateSection(mockEditor, mockSection, 'time')
expect(decorator.createSectionHoursOverlay).toHaveBeenCalled()

it 'calls createSectionPointsOverlay when selected unit is pts', ->
it 'calls createSectionUnitsOverlay when selected unit is pts', ->
decorator.decorateSection(mockEditor, mockSection, 'pts')
expect(decorator.createSectionPointsOverlay).toHaveBeenCalled()
expect(decorator.createSectionUnitsOverlay).toHaveBeenCalled()

it 'calls createSectionCaloriesOverlay when selected unit is cal', ->
it 'calls createSectionUnitsOverlay when selected unit is cal', ->
decorator.decorateSection(mockEditor, mockSection, 'cal')
expect(decorator.createSectionCaloriesOverlay).toHaveBeenCalled()
expect(decorator.createSectionUnitsOverlay).toHaveBeenCalled()

describe 'createSectionHoursOverlay', ->
describe 'createSectionPointsOverlay', ->

describe 'createSectionCaloriesOverlay', ->
it 'calls section.getTotalCalories and section.getCompletedCalories', ->
spyOn(mockSection, 'getTotalCalories')
spyOn(mockSection, 'getCompletedCalories')
decorator.createSectionCaloriesOverlay(mockSection)
expect(mockSection.getTotalCalories).toHaveBeenCalled()
expect(mockSection.getCompletedCalories).toHaveBeenCalled()

it 'creates an overlayElement if the section has calories', ->
spyOn(mockSection, 'getTotalCalories').andReturn(2)
spyOn(mockSection, 'getCompletedCalories').andReturn(1)
decorator.createSectionCaloriesOverlay(mockSection)
describe 'createSectionUnitsOverlay', ->
it 'calls section.getTotalAmount and section.getCompletedAmount', ->
spyOn(mockSection, 'getTotalAmount')
spyOn(mockSection, 'getCompletedAmount')
decorator.createSectionUnitsOverlay(mockSection, 'cal')
expect(mockSection.getTotalAmount).toHaveBeenCalledWith('cal')
expect(mockSection.getCompletedAmount).toHaveBeenCalledWith('cal')

it 'creates an overlayElement if the section has amount of requested unit', ->
spyOn(mockSection, 'getTotalAmount').andReturn(2)
spyOn(mockSection, 'getCompletedAmount').andReturn(1)
decorator.createSectionUnitsOverlay(mockSection, 'cal')
expect(decorator.createSectionOverlayElement).toHaveBeenCalled()

it 'does not create an overlayElement if the section has no calories', ->
spyOn(mockSection, 'getTotalCalories').andReturn(0)
spyOn(mockSection, 'getCompletedCalories').andReturn(0)
decorator.createSectionCaloriesOverlay(mockSection)
it 'does not create an overlayElement if the section has no amount of requested unit', ->
spyOn(mockSection, 'getTotalAmount').andReturn(0)
spyOn(mockSection, 'getCompletedAmount').andReturn(0)
decorator.createSectionUnitsOverlay(mockSection)
expect(decorator.createSectionOverlayElement).not.toHaveBeenCalled()


Expand Down

0 comments on commit 835ae58

Please sign in to comment.