const {it, fit, ffit, afterEach, beforeEach} = require('./async-spec-helpers') // eslint-disable-line no-unused-vars

describe('BackgroundTips', () => {
  let workspaceElement

  const activatePackage = async () => {
    const {mainModule} = await atom.packages.activatePackage('background-tips')
    return mainModule.backgroundTipsView
  }

  beforeEach(() => {
    workspaceElement = atom.views.getView(atom.workspace)
    jasmine.attachToDOM(workspaceElement)
    jasmine.useMockClock()
    spyOn(atom.getCurrentWindow(), 'isFocused').andReturn(true)
  })

  describe('when the package is activated when there is only one pane', () => {
    beforeEach(() => {
      expect(atom.workspace.getCenter().getPanes().length).toBe(1)
    })

    describe('when the pane is empty', () => {
      it('attaches the view after a delay', async () => {
        expect(atom.workspace.getActivePane().getItems().length).toBe(0)

        const backgroundTipsView = await activatePackage()
        expect(backgroundTipsView.element.parentNode).toBeFalsy()
        advanceClock(backgroundTipsView.startDelay + 1)
        expect(backgroundTipsView.element.parentNode).toBeTruthy()
      })
    })

    describe('when the pane is not empty', () => {
      it('does not attach the view', async () => {
        await atom.workspace.open()

        const backgroundTipsView = await activatePackage()
        advanceClock(backgroundTipsView.startDelay + 1)
        expect(backgroundTipsView.element.parentNode).toBeFalsy()
      })
    })

    describe('when a second pane is created', () => {
      it('detaches the view', async () => {
        const backgroundTipsView = await activatePackage()
        advanceClock(backgroundTipsView.startDelay + 1)
        expect(backgroundTipsView.element.parentNode).toBeTruthy()

        atom.workspace.getActivePane().splitRight()
        expect(backgroundTipsView.element.parentNode).toBeFalsy()
      })
    })
  })

  describe('when the package is activated when there are multiple panes', () => {
    beforeEach(() => {
      atom.workspace.getActivePane().splitRight()
      expect(atom.workspace.getCenter().getPanes().length).toBe(2)
    })

    it('does not attach the view', async () => {
      const backgroundTipsView = await activatePackage()
      advanceClock(backgroundTipsView.startDelay + 1)
      expect(backgroundTipsView.element.parentNode).toBeFalsy()
    })

    describe('when all but the last pane is destroyed', () => {
      it('attaches the view', async () => {
        const backgroundTipsView = await activatePackage()
        atom.workspace.getActivePane().destroy()
        advanceClock(backgroundTipsView.startDelay + 1)
        expect(backgroundTipsView.element.parentNode).toBeTruthy()

        atom.workspace.getActivePane().splitRight()
        expect(backgroundTipsView.element.parentNode).toBeFalsy()

        atom.workspace.getActivePane().destroy()
        expect(backgroundTipsView.element.parentNode).toBeTruthy()
      })
    })
  })

  describe('when the view is attached', () => {
    let backgroundTipsView

    beforeEach(async () => {
      expect(atom.workspace.getCenter().getPanes().length).toBe(1)

      backgroundTipsView = await activatePackage()
      advanceClock(backgroundTipsView.startDelay)
      advanceClock(backgroundTipsView.fadeDuration)
    })

    it('has text in the message', () => {
      expect(backgroundTipsView.element.parentNode).toBeTruthy()
      expect(backgroundTipsView.message.textContent).toBeTruthy()
    })

    it('changes text in the message', async () => {
      const oldText = backgroundTipsView.message.textContent
      advanceClock(backgroundTipsView.displayDuration)
      advanceClock(backgroundTipsView.fadeDuration)
      expect(backgroundTipsView.message.textContent).not.toEqual(oldText)
    })
  })
})

@@ -36,16 +36,37 @@
 foreEach
+, emitterEventPromise
 %7D = requ
@@ -3834,12 +3834,1446 @@
  %7D)%0A  %7D)
+%0A%0A  describe('when Atom is not focused but all other requirements are satisfied', () =%3E %7B%0A    beforeEach(() =%3E %7B%0A      jasmine.unspy(atom.getCurrentWindow(), 'isFocused')%0A      spyOn(atom.getCurrentWindow(), 'isFocused').andReturn(false)%0A    %7D)%0A%0A    it('does not display the background tips', async () =%3E %7B%0A      expect(atom.workspace.getActivePane().getItems().length).toBe(0)%0A%0A      const backgroundTipsView = await activatePackage()%0A      expect(backgroundTipsView.element.parentNode).toBeFalsy()%0A      advanceClock(backgroundTipsView.startDelay + 1)%0A      expect(backgroundTipsView.element.parentNode).toBeFalsy()%0A    %7D)%0A%0A    it('reactivates the background tips if the focus event is received', async () =%3E %7B%0A      expect(atom.workspace.getActivePane().getItems().length).toBe(0)%0A%0A      const backgroundTipsView = await activatePackage()%0A      advanceClock(backgroundTipsView.startDelay + 1)%0A      expect(backgroundTipsView.element.parentNode).toBeFalsy()%0A%0A      jasmine.unspy(atom.getCurrentWindow(), 'isFocused')%0A      spyOn(atom.getCurrentWindow(), 'isFocused').andReturn(true)%0A%0A      const focusEvent = emitterEventPromise(atom.getCurrentWindow(), 'focus')%0A      atom.getCurrentWindow().emit('focus') // Manually emit to prevent actually blurring + refocusing the window%0A%0A      await focusEvent%0A%0A      advanceClock(backgroundTipsView.startDelay + 1)%0A      expect(backgroundTipsView.element.parentNode).toBeTruthy()%0A    %7D)%0A  %7D)
 %0A%7D)%0A

Add focus-related tests