如何在atom包规范中与确认对话框交互



问题

我有哪些选项可以为涉及与原子编辑器确认对话框交互的代码编写规范?

背景

我正在为atom开发一个包,并有一个删除文件的命令,然后将更改推送到服务器。我想写一个测试来验证命令的行为,但我很难想出一个好的方法来模拟点击确认对话框上的取消/确定按钮

命令代码看起来像这个

atom.workspaceView.command "mavensmate:delete-file-from-server", =>
  # do setup stuff (build the params object)
  atom.confirm
    message: "You sure?"
    buttons:
      Cancel: => # nothing to do here, just let the window close
      Delete: => # run the delete handler
        @mm.run(params).then (result) =>
          @mmResponseHandler(params, result)

我似乎不知道如何让取消或删除回调在规范中运行。我一直在搜索所有的原子规范,并在谷歌上搜索,但似乎什么都没有出现。我曾希望将返回设置为我想要触发的回调的索引会起作用,但我的删除按钮回调永远不会被调用。

# Delete the metadata in the active pane from the server
describe 'Delete File from Server', ->
  filePath = ''
  beforeEach ->
    # set up the workspace with a fake apex class
    directory = temp.mkdirSync()
    atom.project.setPath(directory)
    filePath = path.join(directory, 'MyClass.cls')
    spyOn(mm, 'run').andCallThrough()
    waitsForPromise ->
      atom.workspace.open(filePath)
  it 'should invoke mavensmate:delete-file-from-server if confirmed', ->
    spyOn(atom, 'confirm').andReturn(1)
    atom.workspaceView.trigger 'mavensmate:delete-file-from-server'
    expect(mm.run).toHaveBeenCalled()

有没有更好的方法来模仿用户点击确认对话框上的按钮?有什么变通办法可以测试一下吗?

如果你用按钮传递回调,似乎没有一个好的方法来模拟与确认对话框的交互,但如果你只是传递一个数组,并让命令触发器对此做出响应,那么你可以根据需要编写规范。

命令代码看起来像这个

atom.workspaceView.command "mavensmate:delete-file-from-server", =>
  # do setup stuff (build the params object)
  atom.confirm
    message: "You sure?"
    buttons: ["Cancel", "Delete"]
  if answer == 1
    @mm.run(params).then (result) =>
      @mmResponseHandler(params, result)

该规范将适用于当前版本的

# Delete the metadata in the active pane from the server
describe 'Delete File from Server', ->
  filePath = ''
  beforeEach ->
    # set up the workspace with a fake apex class
    directory = temp.mkdirSync()
    atom.project.setPath(directory)
    filePath = path.join(directory, 'MyClass.cls')
    spyOn(mm, 'run').andCallThrough()
    waitsForPromise ->
      atom.workspace.open(filePath)
  it 'should invoke mavensmate:delete-file-from-server if confirmed', ->
    spyOn(atom, 'confirm').andReturn(1)
    atom.workspaceView.trigger 'mavensmate:delete-file-from-server'
    expect(mm.run).toHaveBeenCalled()

最新更新