Skip to content

Commit

Permalink
throw exception if data-type isn't provided or is unknown, and added …
Browse files Browse the repository at this point in the history
…a return false to the overline button context to make tests happier.
  • Loading branch information
jejacks0n committed Jan 1, 2012
1 parent 642a83f commit 9fcadcf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
37 changes: 21 additions & 16 deletions spec/javascripts/mercury/page_editor_spec.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ describe "Mercury.PageEditor", ->
beforeEach ->
@initializeInterfaceSpy = spyOn(Mercury.PageEditor.prototype, 'initializeInterface').andCallFake(=>)

it "throws an error if it's not supported", ->
Mercury.supported = false
expect(=>
new Mercury.PageEditor()
).toThrow('Mercury.PageEditor is unsupported in this client. Supported browsers are chrome 10+, firefix 4+, and safari 5+.')
Mercury.supported = true

it "throws an error if it's already instantiated", ->
window.mercuryInstance = true
expect(=>
Expand Down Expand Up @@ -222,7 +215,7 @@ describe "Mercury.PageEditor", ->
it "it calls buildRegion for all the regions found in a document", ->
spy = spyOn(Mercury.PageEditor.prototype, 'buildRegion').andCallFake(=>)
@pageEditor.initializeRegions()
expect(spy.callCount).toEqual(3)
expect(spy.callCount).toEqual(4)

it "focuses the first region", ->
regionIndex = 0
Expand All @@ -233,10 +226,11 @@ describe "Mercury.PageEditor", ->
regionFocusCall = regionIndex
})
@pageEditor.initializeRegions()
expect(spy.callCount).toEqual(3)
expect(spy.callCount).toEqual(4)
expect(regionFocusCall).toEqual(1)

it "doesn't focus the first region if it's not supposed to be visible", ->
spy = spyOn(Mercury.PageEditor.prototype, 'buildRegion').andCallFake(=>)
firstFocusCalled = false
@pageEditor.regions = [{focus: => firstFocusCalled = true}, {}, {}]
@pageEditor.options.visible = false
Expand All @@ -245,25 +239,34 @@ describe "Mercury.PageEditor", ->


describe "#buildRegion", ->
# it "throws an error if it's not supported", ->
# Mercury.supported = false
# expect(=>
# new Mercury.PageEditor()
# ).toThrow('Mercury.PageEditor is unsupported in this client. Supported browsers are chrome 10+, firefix 4+, and safari 5+.')
# Mercury.supported = true
#

beforeEach ->
@resizeSpy = spyOn(Mercury.PageEditor.prototype, 'resize').andCallFake(=>)
Mercury.PageEditor.prototype.initializeFrame = ->
Mercury.Regions.Editable = -> {region: true}
Mercury.Regions.Editable.supported = true
@pageEditor = new Mercury.PageEditor('', {appendTo: $('#test')})

it "instantiates the region and pushes it into the regions array", ->
@pageEditor.buildRegion($('#region2'))
expect(@pageEditor.regions.length).toEqual(1)
expect(@pageEditor.regions[0]).toEqual({region: true})

it "alerts on errors", ->
Mercury.debug = false
Mercury.Regions.Editable = -> throw('error!')
spy = spyOn(window, 'alert').andCallFake(=>)
@pageEditor.buildRegion($('#region2'))
expect(spy.callCount).toEqual(1)
expect(spy.argsForCall[0]).toEqual(['Region type is malformed, no data-type provided, or "Editable" is unknown for the "region2" region.'])
it "throws an exception when there's no data-type attribute", ->
expect(=> @pageEditor.buildRegion($('#region4'))).toThrow('Region type is malformed, no data-type provided, or "Unknown" is unknown for the "region4" region.')
expect(=> @pageEditor.buildRegion($('#region4'))).toThrow('Region type is malformed, no data-type provided, or "Unknown" is unknown for the "region4" region.')

it "throws an exception when the data-type isn't known", ->
expect(=> @pageEditor.buildRegion($('#region4'))).toThrow('Region type is malformed, no data-type provided, or "Unknown" is unknown for the "region4" region.')
$('#region4').attr('data-type', 'foo')
expect(=> @pageEditor.buildRegion($('#region4'))).toThrow('Region type is malformed, no data-type provided, or "Foo" is unknown for the "region4" region.')

it "doesn't re-instantiate the region if the element's already initialized", ->
$('#region2').data('region', {foo: 'bar'})
Expand All @@ -274,13 +277,15 @@ describe "Mercury.PageEditor", ->
it "calls togglePreview on the region if in preview mode", ->
callCount = 0
Mercury.Regions.Editable = -> {region: true, togglePreview: -> callCount += 1 }
Mercury.Regions.Editable.supported = true
@pageEditor.previewing = true
@pageEditor.buildRegion($('#region2'))
expect(callCount).toEqual(1)

it "doesn't call togglePreview if not in preview mode", ->
callCount = 0
Mercury.Regions.Editable = -> {region: true, togglePreview: -> callCount += 1 }
Mercury.Regions.Editable.supported = true
@pageEditor.buildRegion($('#region2'))
expect(callCount).toEqual(0)

Expand Down
1 change: 1 addition & 0 deletions spec/javascripts/templates/mercury/page_editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<form id="form3r" action="" target="_self"></form>
<form id="form4r" action=""></form>
</div>
<div id="region4" class="custom-region-class"></div>

<a id="anchor1" href="#" target="_top">1</a>
<a id="anchor2" href="#" target="_blank">2</a>
Expand Down
2 changes: 1 addition & 1 deletion vendor/assets/javascripts/mercury/page_editor.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class @Mercury.PageEditor
region = region.data('region')
else
type = (region.data('type') || 'unknown').titleize()
Mercury.notify('Region type is malformed, no data-type provided, or "%s" is unknown for the "%s" region.', type, region.attr('id') || 'unknown') unless Mercury.Regions[type]
throw Mercury.I18n('Region type is malformed, no data-type provided, or "%s" is unknown for the "%s" region.', type, region.attr('id') || 'unknown') if type == 'Unknown' || !Mercury.Regions[type]
if !Mercury.Regions[type].supported
Mercury.notify('Mercury.Regions.%s is unsupported in this client. Supported browsers are %s.', type, Mercury.Regions[type].supportedText)
return false
Expand Down
1 change: 1 addition & 0 deletions vendor/assets/javascripts/mercury/toolbar.button.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class @Mercury.Toolbar.Button
return true if node.css('text-decoration') == 'overline'
for parent in node.parentsUntil(@element)
return true if jQuery(parent).css('text-decoration') == 'overline'
return false

strikethrough: (node, region) -> node.css('text-decoration') == 'line-through' || !!node.closest('strike', region).length

Expand Down

0 comments on commit 9fcadcf

Please sign in to comment.