describe "Mercury.Region", -> template 'mercury/region.html' beforeEach -> Mercury.config.regions.className = 'custom-region-class' Mercury.config.regions.dataAttributes = [] afterEach -> @region = null delete(@region) $(window).unbind('mercury:mode') $(window).unbind('mercury:focus:frame') $(window).unbind('mercury:action') describe "constructor", -> beforeEach -> @buildSpy = spyOn(Mercury.Region.prototype, 'build').andCallFake(=>) @bindEventsSpy = spyOn(Mercury.Region.prototype, 'bindEvents').andCallFake(=>) @pushHistorySpy = spyOn(Mercury.Region.prototype, 'pushHistory').andCallFake(=>) it "expects an element and window (for context)", -> @region = new Mercury.Region($('#region'), window) expect(@region.element.get(0)).toEqual($('#region').get(0)) it "accepts options", -> @region = new Mercury.Region($('#region'), window, {foo: 'bar'}) expect(@region.options).toEqual({foo: 'bar'}) it "sets variables we need", -> @region = new Mercury.Region($('#region'), window, {foo: 'bar'}) expect(@region.name).toEqual('region') expect(@region.history).toBeDefined() it "sets name based on configuration", -> Mercury.config.regions.identifier = 'data-scope' @region = new Mercury.Region($('#region'), window, {foo: 'bar'}) expect(@region.name).toEqual('scope') it "calls build", -> @region = new Mercury.Region($('#region'), window, {foo: 'bar'}) expect(@buildSpy.callCount).toEqual(1) it "calls bindEvents", -> @region = new Mercury.Region($('#region'), window, {foo: 'bar'}) expect(@bindEventsSpy.callCount).toEqual(1) it "pushes the initial state to the history buffer", -> @region = new Mercury.Region($('#region'), window, {foo: 'bar'}) expect(@pushHistorySpy.callCount).toEqual(1) it "sets the instance of the region into data for the element", -> @region = new Mercury.Region($('#region'), window, {foo: 'bar'}) expect(@region.element.data('region')).toEqual(@region) describe "#build", -> it "does nothing and is there as an interface" describe "#focus", -> it "does nothing and is there as an interface" describe "observed events", -> beforeEach -> @region = new Mercury.Region($('#region_with_snippet'), window) describe "custom event: mode", -> it "calls togglePreview if the mode is preview", -> spy = spyOn(Mercury.Region.prototype, 'togglePreview').andCallFake(=>) Mercury.trigger('mode', {mode: 'foo'}) expect(spy.callCount).toEqual(0) Mercury.trigger('mode', {mode: 'preview'}) expect(spy.callCount).toEqual(1) describe "custom event: focus:frame", -> beforeEach -> @focusSpy = spyOn(Mercury.Region.prototype, 'focus').andCallFake(=>) it "does nothing if in preview mode", -> @region.previewing = true Mercury.trigger('focus:frame', {region: @region}) expect(@focusSpy.callCount).toEqual(0) it "does nothing if it's not active region", -> Mercury.region = {} Mercury.trigger('focus:frame', {region: @region}) expect(@focusSpy.callCount).toEqual(0) it "calls focus", -> Mercury.region = @region Mercury.trigger('focus:frame', {region: @region}) expect(@focusSpy.callCount).toEqual(1) describe "custom event: action", -> beforeEach -> @execCommandSpy = spyOn(Mercury.Region.prototype, 'execCommand').andCallFake(=>) it "does nothing if in preview mode", -> @region.previewing = true Mercury.trigger('action', {action: 'foo', value: 'bar'}) expect(@execCommandSpy.callCount).toEqual(0) it "does nothing if it's not active region", -> Mercury.region = {} Mercury.trigger('action', {action: 'foo', value: 'bar'}) expect(@execCommandSpy.callCount).toEqual(0) it "calls execCommand with the action and options", -> Mercury.region = @region Mercury.trigger('action', {action: 'foo', value: 'bar'}) expect(@execCommandSpy.callCount).toEqual(1) expect(@execCommandSpy.argsForCall[0]).toEqual(['foo', {action: 'foo', value: 'bar'}]) describe "mouseover", -> beforeEach -> @triggerSpy = spyOn(Mercury, 'trigger').andCallFake(=>) it "does nothing if in preview mode", -> @region.previewing = true jasmine.simulate.mousemove($('#region_with_snippet .mercury-snippet').get(0)) expect(@triggerSpy.callCount).toEqual(0) it "does nothing if it's not the active region", -> Mercury.region = {} jasmine.simulate.mousemove($('#region_with_snippet .mercury-snippet').get(0)) expect(@triggerSpy.callCount).toEqual(0) it "shows the snippet toolbar if a snippet was moused over", -> Mercury.region = @region jasmine.simulate.mousemove($('#region_with_snippet .mercury-snippet').get(0)) expect(@triggerSpy.callCount).toEqual(1) expect(@triggerSpy.argsForCall[0][0]).toEqual('show:toolbar') describe "mouseout", -> beforeEach -> @triggerSpy = spyOn(Mercury, 'trigger').andCallFake(=>) it "does nothing if previewing", -> @region.previewing = true jasmine.simulate.mouseout(@region.element.get(0)) expect(@triggerSpy.callCount).toEqual(0) it "hides the snippet toolbar", -> jasmine.simulate.mouseout(@region.element.get(0)) expect(@triggerSpy.callCount).toEqual(1) expect(@triggerSpy.argsForCall[0]).toEqual(['hide:toolbar', {type: 'snippet', immediately: false}]) describe "#html", -> beforeEach -> @region = new Mercury.Region($('#region_with_snippet'), window) describe "getting html", -> it "returns the html of the element", -> content = @region.content() expect(content).toEqual('contents