From c86858928ad7e85a02c4f4e1553d0e049b66eb81 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Thu, 8 Apr 2021 20:21:08 -0500 Subject: [PATCH] test: add tests for remove-permanently --- spec/async-helper.js | 32 +++++++++++++++++++ spec/tree-view-package-spec.coffee | 49 ++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 spec/async-helper.js diff --git a/spec/async-helper.js b/spec/async-helper.js new file mode 100644 index 00000000..b0b8f21b --- /dev/null +++ b/spec/async-helper.js @@ -0,0 +1,32 @@ +/** + * Spy on an async method and call through + * @param obj {Record | any} + * @param method {string} + * @returns {Jasmine.Spy & {resolvedWith: any, calledWith: Array}} + */ +function spyOnAsyncAndCallThrough (obj, method) { + const originalMethod = obj[method] + if (typeof originalMethod !== 'function') { + throw new Error(`${method} is not a method of ${obj}`) + } + let resolvedWith + let calledWith + let asyncSpy = spyOn(obj, method) + .andCallFake((...args) => { + calledWith = args + originalMethod(...args) + .then((returnValue) => { + resolvedWith = returnValue + // update spy call information + asyncSpy.resolvedWith = resolvedWith + asyncSpy.calledWith = calledWith + }).catch((err) => { + throw err + }) + }) + // initial undefined values + asyncSpy.resolvedWith = resolvedWith + asyncSpy.calledWith = calledWith + return asyncSpy +} +exports.spyOnAsyncAndCallThrough = spyOnAsyncAndCallThrough diff --git a/spec/tree-view-package-spec.coffee b/spec/tree-view-package-spec.coffee index 85a2233d..24bb0cdf 100644 --- a/spec/tree-view-package-spec.coffee +++ b/spec/tree-view-package-spec.coffee @@ -6,6 +6,7 @@ os = require 'os' {remote, shell} = require 'electron' Directory = require '../lib/directory' eventHelpers = require "./event-helpers" +spyOnAsyncAndCallThrough = require('./async-helper').spyOnAsyncAndCallThrough isCaseSensitive = null isFilesystemCaseSensitive = -> @@ -3133,6 +3134,54 @@ describe "TreeView", -> runs -> expect(atom.notifications.getNotifications().length).toBe 0 + describe "treev-view:remove-permanently", -> + beforeEach -> + jasmine.attachToDOM(workspaceElement) + + it "won't remove the root directory", -> + spyOn(atom, 'confirm') + treeView.focus() + root1.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1})) + atom.commands.dispatch(treeView.element, 'tree-view:remove-permanently') + + args = atom.confirm.mostRecentCall.args[0] + expect(args.buttons).toEqual ['OK'] + + it "shows the native alert dialog", -> + spyOn(atom, 'confirm') + + waitForWorkspaceOpenEvent -> + fileView.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1})) + + runs -> + atom.commands.dispatch(treeView.element, 'tree-view:remove-permanently') + args = atom.confirm.mostRecentCall.args[0] + expect(args.buttons).toEqual ['Permanently Delete ⚠️', 'Cancel'] + + + it "calls removeSelectedEntries and removeSelectedPathsPermanently", -> + spyOn(atom, 'confirm') + + removeSelectedPathsPermanentlySpy = spyOnAsyncAndCallThrough(treeView, 'removeSelectedPathsPermanently') + removeSelectedEntriesSpy = spyOn(treeView, 'removeSelectedEntries').andCallThrough() + + filePath = path.join(os.tmpdir(), 'non-project-file.txt') + fs.writeFileSync(filePath, 'test') + + waitsForPromise -> + atom.workspace.open(filePath) + + waitsForPromise -> + atom.commands.dispatch(treeView.element, 'tree-view:remove-permanently') + + waitsFor -> + removeSelectedPathsPermanentlySpy.calledWith isnt undefined + + waitsFor 'removeSelectedEntries amd removeSelectedPathsPermanently to be called', -> + removeSelectedEntriesSpy.callCount is 1 and + removeSelectedEntriesSpy.mostRecentCall.args[0] is true and + removeSelectedPathsPermanentlySpy.calledWith[0] is [filePath] + describe "file system events", -> temporaryFilePath = null