move getQueryStringFromLocation to ui-components

This commit is contained in:
Sebastian Sdorra
2019-04-18 11:42:09 +02:00
parent bfd59c1bac
commit 37a53c295b
18 changed files with 98 additions and 69 deletions

View File

@@ -22,6 +22,7 @@ export { default as ProtectedRoute } from "./ProtectedRoute.js";
export { default as Help } from "./Help";
export { default as HelpIcon } from "./HelpIcon";
export { default as Tooltip } from "./Tooltip";
// TODO do we need this? getPageFromMatch is already exported by urls
export { getPageFromMatch } from "./urls";
export { default as Autocomplete} from "./Autocomplete";
export { default as BranchSelector } from "./BranchSelector";

View File

@@ -1,4 +1,6 @@
// @flow
import queryString from "query-string";
export const contextPath = window.ctxPath || "";
export function withContextPath(path: string) {
@@ -27,3 +29,7 @@ export function getPageFromMatch(match: any) {
}
return page;
}
export function getQueryStringFromLocation(location: any) {
return location.search ? queryString.parse(location.search).q : undefined;
}

View File

@@ -1,5 +1,5 @@
// @flow
import {concat, getPageFromMatch, withEndingSlash} from "./urls";
import { concat, getPageFromMatch, getQueryStringFromLocation, withEndingSlash } from "./urls";
describe("tests for withEndingSlash", () => {
@@ -47,3 +47,28 @@ describe("tests for getPageFromMatch", () => {
expect(getPageFromMatch(match)).toBe(42);
});
});
describe("tests for getQueryStringFromLocation", () => {
function createLocation(search: string) {
return {
search
};
}
it("should return the query string", () => {
const location = createLocation("?q=abc");
expect(getQueryStringFromLocation(location)).toBe("abc");
});
it("should return query string from multiple parameters", () => {
const location = createLocation("?x=a&y=b&q=abc&z=c");
expect(getQueryStringFromLocation(location)).toBe("abc");
});
it("should return undefined if q is not available", () => {
const location = createLocation("?x=a&y=b&z=c");
expect(getQueryStringFromLocation(location)).toBeUndefined();
});
});