Restore commands after each spec

This commit adds the ability to get and restore snapshots of command
listeners. Whatever commands are installed before specs begin are
preserved, but commands added during specs are always cleared away.
This commit is contained in:
Nathan Sobo
2014-09-23 15:41:44 -06:00
parent a7196ec906
commit 2df5957f9b
3 changed files with 41 additions and 0 deletions

View File

@@ -145,3 +145,31 @@ describe "CommandRegistry", ->
expect(registry.dispatch(grandchild, 'command')).toBe true
expect(registry.dispatch(grandchild, 'bogus')).toBe false
expect(registry.dispatch(parent, 'command')).toBe false
describe "::getSnapshot and ::restoreSnapshot", ->
it "removes all command handlers except for those in the snapshot", ->
registry.add '.parent', 'namespace:command-1', ->
registry.add '.child', 'namespace:command-2', ->
snapshot = registry.getSnapshot()
registry.add '.grandchild', 'namespace:command-3', ->
expect(registry.findCommands(target: grandchild)[0..2]).toEqual [
{name: 'namespace:command-3', displayName: 'Namespace: Command 3'}
{name: 'namespace:command-2', displayName: 'Namespace: Command 2'}
{name: 'namespace:command-1', displayName: 'Namespace: Command 1'}
]
registry.restoreSnapshot(snapshot)
expect(registry.findCommands(target: grandchild)[0..1]).toEqual [
{name: 'namespace:command-2', displayName: 'Namespace: Command 2'}
{name: 'namespace:command-1', displayName: 'Namespace: Command 1'}
]
registry.add '.grandchild', 'namespace:command-3', ->
registry.restoreSnapshot(snapshot)
expect(registry.findCommands(target: grandchild)[0..1]).toEqual [
{name: 'namespace:command-2', displayName: 'Namespace: Command 2'}
{name: 'namespace:command-1', displayName: 'Namespace: Command 1'}
]

View File

@@ -27,6 +27,7 @@ fixturePackagesPath = path.resolve(__dirname, './fixtures/packages')
atom.packages.packageDirPaths.unshift(fixturePackagesPath)
atom.keymaps.loadBundledKeymaps()
keyBindingsToRestore = atom.keymaps.getKeyBindings()
commandsToRestore = atom.commands.getSnapshot()
$(window).on 'core:close', -> window.close()
$(window).on 'beforeunload', ->
@@ -65,6 +66,7 @@ beforeEach ->
atom.workspace = new Workspace()
atom.keymaps.keyBindings = _.clone(keyBindingsToRestore)
atom.commands.setRootNode(document.body)
atom.commands.restoreSnapshot(commandsToRestore)
window.resetTimeouts()
atom.packages.packageStates = {}

View File

@@ -46,6 +46,8 @@ class CommandRegistry
constructor: (@rootNode) ->
@listenersByCommandName = {}
getRootNode: -> @rootNode
setRootNode: (newRootNode) ->
oldRootNode = @rootNode
@rootNode = newRootNode
@@ -149,6 +151,15 @@ class CommandRegistry
eventWithTarget = Object.create(event, target: value: target)
@handleCommandEvent(eventWithTarget)
getSnapshot: ->
_.deepClone(@listenersByCommandName)
restoreSnapshot: (snapshot) ->
rootNode = @getRootNode()
@setRootNode(null) # clear listeners for current commands
@listenersByCommandName = _.deepClone(snapshot)
@setRootNode(rootNode) # restore listeners for commands in snapshot
handleCommandEvent: (event) =>
propagationStopped = false
immediatePropagationStopped = false