Commit a7272c0c authored by Alex Terekhov's avatar Alex Terekhov Committed by Kushal Pandya

Highlight line which includes search term is code search results

parent 244966bf
export default () => {
const highlightLineClass = 'hll';
const contentBody = document.getElementById('content-body');
const searchTerm = contentBody.querySelector('.js-search-input').value.toLowerCase();
const blobs = contentBody.querySelectorAll('.blob-result');
blobs.forEach(blob => {
const lines = blob.querySelectorAll('.line');
lines.forEach(line => {
if (line.textContent.toLowerCase().includes(searchTerm)) {
line.classList.add(highlightLineClass);
}
});
});
};
...@@ -5,9 +5,11 @@ import Api from '~/api'; ...@@ -5,9 +5,11 @@ import Api from '~/api';
import { __ } from '~/locale'; import { __ } from '~/locale';
import Project from '~/pages/projects/project'; import Project from '~/pages/projects/project';
import refreshCounts from './refresh_counts'; import refreshCounts from './refresh_counts';
import setHighlightClass from './highlight_blob_search_result';
export default class Search { export default class Search {
constructor() { constructor() {
setHighlightClass();
const $groupDropdown = $('.js-search-group-dropdown'); const $groupDropdown = $('.js-search-group-dropdown');
const $projectDropdown = $('.js-search-project-dropdown'); const $projectDropdown = $('.js-search-project-dropdown');
......
---
title: Highlight line which includes search term is code search results
merge_request: 22914
author: Alex Terekhov (terales)
type: added
...@@ -16,4 +16,19 @@ describe SearchController, '(JavaScript fixtures)', type: :controller do ...@@ -16,4 +16,19 @@ describe SearchController, '(JavaScript fixtures)', type: :controller do
expect(response).to be_successful expect(response).to be_successful
end end
context 'search within a project' do
let(:namespace) { create(:namespace, name: 'frontend-fixtures') }
let(:project) { create(:project, :public, :repository, namespace: namespace, path: 'search-project') }
it 'search/blob_search_result.html' do
get :show, params: {
search: 'Send',
project_id: project.id,
scope: :blobs
}
expect(response).to be_successful
end
end
end end
import setHighlightClass from '~/pages/search/show/highlight_blob_search_result';
const fixture = 'search/blob_search_result.html';
describe('pages/search/show/highlight_blob_search_result', () => {
preloadFixtures(fixture);
beforeEach(() => loadFixtures(fixture));
it('highlights lines with search term occurrence', () => {
setHighlightClass();
expect(document.querySelectorAll('.blob-result .hll').length).toBe(11);
});
});
import $ from 'jquery'; import $ from 'jquery';
import Api from '~/api'; import Api from '~/api';
import Search from '~/pages/search/show/search'; import Search from '~/pages/search/show/search';
import setHighlightClass from '~/pages/search/show/highlight_blob_search_result';
jest.mock('~/api'); jest.mock('~/api');
jest.mock('~/pages/search/show/highlight_blob_search_result');
describe('Search', () => { describe('Search', () => {
const fixturePath = 'search/show.html'; const fixturePath = 'search/show.html';
...@@ -16,27 +18,41 @@ describe('Search', () => { ...@@ -16,27 +18,41 @@ describe('Search', () => {
preloadFixtures(fixturePath); preloadFixtures(fixturePath);
beforeEach(() => { describe('constructor side effects', () => {
loadFixtures(fixturePath); afterEach(() => {
new Search(); // eslint-disable-line no-new jest.restoreAllMocks();
});
it('highlights lines with search terms in blob search results', () => {
new Search(); // eslint-disable-line no-new
expect(setHighlightClass).toHaveBeenCalled();
});
}); });
it('requests groups from backend when filtering', () => { describe('dropdown behavior', () => {
jest.spyOn(Api, 'groups').mockImplementation(term => { beforeEach(() => {
expect(term).toBe(searchTerm); loadFixtures(fixturePath);
new Search(); // eslint-disable-line no-new
}); });
const inputElement = fillDropdownInput('.js-search-group-dropdown'); it('requests groups from backend when filtering', () => {
jest.spyOn(Api, 'groups').mockImplementation(term => {
expect(term).toBe(searchTerm);
});
$(inputElement).trigger('input'); const inputElement = fillDropdownInput('.js-search-group-dropdown');
});
it('requests projects from backend when filtering', () => { $(inputElement).trigger('input');
jest.spyOn(Api, 'projects').mockImplementation(term => {
expect(term).toBe(searchTerm);
}); });
const inputElement = fillDropdownInput('.js-search-project-dropdown');
$(inputElement).trigger('input'); it('requests projects from backend when filtering', () => {
jest.spyOn(Api, 'projects').mockImplementation(term => {
expect(term).toBe(searchTerm);
});
const inputElement = fillDropdownInput('.js-search-project-dropdown');
$(inputElement).trigger('input');
});
}); });
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment