Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/encoded/static/libs/__tests__/query_string-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import QueryString from '../query_string';

describe('QueryString Class', () => {
it('Returns given query string unmodified', () => {
let query = new QueryString('type=Experiment&assay_term_id=OBI:0000716&type=ReferenceEpigenome&assay_title=Histone+ChIP-seq');
expect(query.format()).toEqual('type=Experiment&assay_term_id=OBI:0000716&type=ReferenceEpigenome&assay_title=Histone+ChIP-seq');
query = new QueryString('type=Experiment&assay_term_id=OBI:0000716&type!=ReferenceEpigenome&assay_title=Histone+ChIP-seq');
expect(query.format()).toEqual('type=Experiment&assay_term_id=OBI:0000716&type!=ReferenceEpigenome&assay_title=Histone+ChIP-seq');
let query = new QueryString('type=Experiment&type=ReferenceEpigenome&assay_term_id=OBI:0000716&assay_title=Histone+ChIP-seq');
expect(query.format()).toEqual('type=Experiment&type=ReferenceEpigenome&assay_term_id=OBI:0000716&assay_title=Histone+ChIP-seq');
query = new QueryString('type=Experiment&type!=ReferenceEpigenome&assay_term_id=OBI:0000716&assay_title=Histone+ChIP-seq');
expect(query.format()).toEqual('type=Experiment&type!=ReferenceEpigenome&assay_term_id=OBI:0000716&assay_title=Histone+ChIP-seq');
});

it('Finds all values matching a key', () => {
Expand All @@ -26,7 +26,7 @@ describe('QueryString Class', () => {
query.addKeyValue('assay_term_id', 'OBI:0000716');
expect(query.format()).toEqual('type=Experiment&assay_term_id=OBI:0000716');
query.addKeyValue('type', 'Annotation', true);
expect(query.format()).toEqual('type=Experiment&assay_term_id=OBI:0000716&type!=Annotation');
expect(query.format()).toEqual('type=Experiment&type!=Annotation&assay_term_id=OBI:0000716');
});

it('Deletes all keys of a value', () => {
Expand All @@ -36,7 +36,7 @@ describe('QueryString Class', () => {

query = new QueryString('type=Experiment&assay_term_id=OBI:0000716&type=Annotation');
query.deleteKeyValue('type', 'Experiment');
expect(query.format()).toEqual('assay_term_id=OBI:0000716&type=Annotation');
expect(query.format()).toEqual('type=Annotation&assay_term_id=OBI:0000716');

query = new QueryString('type=Experiment&assay_term_id=OBI:0000716&type!=Annotation');
query.deleteKeyValue('type');
Expand All @@ -50,27 +50,27 @@ describe('QueryString Class', () => {
it('Replaces all keys with a new value', () => {
let query = new QueryString('type=Experiment&assay_term_id=OBI:0000716&type=Annotation');
query.replaceKeyValue('type', 'Biosample');
expect(query.format()).toEqual('assay_term_id=OBI:0000716&type=Biosample');
expect(query.format()).toEqual('type=Biosample&assay_term_id=OBI:0000716');

query = new QueryString('type=Experiment&assay_term_id=OBI:0000716&type=Annotation');
query.replaceKeyValue('assay_term_id', 'OBI:0001919');
expect(query.format()).toEqual('type=Experiment&type=Annotation&assay_term_id=OBI:0001919');

query = new QueryString('type=Experiment&assay_term_id=OBI:0000716&type!=Annotation');
query.replaceKeyValue('type', 'Biosample');
expect(query.format()).toEqual('assay_term_id=OBI:0000716&type=Biosample');
expect(query.format()).toEqual('type=Biosample&assay_term_id=OBI:0000716');

query = new QueryString('type=Experiment&assay_term_id=OBI:0000716&type!=Annotation');
query.replaceKeyValue('type', 'Biosample', true);
expect(query.format()).toEqual('assay_term_id=OBI:0000716&type!=Biosample');
expect(query.format()).toEqual('type!=Biosample&assay_term_id=OBI:0000716');
});

it('Creates an independent clone', () => {
const query = new QueryString('type=Experiment&assay_term_id=OBI:0000716&type!=Annotation');
const queryCopy = query.clone();
queryCopy.addKeyValue('assay_title', 'Histone ChIP-seq', true);
expect(query.format()).toEqual('type=Experiment&assay_term_id=OBI:0000716&type!=Annotation');
expect(queryCopy.format()).toEqual('type=Experiment&assay_term_id=OBI:0000716&type!=Annotation&assay_title!=Histone+ChIP-seq');
expect(query.format()).toEqual('type=Experiment&type!=Annotation&assay_term_id=OBI:0000716');
expect(queryCopy.format()).toEqual('type=Experiment&type!=Annotation&assay_term_id=OBI:0000716&assay_title!=Histone+ChIP-seq');
});

it('Counts the number of query keys', () => {
Expand Down
18 changes: 15 additions & 3 deletions src/encoded/static/libs/query_string.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,25 @@ class QueryString {
/**
* Get the query string corresponding to the query this object holds. If you have made no
* modifications to the query this object holds, you get back the same query string that
* initialized this object.
* initialized this object. `type=` elements get sorted to the front to avoid requests getting
* blocked.
*
* @return {string} Equivalent query string.
*/
format() {
return this._parsedQuery.map((queryElement) => {
const key = Object.keys(queryElement)[0];
// If any of the query strings has a key of `type` move all those to the front of the query
// string. The `reduce()` accumulator is a two-element array. The first element is an array
// of `type=` elements, and the second element is an array of all other query elements.
const [typeQueries, otherQueries] = this._parsedQuery.reduce(
([types, others], queryElement) =>
queryElement.type ? [types.concat(queryElement), others] : [types, others.concat(queryElement)],
[[], []]
);
const sortedQueries = [...typeQueries, ...otherQueries];

// Convert sorted queries to the formatted query string.
return sortedQueries.map((queryElement) => {
const key = QueryString._getQueryElementKey(queryElement);
return `${key}${queryElement.negative ? '!=' : '='}${queryEncoding.encodedURIComponent(queryElement[key])}`;
}).join('&');
}
Expand Down