From 2df5957f9b462fa73d3b950e319683cb685afecd Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 23 Sep 2014 15:41:44 -0600 Subject: [PATCH] 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. --- spec/command-registry-spec.coffee | 28 ++++++++++++++++++++++++++++ spec/spec-helper.coffee | 2 ++ src/command-registry.coffee | 11 +++++++++++ 3 files changed, 41 insertions(+) diff --git a/spec/command-registry-spec.coffee b/spec/command-registry-spec.coffee index 98c480375..03c0b4717 100644 --- a/spec/command-registry-spec.coffee +++ b/spec/command-registry-spec.coffee @@ -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'} + ] diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index c4f39bbb7..a5a31ac4b 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -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 = {} diff --git a/src/command-registry.coffee b/src/command-registry.coffee index 11dec2dca..fb05341f8 100644 --- a/src/command-registry.coffee +++ b/src/command-registry.coffee @@ -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