开发插件写测试时无法正确获得ActiveTextEditor


#1

学着写插件,写测试的时候,我打开了一个文件,并且返回我了一个resolve的promise,但是我通过传递的TextEditor.getText() 方法没有获取到任何字符,通过atom.workspace.getActiveTextEditor().getText() 也无法获取到任何字符。

下面是测试源码

'use babel';

import MyPackage3 from '../lib/my-package3';
// import fs from 'fs';

// Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
//
// To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit`
// or `fdescribe`). Remove the `f` to unfocus the block.

describe('MyPackage3', () => {
  let workspaceElement, activationPromise;
  let editor;

  beforeEach(() => {
    workspaceElement = atom.views.getView(atom.workspace);
    activationPromise = atom.workspace.open('./test.html');

  });

  describe('when the my-package3:beautify event is triggered', () => {
    it('can format the whole text this editor', () => {
      // Before the activation event the view is not on the DOM, and no panel
      // has been created

      // This is an activation event, triggering it will cause the package to be
      // activated.

      waitsForPromise(() => {
        return activationPromise.then(edit => editor=edit).catch(e=>{console.log(e)});
      });

      runs(() => {
        let text = atom.workspace.getActiveTextEditor().getText();
        
        console.log(atom.workspace.getActiveTextEditor())
        
        atom.commands.dispatch(workspaceElement, 'my-package3:beautify');

        expect(editor.getText()).not.toBe(text);
      });
    });
  });
});