<commit_before>path = require 'path'
{$$} = require 'atom-space-pen-views'
SettingsView = require '../lib/settings-view'

describe "SettingsView", ->
  settingsView = null

  beforeEach ->
    settingsView = new SettingsView
    spyOn(settingsView, "initializePanels").andCallThrough()
    window.advanceClock(10000)
    waitsFor ->
      settingsView.initializePanels.callCount > 0

  describe "serialization", ->
    it "remembers which panel was visible", ->
      settingsView.showPanel('Themes')
      newSettingsView = new SettingsView(settingsView.serialize())
      settingsView.remove()
      jasmine.attachToDOM(newSettingsView.element)
      newSettingsView.initializePanels()
      expect(newSettingsView.activePanelName).toBe 'Themes'

    it "shows the previously active panel if it is added after deserialization", ->
      settingsView.addCorePanel('Panel 1', 'panel1', -> $$ -> @div id: 'panel-1')
      settingsView.showPanel('Panel 1')
      newSettingsView = new SettingsView(settingsView.serialize())
      newSettingsView.addPanel('Panel 1', 'panel1', -> $$ -> @div id: 'panel-1')
      newSettingsView.initializePanels()
      jasmine.attachToDOM(newSettingsView.element)
      expect(newSettingsView.activePanelName).toBe 'Panel 1'

    it "shows the Settings panel if the last saved active panel name no longer exists", ->
      settingsView.addCorePanel('Panel 1', 'panel1', -> $$ -> @div id: 'panel-1')
      settingsView.showPanel('Panel 1')
      newSettingsView = new SettingsView(settingsView.serialize())
      settingsView.remove()
      jasmine.attachToDOM(newSettingsView.element)
      newSettingsView.initializePanels()
      expect(newSettingsView.activePanelName).toBe 'Settings'

    it "serializes the active panel name even when the panels were never initialized", ->
      settingsView.showPanel('Themes')
      settingsView2 = new SettingsView(settingsView.serialize())
      settingsView3 = new SettingsView(settingsView2.serialize())
      jasmine.attachToDOM(settingsView3.element)
      settingsView3.initializePanels()
      expect(settingsView3.activePanelName).toBe 'Themes'

  describe ".addCorePanel(name, iconName, view)", ->
    it "adds a menu entry to the left and a panel that can be activated by clicking it", ->
      settingsView.addCorePanel('Panel 1', 'panel1', -> $$ -> @div id: 'panel-1')
      settingsView.addCorePanel('Panel 2', 'panel2', -> $$ -> @div id: 'panel-2')

      expect(settingsView.panelMenu.find('li a:contains(Panel 1)')).toExist()
      expect(settingsView.panelMenu.find('li a:contains(Panel 2)')).toExist()
      expect(settingsView.panelMenu.children(':first')).toHaveClass 'active'

      jasmine.attachToDOM(settingsView.element)
      settingsView.panelMenu.find('li a:contains(Panel 1)').click()
      expect(settingsView.panelMenu.children('.active').length).toBe 1
      expect(settingsView.panelMenu.find('li:contains(Panel 1)')).toHaveClass('active')
      expect(settingsView.panels.find('#panel-1')).toBeVisible()
      expect(settingsView.panels.find('#panel-2')).not.toExist()
      settingsView.panelMenu.find('li a:contains(Panel 2)').click()
      expect(settingsView.panelMenu.children('.active').length).toBe 1
      expect(settingsView.panelMenu.find('li:contains(Panel 2)')).toHaveClass('active')
      expect(settingsView.panels.find('#panel-1')).toBeHidden()
      expect(settingsView.panels.find('#panel-2')).toBeVisible()

  describe "when the package is activated", ->
    [mainModule] = []
    beforeEach ->
      jasmine.attachToDOM(atom.views.getView(atom.workspace))
      waitsForPromise ->
        atom.packages.activatePackage('settings-view')

    describe "when the settings view is opened with a settings-view:* command", ->
      openWithCommand = (command) ->
        atom.commands.dispatch(atom.views.getView(atom.workspace), command)
        waitsFor ->
          atom.workspace.getActivePaneItem()?

      beforeEach ->
        settingsView = null

      describe "settings-view:open", ->
        it "opens the settings view", ->
          openWithCommand('settings-view:open')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Settings'

      describe "settings-view:show-keybindings", ->
        it "opens the settings view to the keybindings page", ->
          openWithCommand('settings-view:show-keybindings')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Keybindings'

      describe "settings-view:change-themes", ->
        it "opens the settings view to the themes page", ->
          openWithCommand('settings-view:change-themes')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Themes'

      describe "settings-view:uninstall-themes", ->
        it "opens the settings view to the themes page", ->
          openWithCommand('settings-view:uninstall-themes')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Themes'

      describe "settings-view:uninstall-packages", ->
        it "opens the settings view to the install page", ->
          openWithCommand('settings-view:uninstall-packages')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Packages'

      describe "settings-view:install-packages-and-themes", ->
        it "opens the settings view to the install page", ->
          openWithCommand('settings-view:install-packages-and-themes')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Install'

      describe "settings-view:check-for-package-updates", ->
        it "opens the settings view to the install page", ->
          openWithCommand('settings-view:check-for-package-updates')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Updates'

    describe "when atom.workspace.open() is used with a config URI", ->
      beforeEach ->
        settingsView = null

      it "opens the settings to the correct panel with atom://config/<panel-name>", ->
        waitsForPromise ->
          atom.workspace.open('atom://config').then (s) -> settingsView = s

        waits 1
        runs ->
          expect(settingsView.activePanelName).toBe 'Settings'

        waitsForPromise ->
          atom.workspace.open('atom://config/themes').then (s) -> settingsView = s

        waits 1
        runs ->
          expect(settingsView.activePanelName).toBe 'Themes'

        waitsForPromise ->
          atom.workspace.open('atom://config/install').then (s) -> settingsView = s

        waits 1
        runs ->
          expect(settingsView.activePanelName).toBe 'Install'

  describe "when an installed package is clicked from the Install panel", ->
    it "displays the package details", ->
      waitsFor ->
        atom.packages.activatePackage('settings-view')

      runs ->
        settingsView.packageManager.getClient()
        spyOn(settingsView.packageManager.client, 'featuredPackages').andCallFake (callback) ->
          callback(null, [{name: 'settings-view'}])
        settingsView.showPanel('Install')

      waitsFor ->
        settingsView.find('.package-card:not(.hidden)').length > 0

      runs ->
        settingsView.find('.package-card:not(.hidden):first').click()

        packageDetail = settingsView.find('.package-detail').view()
        expect(packageDetail.title.text()).toBe 'Settings View'

  describe "when the active theme has settings", ->
    panel = null

    beforeEach ->
      atom.packages.packageDirPaths.push(path.join(__dirname, 'fixtures'))
      atom.packages.loadPackage('ui-theme-with-config')
      atom.packages.loadPackage('syntax-theme-with-config')
      atom.config.set('core.themes', ['ui-theme-with-config', 'syntax-theme-with-config'])

      reloadedHandler = jasmine.createSpy('reloadedHandler')
      atom.themes.onDidChangeActiveThemes(reloadedHandler)
      atom.themes.activatePackages()

      waitsFor "themes to be reloaded", ->
        reloadedHandler.callCount is 1

      runs ->
        settingsView.showPanel('Themes')
        panel = settingsView.find('.themes-panel').view()

    describe "when the UI theme's settings button is clicked", ->
      it "navigates to that theme's detail view", ->
        jasmine.attachToDOM(settingsView.element)
        expect(panel.currentUiThemeSettings).toBeVisible()

        panel.currentUiThemeSettings.click()
        packageDetail = settingsView.find('.package-detail').view()
        expect(packageDetail.title.text()).toBe 'Ui Theme With Config'

    describe "when the syntax theme's settings button is clicked", ->
      it "navigates to that theme's detail view", ->
        jasmine.attachToDOM(settingsView.element)
        expect(panel.currentSyntaxThemeSettings).toBeVisible()

        panel.currentSyntaxThemeSettings.click()
        packageDetail = settingsView.find('.package-detail').view()
        expect(packageDetail.title.text()).toBe 'Syntax Theme With Config'<commit_msg>Unwatch user stylesheet in an afterEach<commit_after>path = require 'path'
{$$} = require 'atom-space-pen-views'
SettingsView = require '../lib/settings-view'

