Workspace spec


I’m writing some specs in order to test the behaviour of a button placed in the status bar.
My button is placed in the status bar, and its label should change accordingly to some package configuration. Plus, when I click the button the label should change as well as the configuration value.

What I tried was:

{$} = require('atom-space-pen-views')

describe 'MyPackageView', ->

  envButton = null

  beforeEach ->
    workspaceElement = atom.views.getView(atom.workspace)
    waitsForPromise ->
      atom.packages.activatePackage 'status-bar'

    waitsForPromise ->
      atom.packages.activatePackage 'my-package'

    runs ->
      envButton = workspaceElement.querySelector('.my-class')

  describe 'when env button is added to status bar', ->

    it 'should properly show the working environment', ->
      expect(envButton).not.toBe null
      expect(envButton.innerText).toEqual 'Initial value'

    it 'should properly switch environment', ->
      expect(envButton.innerText).toEqual 'Changed value'
      expect(atom.config.get 'my-package.env').toEqual 'changedValue'

What happens here is, the first test passes successfully, while the second test fails because of envButton, being it null. I could easily move the code of the second test in the first one, but I would prefer to keep them separate since they test different features, and I would really like to understand what’s wrong with it.

Cheers guys!

  1. I would put atom.packages.emitter.emit('did-activate-all') first in the runs block. If your package waits for all packages to activate to do something … then I would expect that the workspaceElement.querySelector to fail sometimes or all the time.
  2. I would include jasmine.attachToDOM(workspaceElement) in the runs block before the workspaceElement.querySelector call also. Any time I’m going to call DOM methods like querySelector or click in my tests then I would expect to have a “real” DOM in place.


Yeah, I tried that before, taking inspiration from some tests in the status-bar and settings packages.
Even with that, no luck.

runs ->
  envButton = workspaceElement.querySelector('.my-class')

I also tried to deactivate the packages in an afterEach block, I thought that maybe since the packages are loaded already, there was some sort of race condition. Again, no luck :frowning:


Funny enough, if I try to access the button with $ in the second test, and it’s not null. So,

expect($('.sw-env-button')).not.toBe null

succeeds, even if

expect($('.sw-env-button').text()).toEqual ' SW on production environment'
expect(atom.config.get 'statwolf-atom-configuration.env').toEqual 'production'

fail because the text is null. Whaaat is going on here??


Still no clue on this.
May it be helpful, I include here the main package class.

ModuleNameView = require './module-name-view'

module.exports = ModuleName =

  moduleNameView: null

    [really big bunch of config here]

  activate: (state) ->
    @moduleNameView = new ModuleNameView state.moduleNameViewState

  deactivate: ->

  consumeStatusBar: (statusBar) ->
    @moduleNameView.attach statusBar

  # this is a provided service
  getConfigItem: ->
    configService =
      get: (key) ->
        [play around with configuration]
    return configService