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

Commit

Permalink
test: add tests for remove-permanently
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Apr 9, 2021
1 parent a23facd commit c868589
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
32 changes: 32 additions & 0 deletions spec/async-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Spy on an async method and call through
* @param obj {Record<string, Function> | any}
* @param method {string}
* @returns {Jasmine.Spy & {resolvedWith: any, calledWith: Array<any>}}
*/
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
49 changes: 49 additions & 0 deletions spec/tree-view-package-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ->
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit c868589

Please sign in to comment.