describe "SettingsView", ->
  settingsView = null

  beforeEach ->
    settingsView = new SettingsView
    spyOn(settingsView, "initializePanels").andCallThrough()
    window.advanceClock(10000)
    waitsFor ->
      settingsView.initializePanels.callCount > 0

  describe "serialization", ->
    it "remembers which panel was visible", ->
      settingsView.showPanel('Themes')
      newSettingsView = new SettingsView(settingsView.serialize())
      settingsView.remove()
      jasmine.attachToDOM(newSettingsView.element)
      newSettingsView.initializePanels()
      expect(newSettingsView.activePanelName).toBe 'Themes'

    it "shows the previously active panel if it is added after deserialization", ->
      settingsView.addCorePanel('Panel 1', 'panel1', -> $$ -> @div id: 'panel-1')
      settingsView.showPanel('Panel 1')
      newSettingsView = new SettingsView(settingsView.serialize())
      newSettingsView.addPanel('Panel 1', 'panel1', -> $$ -> @div id: 'panel-1')
      newSettingsView.initializePanels()
      jasmine.attachToDOM(newSettingsView.element)
      expect(newSettingsView.activePanelName).toBe 'Panel 1'

    it "shows the Settings panel if the last saved active panel name no longer exists", ->
      settingsView.addCorePanel('Panel 1', 'panel1', -> $$ -> @div id: 'panel-1')
      settingsView.showPanel('Panel 1')
      newSettingsView = new SettingsView(settingsView.serialize())
      settingsView.remove()
      jasmine.attachToDOM(newSettingsView.element)
      newSettingsView.initializePanels()
      expect(newSettingsView.activePanelName).toBe 'Settings'

    it "serializes the active panel name even when the panels were never initialized", ->
      settingsView.showPanel('Themes')
      settingsView2 = new SettingsView(settingsView.serialize())
      settingsView3 = new SettingsView(settingsView2.serialize())
      jasmine.attachToDOM(settingsView3.element)
      settingsView3.initializePanels()
      expect(settingsView3.activePanelName).toBe 'Themes'

  describe ".addCorePanel(name, iconName, view)", ->
    it "adds a menu entry to the left and a panel that can be activated by clicking it", ->
      settingsView.addCorePanel('Panel 1', 'panel1', -> $$ -> @div id: 'panel-1')
      settingsView.addCorePanel('Panel 2', 'panel2', -> $$ -> @div id: 'panel-2')

      expect(settingsView.panelMenu.find('li a:contains(Panel 1)')).toExist()
      expect(settingsView.panelMenu.find('li a:contains(Panel 2)')).toExist()
      expect(settingsView.panelMenu.children(':first')).toHaveClass 'active'

      jasmine.attachToDOM(settingsView.element)
      settingsView.panelMenu.find('li a:contains(Panel 1)').click()
      expect(settingsView.panelMenu.children('.active').length).toBe 1
      expect(settingsView.panelMenu.find('li:contains(Panel 1)')).toHaveClass('active')
      expect(settingsView.panels.find('#panel-1')).toBeVisible()
      expect(settingsView.panels.find('#panel-2')).not.toExist()
      settingsView.panelMenu.find('li a:contains(Panel 2)').click()
      expect(settingsView.panelMenu.children('.active').length).toBe 1
      expect(settingsView.panelMenu.find('li:contains(Panel 2)')).toHaveClass('active')
      expect(settingsView.panels.find('#panel-1')).toBeHidden()
      expect(settingsView.panels.find('#panel-2')).toBeVisible()

  describe "when the package is activated", ->
    [mainModule] = []
    beforeEach ->
      jasmine.attachToDOM(atom.views.getView(atom.workspace))
      waitsForPromise ->
        atom.packages.activatePackage('settings-view')

    describe "when the settings view is opened with a settings-view:* command", ->
      openWithCommand = (command) ->
        atom.commands.dispatch(atom.views.getView(atom.workspace), command)
        waitsFor ->
          atom.workspace.getActivePaneItem()?

      beforeEach ->
        settingsView = null

      describe "settings-view:open", ->
        it "opens the settings view", ->
          openWithCommand('settings-view:open')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Settings'

      describe "settings-view:show-keybindings", ->
        it "opens the settings view to the keybindings page", ->
          openWithCommand('settings-view:show-keybindings')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Keybindings'

      describe "settings-view:change-themes", ->
        it "opens the settings view to the themes page", ->
          openWithCommand('settings-view:change-themes')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Themes'

      describe "settings-view:uninstall-themes", ->
        it "opens the settings view to the themes page", ->
          openWithCommand('settings-view:uninstall-themes')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Themes'

      describe "settings-view:uninstall-packages", ->
        it "opens the settings view to the install page", ->
          openWithCommand('settings-view:uninstall-packages')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Packages'

      describe "settings-view:install-packages-and-themes", ->
        it "opens the settings view to the install page", ->
          openWithCommand('settings-view:install-packages-and-themes')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Install'

      describe "settings-view:check-for-package-updates", ->
        it "opens the settings view to the install page", ->
          openWithCommand('settings-view:check-for-package-updates')
          runs ->
            expect(atom.workspace.getActivePaneItem().activePanelName).toBe 'Updates'

    describe "when atom.workspace.open() is used with a config URI", ->
      beforeEach ->
        settingsView = null

      it "opens the settings to the correct panel with atom://config/<panel-name>", ->
        waitsForPromise ->
          atom.workspace.open('atom://config').then (s) -> settingsView = s

        waits 1
        runs ->
          expect(settingsView.activePanelName).toBe 'Settings'

        waitsForPromise ->
          atom.workspace.open('atom://config/themes').then (s) -> settingsView = s

        waits 1
        runs ->
          expect(settingsView.activePanelName).toBe 'Themes'

        waitsForPromise ->
          atom.workspace.open('atom://config/install').then (s) -> settingsView = s

        waits 1
        runs ->
          expect(settingsView.activePanelName).toBe 'Install'

  describe "when an installed package is clicked from the Install panel", ->
    it "displays the package details", ->
      waitsFor ->
        atom.packages.activatePackage('settings-view')

      runs ->
        settingsView.packageManager.getClient()
        spyOn(settingsView.packageManager.client, 'featuredPackages').andCallFake (callback) ->
          callback(null, [{name: 'settings-view'}])
        settingsView.showPanel('Install')

      waitsFor ->
        settingsView.find('.package-card:not(.hidden)').length > 0

      runs ->
        settingsView.find('.package-card:not(.hidden):first').click()

        packageDetail = settingsView.find('.package-detail').view()
        expect(packageDetail.title.text()).toBe 'Settings View'

  describe "when the active theme has settings", ->
    panel = null

    beforeEach ->
      atom.packages.packageDirPaths.push(path.join(__dirname, 'fixtures'))
      atom.packages.loadPackage('ui-theme-with-config')
      atom.packages.loadPackage('syntax-theme-with-config')
      atom.config.set('core.themes', ['ui-theme-with-config', 'syntax-theme-with-config'])

      reloadedHandler = jasmine.createSpy('reloadedHandler')
      atom.themes.onDidChangeActiveThemes(reloadedHandler)
      atom.themes.activatePackages()

      waitsFor "themes to be reloaded", ->
        reloadedHandler.callCount is 1

      runs ->
        settingsView.showPanel('Themes')
        panel = settingsView.find('.themes-panel').view()

    afterEach ->
      atom.themes.unwatchUserStylesheet()

    describe "when the UI theme's settings button is clicked", ->
      it "navigates to that theme's detail view", ->
        jasmine.attachToDOM(settingsView.element)
        expect(panel.currentUiThemeSettings).toBeVisible()

        panel.currentUiThemeSettings.click()
        packageDetail = settingsView.find('.package-detail').view()
        expect(packageDetail.title.text()).toBe 'Ui Theme With Config'

    describe "when the syntax theme's settings button is clicked", ->
      it "navigates to that theme's detail view", ->
        jasmine.attachToDOM(settingsView.element)
        expect(panel.currentSyntaxThemeSettings).toBeVisible()

        panel.currentSyntaxThemeSettings.click()
        packageDetail = settingsView.find('.package-detail').view()
        expect(packageDetail.title.text()).toBe 'Syntax Theme With Config'
{'fraction_non_alphanumeric': 0.12738643844634628, 'fraction_numerical': 0.0051569014702655255, 'mean_word_length': 3.187686652883069, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 160, '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': '496409', 'n_tokens_mistral': 5084, 'n_tokens_neox': 4936, 'n_words': 996}