Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 18729a3

Browse files
committed
test: add tests for remove-permanently
1 parent a23facd commit 18729a3

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

spec/async-helper.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Spy on an async method and call through
3+
* @param obj {Record<string, Function> | any}
4+
* @param method {string}
5+
* @returns {Jasmine.Spy & {resolvedWith: any, calledWith: Array<any>}}
6+
*/
7+
function spyOnAsyncAndCallThrough(obj, method) {
8+
const originalMethod = obj[method]
9+
if (typeof originalMethod !== "function") {
10+
throw new Error(`${method} is not a method of ${obj}`)
11+
}
12+
let resolvedWith = undefined
13+
let calledWith = undefined
14+
let asyncSpy = spyOn(obj, method)
15+
.andCallFake((...args) => {
16+
calledWith = args
17+
originalMethod(...args)
18+
.then((returnValue) => {
19+
resolvedWith = returnValue
20+
// update spy call information
21+
asyncSpy.resolvedWith = resolvedWith
22+
asyncSpy.calledWith = calledWith
23+
}).catch((err) => {
24+
throw err
25+
})
26+
})
27+
// initial undefined values
28+
asyncSpy.resolvedWith = resolvedWith
29+
asyncSpy.calledWith = calledWith
30+
return asyncSpy
31+
}
32+
exports.spyOnAsyncAndCallThrough = spyOnAsyncAndCallThrough

spec/tree-view-package-spec.coffee

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ os = require 'os'
66
{remote, shell} = require 'electron'
77
Directory = require '../lib/directory'
88
eventHelpers = require "./event-helpers"
9+
spyOnAsyncAndCallThrough = require('./async-helper').spyOnAsyncAndCallThrough
910

1011
isCaseSensitive = null
1112
isFilesystemCaseSensitive = ->
@@ -3133,6 +3134,54 @@ describe "TreeView", ->
31333134
runs ->
31343135
expect(atom.notifications.getNotifications().length).toBe 0
31353136

3137+
describe "treev-view:remove-permanently", ->
3138+
beforeEach ->
3139+
jasmine.attachToDOM(workspaceElement)
3140+
3141+
it "won't remove the root directory", ->
3142+
spyOn(atom, 'confirm')
3143+
treeView.focus()
3144+
root1.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
3145+
atom.commands.dispatch(treeView.element, 'tree-view:remove-permanently')
3146+
3147+
args = atom.confirm.mostRecentCall.args[0]
3148+
expect(args.buttons).toEqual ['OK']
3149+
3150+
it "shows the native alert dialog", ->
3151+
spyOn(atom, 'confirm')
3152+
3153+
waitForWorkspaceOpenEvent ->
3154+
fileView.dispatchEvent(new MouseEvent('click', {bubbles: true, detail: 1}))
3155+
3156+
runs ->
3157+
atom.commands.dispatch(treeView.element, 'tree-view:remove-permanently')
3158+
args = atom.confirm.mostRecentCall.args[0]
3159+
expect(args.buttons).toEqual ['Permanently Delete ⚠️', 'Cancel']
3160+
3161+
3162+
it "calls removeSelectedEntries and removeSelectedPathsPermanently", ->
3163+
spyOn(atom, 'confirm')
3164+
3165+
removeSelectedPathsPermanentlySpy = spyOnAsyncAndCallThrough(treeView, 'removeSelectedPathsPermanently')
3166+
removeSelectedEntriesSpy = spyOn(treeView, 'removeSelectedEntries').andCallThrough()
3167+
3168+
filePath = path.join(os.tmpdir(), 'non-project-file.txt')
3169+
fs.writeFileSync(filePath, 'test')
3170+
3171+
waitsForPromise ->
3172+
atom.workspace.open(filePath)
3173+
3174+
waitsForPromise ->
3175+
atom.commands.dispatch(treeView.element, 'tree-view:remove-permanently')
3176+
3177+
waitsFor ->
3178+
removeSelectedPathsPermanentlySpy.calledWith isnt undefined
3179+
3180+
waitsFor 'removeSelectedEntries amd removeSelectedPathsPermanently to be called', ->
3181+
removeSelectedEntriesSpy.callCount is 1 and
3182+
removeSelectedEntriesSpy.mostRecentCall.args[0] is true and
3183+
removeSelectedPathsPermanentlySpy.calledWith[0] is [filePath]
3184+
31363185
describe "file system events", ->
31373186
temporaryFilePath = null
31383187

0 commit comments

Comments
 (0)