Skip to content

Commit

Permalink
Destroy method
Browse files Browse the repository at this point in the history
  • Loading branch information
leoasis committed Nov 12, 2014
1 parent ba7ae46 commit 049c176
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/core/editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Editor
@timer = setInterval(_.bind(this.checkUpdate, this), @options.pollInterval)
this.enable() unless @options.readOnly

destroy: ->
clearInterval(@timer)

disable: ->
this.enable(false)

Expand Down
10 changes: 10 additions & 0 deletions src/quill.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ class Quill extends EventEmitter2
this.addModule(name, option)
)

destroy: ->
html = this.getHTML()
_.each(@modules, (module, name) ->
module.destroy() if _.isFunction(module.destroy)
)
@editor.destroy()
this.removeAllListeners()
Quill.editors.splice(_.indexOf(Quill.editors, this), 1)
@container.innerHTML = html

addContainer: (className, before = false) ->
refNode = if before then @root else null
container = document.createElement('div')
Expand Down
35 changes: 30 additions & 5 deletions test/unit/core/editor.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
describe('Editor', ->
beforeEach( ->
resetContainer()
@container = $('#test-container').html('<div></div>').get(0)
Quill.Lib.EventEmitter2.events = Quill.events
emitter = new Quill.Lib.EventEmitter2
@editor = new Quill.Editor(@container.firstChild, emitter, { formats: Quill.DEFAULTS.formats })
@createEditor = (options) ->
resetContainer()
@container = $('#test-container').html('<div></div>').get(0)
Quill.Lib.EventEmitter2.events = Quill.events
emitter = new Quill.Lib.EventEmitter2
@editor = new Quill.Editor(@container.firstChild, emitter, options)

@createEditor({ formats: Quill.DEFAULTS.formats })
)

describe('_deleteAt()', ->
Expand Down Expand Up @@ -245,4 +248,26 @@ describe('Editor', ->
expect(@editor.root).toEqualHTML(expected, true)
)
)

describe('destroy()', ->
beforeEach( ->
jasmine.clock().install()
# Mock checkUpdate() before creating the editor because it is bound
# in the constructor.
spyOn(Quill.Editor.prototype, 'checkUpdate')

@createEditor({ formats: Quill.DEFAULTS.formats, pollInterval: 100 })

@editor.destroy()
)

afterEach( ->
jasmine.clock().uninstall()
)

it('clears the update timer', ->
jasmine.clock().tick(5 * @editor.options.pollInterval)
expect(@editor.checkUpdate).not.toHaveBeenCalled()
)
)
)
52 changes: 52 additions & 0 deletions test/unit/core/quill.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,56 @@ describe('Quill', ->
expect(source).toEqual('silent')
)
)

describe('destroy()', ->
htmlBeforeDestroying = null

beforeEach( ->
spyOn(@quill.editor, 'destroy')

Destroyable = ->
@destroy = jasmine.createSpy('destroy')
undefined

Quill.registerModule('destroyable', Destroyable)

@quill.addModule('destroyable')

@listener = jasmine.createSpy('listener')
@quill.on('some event', @listener)

@quill.insertText(0, 'Hello world!')
htmlBeforeDestroying = @quill.getHTML()

@quill.destroy()
)

afterEach( ->
delete Quill.modules.destroyable
)

it('destroys the editor', ->
expect(@quill.editor.destroy).toHaveBeenCalled()
)

it('sets the container innerHTML with the current editor html', ->
expect(@quill.container.innerHTML).toEqual(htmlBeforeDestroying)
)

it('removes the editor from the global list', ->
expect(Quill.editors.indexOf(@quill)).toEqual(-1)
)

it('destroys all modules that have a destroy() method', ->
_.each(@quill.modules, (module, name) ->
if _.isFunction(module.destroy)
expect(module.destroy).toHaveBeenCalled()
)
)

it('removes all listeners', ->
@quill.emit('some event')
expect(@listener).not.toHaveBeenCalled()
)
)
)

0 comments on commit 049c176

Please sign in to comment.