<commit_before>describe "Mercury.PageEditor", ->

  beforeEach ->
    fixture.load('mercury/page_editor.html')
    Mercury.config.regions.attribute = 'custom-region-attribute'

  afterEach ->
    @pageEditor = null
    delete(@pageEditor)
    window.mercuryInstance = null
    $(window).unbind('mercury:initialize:frame')
    $(window).unbind('mercury:focus:frame')
    $(window).unbind('mercury:focus:window')
    $(window).unbind('mercury:toggle:interface')
    $(window).unbind('mercury:reinitialize')
    $(window).unbind('mercury:mode')
    $(window).unbind('mercury:action')
    $(window).unbind('resize')
    $(document).unbind('mousedown')

  describe "constructor", ->

    beforeEach ->
      @initializeInterfaceSpy = spyOn(Mercury.PageEditor.prototype, 'initializeInterface').andCallFake(=>)

    it "throws an error if it's already instantiated", ->
      window.mercuryInstance = true
      expect(=>
        new Mercury.PageEditor()
      ).toThrow('Mercury.PageEditor can only be instantiated once.')
      window.mercuryInstance = false

    it "sets the mercuryInstance to window", ->
      @pageEditor = new Mercury.PageEditor()
      expect(window.mercuryInstance).toEqual(@pageEditor)

    it "accepts a saveUrl, and options", ->
      @pageEditor = new Mercury.PageEditor('/foo/1', {foo: 'bar', saveDataType: 'text'})
      expect(@pageEditor.saveUrl).toEqual('/foo/1')
      expect(@pageEditor.options).toEqual({foo: 'bar', saveDataType: 'text', visible: true})

    it "sets the visible option to true unless it's set", ->
      @pageEditor = new Mercury.PageEditor('/foo/1', {foo: 'bar', visible: false})
      expect(@pageEditor.options.visible).toEqual(false)
      window.mercuryInstance = null
      @pageEditor = new Mercury.PageEditor('/foo/1', {foo: 'bar', visible: true})
      expect(@pageEditor.options.visible).toEqual(true)

    it "sets visible based on the options", ->
      @pageEditor = new Mercury.PageEditor('/foo/1', {foo: 'bar', visible: false})
      expect(@pageEditor.visible).toEqual(false)
      window.mercuryInstance = null
      @pageEditor = new Mercury.PageEditor('/foo/1', {foo: 'bar', visible: true})
      expect(@pageEditor.visible).toEqual(true)

    it "calls initializeInterface", ->
      @pageEditor = new Mercury.PageEditor()
      expect(@initializeInterfaceSpy.callCount).toEqual(1)

    it "gets the csrf token if there's one available", ->
      new Mercury.PageEditor()
      expect(Mercury.csrfToken).toBeDefined()


  describe "#initializeInterface", ->

    beforeEach ->
      @resizeSpy = spyOn(Mercury.PageEditor.prototype, 'resize').andCallFake(=>)
      spyOn(Mercury, "Toolbar").andReturn({toolbar: true})
      spyOn(Mercury, "Statusbar").andReturn({statusbar: true})
      @iframeSrcSpy = spyOn(Mercury.PageEditor.prototype, 'iframeSrc').andCallFake(=> '/foo')

    it "builds a focusable element (so we can get focus off the iframe)", ->
      @pageEditor = new Mercury.PageEditor('', {appendTo: fixture.el})
      expect($('input.mercury-focusable[type=text]').length).toEqual(1)

    it "builds an iframe", ->
      @pageEditor = new Mercury.PageEditor('', {appendTo: fixture.el})
      expect($('iframe.mercury-iframe').length).toEqual(1)

    it "appends the elements to any node", ->
      @pageEditor = new Mercury.PageEditor('', {appendTo: $('#page_editor_container')})<commit_msg>spec asserting iframe id/name and class are the same<commit_after>describe "Mercury.PageEditor", ->

  beforeEach ->
    fixture.load('mercury/page_editor.html')
    Mercury.config.regions.attribute = 'custom-region-attribute'

  afterEach ->
    @pageEditor = null
    delete(@pageEditor)
    window.mercuryInstance = null
    $(window).unbind('mercury:initialize:frame')
    $(window).unbind('mercury:focus:frame')
    $(window).unbind('mercury:focus:window')
    $(window).unbind('mercury:toggle:interface')
    $(window).unbind('mercury:reinitialize')
    $(window).unbind('mercury:mode')
    $(window).unbind('mercury:action')
    $(window).unbind('resize')
    $(document).unbind('mousedown')

  describe "constructor", ->

    beforeEach ->
      @initializeInterfaceSpy = spyOn(Mercury.PageEditor.prototype, 'initializeInterface').andCallFake(=>)

    it "throws an error if it's already instantiated", ->
      window.mercuryInstance = true
      expect(=>
        new Mercury.PageEditor()
      ).toThrow('Mercury.PageEditor can only be instantiated once.')
      window.mercuryInstance = false

    it "sets the mercuryInstance to window", ->
      @pageEditor = new Mercury.PageEditor()
      expect(window.mercuryInstance).toEqual(@pageEditor)

    it "accepts a saveUrl, and options", ->
      @pageEditor = new Mercury.PageEditor('/foo/1', {foo: 'bar', saveDataType: 'text'})
      expect(@pageEditor.saveUrl).toEqual('/foo/1')
      expect(@pageEditor.options).toEqual({foo: 'bar', saveDataType: 'text', visible: true})

    it "sets the visible option to true unless it's set", ->
      @pageEditor = new Mercury.PageEditor('/foo/1', {foo: 'bar', visible: false})
      expect(@pageEditor.options.visible).toEqual(false)
      window.mercuryInstance = null
      @pageEditor = new Mercury.PageEditor('/foo/1', {foo: 'bar', visible: true})
      expect(@pageEditor.options.visible).toEqual(true)

    it "sets visible based on the options", ->
      @pageEditor = new Mercury.PageEditor('/foo/1', {foo: 'bar', visible: false})
      expect(@pageEditor.visible).toEqual(false)
      window.mercuryInstance = null
      @pageEditor = new Mercury.PageEditor('/foo/1', {foo: 'bar', visible: true})
      expect(@pageEditor.visible).toEqual(true)

    it "calls initializeInterface", ->
      @pageEditor = new Mercury.PageEditor()
      expect(@initializeInterfaceSpy.callCount).toEqual(1)

    it "gets the csrf token if there's one available", ->
      new Mercury.PageEditor()
      expect(Mercury.csrfToken).toBeDefined()


  describe "#initializeInterface", ->

    beforeEach ->
      @resizeSpy = spyOn(Mercury.PageEditor.prototype, 'resize').andCallFake(=>)
      spyOn(Mercury, "Toolbar").andReturn({toolbar: true})
      spyOn(Mercury, "Statusbar").andReturn({statusbar: true})
      @iframeSrcSpy = spyOn(Mercury.PageEditor.prototype, 'iframeSrc').andCallFake(=> '/foo')

    it "builds a focusable element (so we can get focus off the iframe)", ->
      @pageEditor = new Mercury.PageEditor('', {appendTo: fixture.el})
      expect($('input.mercury-focusable[type=text]').length).toEqual(1)

    it "builds an iframe, setting id, class and name", ->
      @pageEditor = new Mercury.PageEditor('', {appendTo: fixture.el})
      iframe = $('iframe.mercury-iframe')
      expect(iframe.length).toEqual(1)
      expect(iframe.attr('name')).toEqual 'mercury_iframe'
      expect(iframe.attr('id')).toEqual   'mercury_iframe'

    it "appends the elements to any node", ->
      @pageEditor = new Mercury.PageEditor('', {appendTo: $('#page_editor_container')})
{'fraction_non_alphanumeric': 0.15513023456612463, 'fraction_numerical': 0.002590300762699669, 'mean_word_length': 3.8943661971830985, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5895084', 'n_tokens_mistral': 2120, 'n_tokens_neox': 2106, 'n_words': 432}