如何与原子包规范中的确认对话框进行交互?

时间:2014-07-29 20:21:35

标签: coffeescript tdd jasmine jasmine-node atom-editor

问题

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

背景

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

命令代码如下所示

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()

有没有更好的方法来模仿用户点击确认对话框上的按钮?是否有任何变通方法可以进行测试?

1 个答案:

答案 0 :(得分:0)

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

命令代码将如下所示

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()