<commit_before>$ = require 'jquery'
{$$} = require 'space-pen'
fsUtils = require 'fs-utils'
path = require 'path'
{less} = require 'less'
WindowEventHandler = require 'window-event-handler'

describe "Window", ->
  [projectPath, windowEventHandler] = []

  beforeEach ->
    atom.getLoadSettings().initialPath = project.getPath()
    windowEventHandler = new WindowEventHandler()
    window.deserializeEditorWindow()
    projectPath = project.getPath()

  afterEach ->
    windowEventHandler.unsubscribe()
    $(window).off 'beforeunload'

  describe "when the window is loaded", ->
    it "doesn't have .is-blurred on the body tag", ->
      expect($("body")).not.toHaveClass("is-blurred")

  describe "when the window is blurred", ->
    beforeEach ->
      $(window).trigger 'blur'

    afterEach ->
      $('body').removeClass('is-blurred')

    it "adds the .is-blurred class on the body", ->
      expect($("body")).toHaveClass("is-blurred")

    describe "when the window is focused again", ->
      it "removes the .is-blurred class from the body", ->
        $(window).trigger 'focus'
        expect($("body")).not.toHaveClass("is-blurred")

  describe "window:close event", ->
    describe "when no pane items are modified", ->
      it "calls window.closeWithoutConfirm", ->
        spyOn window, 'closeWithoutConfirm'
        $(window).trigger 'window:close'
        expect(window.closeWithoutConfirm).toHaveBeenCalled()

    describe "when pane items are are modified", ->
      it "prompts user to save and and calls window.closeWithoutConfirm", ->
        spyOn(window, 'closeWithoutConfirm')
        spyOn(atom, "confirmSync").andReturn(2)
        editSession = rootView.open("sample.js")
        editSession.insertText("I look different, I feel different.")
        $(window).trigger 'window:close'
        expect(window.closeWithoutConfirm).toHaveBeenCalled()
        expect(atom.confirmSync).toHaveBeenCalled()

      it "prompts user to save and aborts if dialog is canceled", ->
        spyOn(window, 'closeWithoutConfirm')
        spyOn(atom, "confirmSync").andReturn(1)
        editSession = rootView.open("sample.js")
        editSession.insertText("I look different, I feel different.")
        $(window).trigger 'window:close'
        expect(window.closeWithoutConfirm).not.toHaveBeenCalled()
        expect(atom.confirmSync).toHaveBeenCalled()

  describe "requireStylesheet(path)", ->
    it "synchronously loads css at the given path and installs a style tag for it in the head", ->
      cssPath = project.resolve('css.css')
      lengthBefore = $('head style').length

      requireStylesheet(cssPath)
      expect($('head style').length).toBe lengthBefore + 1

      element = $('head style[id*="css.css"]')
      expect(element.attr('id')).toBe cssPath<commit_msg>Fix specs regarding beforeunload event.<commit_after>$ = require 'jquery'
{$$} = require 'space-pen'
fsUtils = require 'fs-utils'
path = require 'path'
{less} = require 'less'
WindowEventHandler = require 'window-event-handler'

describe "Window", ->
  [projectPath, windowEventHandler] = []

  beforeEach ->
    atom.getLoadSettings().initialPath = project.getPath()
    windowEventHandler = new WindowEventHandler()
    window.deserializeEditorWindow()
    projectPath = project.getPath()

  afterEach ->
    windowEventHandler.unsubscribe()
    $(window).off 'beforeunload'

  describe "when the window is loaded", ->
    it "doesn't have .is-blurred on the body tag", ->
      expect($("body")).not.toHaveClass("is-blurred")

  describe "when the window is blurred", ->
    beforeEach ->
      $(window).trigger 'blur'

    afterEach ->
      $('body').removeClass('is-blurred')

    it "adds the .is-blurred class on the body", ->
      expect($("body")).toHaveClass("is-blurred")

    describe "when the window is focused again", ->
      it "removes the .is-blurred class from the body", ->
        $(window).trigger 'focus'
        expect($("body")).not.toHaveClass("is-blurred")

  describe "window:close event", ->
    it "closes the window", ->
      spyOn(window, 'close')
      $(window).trigger 'window:close'
      expect(window.close).toHaveBeenCalled()

    it "emits the beforeunload event", ->
      $(window).off 'beforeunload'
      beforeunload = jasmine.createSpy('beforeunload').andReturn(false)
      $(window).on 'beforeunload', beforeunload

      $(window).trigger 'window:close'
      expect(beforeunload).toHaveBeenCalled()

  describe "beforeunload event", ->
    describe "when pane items are are modified", ->
      it "prompts user to save and and calls rootView.confirmClose", ->
        spyOn(rootView, 'confirmClose').andCallThrough()
        spyOn(atom, "confirmSync").andReturn(2)
        editSession = rootView.open("sample.js")
        editSession.insertText("I look different, I feel different.")
        $(window).trigger 'beforeunload'
        expect(rootView.confirmClose).toHaveBeenCalled()
        expect(atom.confirmSync).toHaveBeenCalled()

      it "prompts user to save and handler returns true if don't save", ->
        spyOn(atom, "confirmSync").andReturn(2)
        editSession = rootView.open("sample.js")
        editSession.insertText("I look different, I feel different.")
        expect(window.onbeforeunload(new Event('beforeunload'))).toBeTruthy()
        expect(atom.confirmSync).toHaveBeenCalled()

      it "prompts user to save and handler returns false if dialog is canceled", ->
        spyOn(atom, "confirmSync").andReturn(1)
        editSession = rootView.open("sample.js")
        editSession.insertText("I look different, I feel different.")
        expect(window.onbeforeunload(new Event('beforeunload'))).toBeFalsy()
        expect(atom.confirmSync).toHaveBeenCalled()

  describe "requireStylesheet(path)", ->
    it "synchronously loads css at the given path and installs a style tag for it in the head", ->
      cssPath = project.resolve('css.css')
      lengthBefore = $('head style').length

      requireStylesheet(cssPath)
      expect($('head style').length).toBe lengthBefore + 1

      element = $('head style[id*="css.css"]')
      expect(element.attr('id')).toBe cssPath
{'fraction_non_alphanumeric': 0.1368215794605135, 'fraction_numerical': 0.0011374715632109197, 'mean_word_length': 3.418521177315147, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1851679', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1751, 'n_words': 438}