return getTree<TreeComponentWithPath>({ ...data, qualifiers: 'FIL' });
}
+export function getDirectories(data: GetTreeParams) {
+ return getTree<TreeComponentWithPath>({ ...data, qualifiers: 'DIR' });
+}
+
export function getComponentData(data: { component: string } & T.BranchParameters): Promise<any> {
return getJSON('/api/components/show', data);
}
import { translate } from 'sonar-ui-common/helpers/l10n';
import { collapsePath } from 'sonar-ui-common/helpers/path';
import { highlightTerm } from 'sonar-ui-common/helpers/search';
-import { getTree, TreeComponent } from '../../../api/components';
+import { getDirectories, TreeComponentWithPath } from '../../../api/components';
import ListStyleFacet from '../../../components/facet/ListStyleFacet';
import { Facet, Query } from '../utils';
interface Props {
componentKey: string;
- fetching: boolean;
directories: string[];
+ fetching: boolean;
loadSearchResultCount: (property: string, changes: Partial<Query>) => Promise<Facet>;
onChange: (changes: Partial<Query>) => void;
onToggle: (property: string) => void;
open: boolean;
query: Query;
- stats: T.Dict<number> | undefined;
+ stats: Facet | undefined;
}
export default class DirectoryFacet extends React.PureComponent<Props> {
- getFacetItemText = (directory: string) => {
- return collapsePath(directory, 15);
+ getFacetItemText = (path: string) => {
+ return collapsePath(path, 15);
};
- getSearchResultKey = (directory: TreeComponent) => {
- return directory.name;
+ getSearchResultKey = (directory: TreeComponentWithPath) => {
+ return directory.path;
};
- getSearchResultText = (directory: TreeComponent) => {
+ getSearchResultText = (directory: TreeComponentWithPath) => {
return directory.name;
};
handleSearch = (query: string, page: number) => {
- return getTree({
+ return getDirectories({
component: this.props.componentKey,
q: query,
- qualifiers: 'DIR',
p: page,
ps: 30
- }).then(({ components, paging }) => ({ paging, results: components }));
+ }).then(({ components, paging }) => ({
+ paging,
+ results: components.filter(dir => dir.path !== undefined)
+ }));
};
- loadSearchResultCount = (directories: TreeComponent[]) => {
+ loadSearchResultCount = (directories: TreeComponentWithPath[]) => {
return this.props.loadSearchResultCount('directories', {
- directories: directories.map(directory => directory.name)
+ directories: directories.map(directory => directory.path)
});
};
</>
);
- renderFacetItem = (directory: string) => {
- return this.renderDirectory(collapsePath(directory, 15));
+ renderFacetItem = (path: string) => {
+ return this.renderDirectory(collapsePath(path, 15));
};
- renderSearchResult = (directory: TreeComponent, term: string) => {
- return this.renderDirectory(highlightTerm(collapsePath(directory.name), term));
+ renderSearchResult = (directory: TreeComponentWithPath, term: string) => {
+ return this.renderDirectory(highlightTerm(collapsePath(directory.path, 15), term));
};
render() {
return (
- <ListStyleFacet<TreeComponent>
+ <ListStyleFacet<TreeComponentWithPath>
facetHeader={translate('issues.facet.directories')}
fetching={this.props.fetching}
getFacetItemText={this.getFacetItemText}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+import { shallow } from 'enzyme';
+import * as React from 'react';
+import { TreeComponentWithPath } from '../../../../api/components';
+import { Query } from '../../utils';
+import DirectoryFacet from '../DirectoryFacet';
+
+it('should render correctly', () => {
+ const wrapper = shallowRender();
+ const instance = wrapper.instance();
+ expect(wrapper).toMatchSnapshot();
+ expect(
+ instance.renderSearchResult({ path: 'foo/bar' } as TreeComponentWithPath, 'foo')
+ ).toMatchSnapshot();
+ expect(instance.renderFacetItem('foo/bar')).toMatchSnapshot();
+});
+
+describe("ListStyleFacet's callback props", () => {
+ const wrapper = shallowRender();
+ const instance = wrapper.instance();
+
+ test('#getSearchResultText()', () => {
+ expect(instance.getSearchResultText({ name: 'bar' } as TreeComponentWithPath)).toBe('bar');
+ });
+
+ test('#getSearchResultKey()', () => {
+ expect(instance.getSearchResultKey({ path: 'foo/bar' } as TreeComponentWithPath)).toBe(
+ 'foo/bar'
+ );
+ });
+
+ test('#getFacetItemText()', () => {
+ expect(instance.getFacetItemText('foo/bar')).toBe('foo/bar');
+ });
+});
+
+function shallowRender(props: Partial<DirectoryFacet['props']> = {}) {
+ return shallow<DirectoryFacet>(
+ <DirectoryFacet
+ componentKey="foo"
+ directories={['foo/', 'bar/baz/']}
+ fetching={false}
+ loadSearchResultCount={jest.fn()}
+ onChange={jest.fn()}
+ onToggle={jest.fn()}
+ open={false}
+ query={{} as Query}
+ stats={undefined}
+ {...props}
+ />
+ );
+}
--- /dev/null
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`should render correctly 1`] = `
+<ListStyleFacet
+ facetHeader="issues.facet.directories"
+ fetching={false}
+ getFacetItemText={[Function]}
+ getSearchResultKey={[Function]}
+ getSearchResultText={[Function]}
+ loadSearchResultCount={[Function]}
+ maxInitialItems={15}
+ maxItems={100}
+ minSearchLength={3}
+ onChange={[MockFunction]}
+ onSearch={[Function]}
+ onToggle={[MockFunction]}
+ open={false}
+ property="directories"
+ query={Object {}}
+ renderFacetItem={[Function]}
+ renderSearchResult={[Function]}
+ searchPlaceholder="search.search_for_directories"
+ values={
+ Array [
+ "foo/",
+ "bar/baz/",
+ ]
+ }
+/>
+`;
+
+exports[`should render correctly 2`] = `
+<React.Fragment>
+ <QualifierIcon
+ className="little-spacer-right"
+ qualifier="DIR"
+ />
+ <React.Fragment>
+ <mark>
+ foo
+ </mark>
+ /bar
+ </React.Fragment>
+</React.Fragment>
+`;
+
+exports[`should render correctly 3`] = `
+<React.Fragment>
+ <QualifierIcon
+ className="little-spacer-right"
+ qualifier="DIR"
+ />
+ foo/bar
+</React.Fragment>
+`;