<commit_before>/** @babel */

const path = require("path");
const {beforeEach, it, fit, ffit, fffit} = require('./async-spec-helpers')

describe("FindView", () => {
  let workspaceElement, editorView, editor, findView, activationPromise;

  function getFindAtomPanel() {
    return workspaceElement.querySelector(".find-and-replace").parentNode;
  }

  function getResultDecorations(editor, clazz) {
    const result = [];
    const decorations = editor.decorationsStateForScreenRowRange(0, editor.getLineCount())
    for (let id in decorations) {
      const decoration = decorations[id]
      if (decoration.properties.class === clazz) {
        result.push(decoration);
      }
    }
    return result;
  }

  beforeEach(async () => {
    spyOn(atom, "beep");
    workspaceElement = atom.views.getView(atom.workspace);
    workspaceElement.style.height = '800px'
    atom.project.setPaths([path.join(__dirname, "fixtures")]);

    await atom.workspace.open("sample.js");

    jasmine.attachToDOM(workspaceElement);
    editor = atom.workspace.getCenter().getActiveTextEditor();
    editorView = editor.element;

    activationPromise = atom.packages.activatePackage("find-and-replace").then(function({mainModule}) {
      mainModule.createViews();
      ({findView} = mainModule);
    });
  });

  describe("when find-and-replace:show is triggered", () => {
    it("attaches FindView to the root view", async () => {
      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      expect(workspaceElement.querySelector(".find-and-replace")).toBeDefined();
    });

    it("populates the findEditor with selection when there is a selection", async () => {
      editor.setSelectedBufferRange([[2, 8], [2, 13]]);

      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      expect(getFindAtomPanel()).toBeVisible();
      expect(findView.findEditor.getText()).toBe("items");

      findView.findEditor.setText("");
      editor.setSelectedBufferRange([[2, 14], [2, 20]]);
      atom.commands.dispatch(editorView, "find-and-replace:show");
      expect(getFindAtomPanel()).toBeVisible();
      expect(findView.findEditor.getText()).toBe("length");
    });

    it("does not change the findEditor text when there is no selection", async () => {
      editor.setSelectedBufferRange([[2, 8], [2, 8]]);

      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      findView.findEditor.setText("kitten");
      atom.commands.dispatch(editorView, "find-and-replace:show");
      expect(findView.findEditor.getText()).toBe("kitten");
    });

    it("does not change the findEditor text when there is a multiline selection", async () => {
      editor.setSelectedBufferRange([[2, 8], [3, 12]]);

      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      expect(getFindAtomPanel()).toBeVisible();
      expect(findView.findEditor.getText()).toBe("");
    });

    it("honors config settings for find options", async () => {
      atom.config.set("find-and-replace.useRegex", true);
      atom.config.set("find-and-replace.caseSensitive", true);
      atom.config.set("find-and-replace.inCurrentSelection", true);

      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      expect(findView.refs.caseOptionButton).toHaveClass("selected");
      expect(findView.refs.regexOptionButton).toHaveClass("selected");
      expect(findView.refs.selectionOptionButton).toHaveClass("selected");
    });

    it("places selected text into the find editor and escapes it when Regex is enabled", async () => {
      atom.config.set("find-and-replace.useRegex", true);
      editor.setSelectedBufferRange([[6, 6], [6, 65]]);

      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      expect(findView.findEditor.getText()).toBe(
        "current < pivot \\? left\\.push\\(current\\) : right\\.push\\(current\\);"
      );
    });
  });<commit_msg>Add test to verify that the find text is selected correctly<commit_after>/** @babel */

const path = require("path");
const {beforeEach, it, fit, ffit, fffit} = require('./async-spec-helpers')

describe("FindView", () => {
  let workspaceElement, editorView, editor, findView, activationPromise;

  function getFindAtomPanel() {
    return workspaceElement.querySelector(".find-and-replace").parentNode;
  }

  function getResultDecorations(editor, clazz) {
    const result = [];
    const decorations = editor.decorationsStateForScreenRowRange(0, editor.getLineCount())
    for (let id in decorations) {
      const decoration = decorations[id]
      if (decoration.properties.class === clazz) {
        result.push(decoration);
      }
    }
    return result;
  }

  beforeEach(async () => {
    spyOn(atom, "beep");
    workspaceElement = atom.views.getView(atom.workspace);
    workspaceElement.style.height = '800px'
    atom.project.setPaths([path.join(__dirname, "fixtures")]);

    await atom.workspace.open("sample.js");

    jasmine.attachToDOM(workspaceElement);
    editor = atom.workspace.getCenter().getActiveTextEditor();
    editorView = editor.element;

    activationPromise = atom.packages.activatePackage("find-and-replace").then(function({mainModule}) {
      mainModule.createViews();
      ({findView} = mainModule);
    });
  });

  describe("when find-and-replace:show is triggered", () => {
    it("attaches FindView to the root view", async () => {
      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      expect(workspaceElement.querySelector(".find-and-replace")).toBeDefined();
    });

    it("populates the findEditor with selection when there is a selection", async () => {
      editor.setSelectedBufferRange([[2, 8], [2, 13]]);

      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      expect(getFindAtomPanel()).toBeVisible();
      expect(findView.findEditor.getText()).toBe("items");

      findView.findEditor.setText("");
      editor.setSelectedBufferRange([[2, 14], [2, 20]]);
      atom.commands.dispatch(editorView, "find-and-replace:show");
      expect(getFindAtomPanel()).toBeVisible();
      expect(findView.findEditor.getText()).toBe("length");
    });

    it("does not change the findEditor text when there is no selection", async () => {
      editor.setSelectedBufferRange([[2, 8], [2, 8]]);

      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      findView.findEditor.setText("kitten");
      atom.commands.dispatch(editorView, "find-and-replace:show");
      expect(findView.findEditor.getText()).toBe("kitten");
    });

    it("does not change the findEditor text when there is a multiline selection", async () => {
      editor.setSelectedBufferRange([[2, 8], [3, 12]]);

      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      expect(getFindAtomPanel()).toBeVisible();
      expect(findView.findEditor.getText()).toBe("");
    });

    it("honors config settings for find options", async () => {
      atom.config.set("find-and-replace.useRegex", true);
      atom.config.set("find-and-replace.caseSensitive", true);
      atom.config.set("find-and-replace.inCurrentSelection", true);

      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      expect(findView.refs.caseOptionButton).toHaveClass("selected");
      expect(findView.refs.regexOptionButton).toHaveClass("selected");
      expect(findView.refs.selectionOptionButton).toHaveClass("selected");
    });

    it("places selected text into the find editor and escapes it when Regex is enabled", async () => {
      atom.config.set("find-and-replace.useRegex", true);
      editor.setSelectedBufferRange([[6, 6], [6, 65]]);

      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      expect(findView.findEditor.getText()).toBe(
        "current < pivot \\? left\\.push\\(current\\) : right\\.push\\(current\\);"
      );
    });

    it('selects the text to find when the panel is re-shown', async () => {
      atom.commands.dispatch(editorView, "find-and-replace:show");
      await activationPromise;

      const stringToSearch = "not found";
      const findEditor = findView.findEditor;

      findEditor.setText(stringToSearch);

      atom.commands.dispatch(findEditor.element, "core:confirm");
      atom.commands.dispatch(document.activeElement, "core:cancel");
      atom.commands.dispatch(editorView, "find-and-replace:show");

      expect(findEditor.getSelectedBufferRange()).toEqual([[0, 0], [0, stringToSearch.length]]);

      const selectionElement = findEditor.getElement().querySelector('.highlight.selection .selection');

      expect(selectionElement.getBoundingClientRect().width).toBeGreaterThan(0);
    });
  });
{'fraction_non_alphanumeric': 0.15485941996900598, 'fraction_numerical': 0.006862962143015275, 'mean_word_length': 3.8393144081414032, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '4083493', 'n_tokens_mistral': 2662, 'n_tokens_neox': 2572, 'n_words': 468}