-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
global: array of filters as input for query
- Loading branch information
Showing
3 changed files
with
279 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
/* | ||
* This file is part of React-SearchKit. | ||
* Copyright (C) 2018-2019 CERN. | ||
* | ||
* React-SearchKit is free software; you can redistribute it and/or modify it | ||
* under the terms of the MIT License; see LICENSE file for more details. | ||
*/ | ||
|
||
import { updateQueryFilters } from './query'; | ||
|
||
/** | ||
* Same tests as query.test.js but query is an array of filters. | ||
*/ | ||
describe('queries with first level filters.', () => { | ||
test('query with `type: Publication` should be added when not in the state.', () => { | ||
const state = [['file_type', 'pdf']]; | ||
const query = [['type', 'Publication']]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'pdf'], | ||
['type', 'Publication'], | ||
]); | ||
}); | ||
|
||
test('query with `type: Publication` should be added when another `type` is already in the state.', () => { | ||
const state = [ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
]; | ||
const query = [['type', 'Publication']]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication'], | ||
]); | ||
}); | ||
|
||
test('query with `type: Image` should remove it from the state when it is already there.', () => { | ||
const state = [ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication'], | ||
]; | ||
const query = [['type', 'Image']]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'pdf'], | ||
['type', 'Publication'], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('queries with second level filters.', () => { | ||
test('query with `subtype: Other` should be added when not in the state.', () => { | ||
const state = [ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
]; | ||
const query = [['type', 'Publication', ['subtype', 'Other']]]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication', ['subtype', 'Other']], | ||
]); | ||
}); | ||
|
||
test('query with `type` should remove from the state any children query.', () => { | ||
const state = [ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication', ['subtype', 'Other']], | ||
['type', 'Publication', ['subtype', 'Report']], | ||
['type', 'Publication', ['subtype', 'Article']], | ||
]; | ||
const query = [['type', 'Publication']]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
]); | ||
}); | ||
|
||
test('query with `subtype: Other` should remove any query with the parent `type: Publication`.', () => { | ||
const state = [ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication'], | ||
]; | ||
const query = [['type', 'Publication', ['subtype', 'Other']]]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication', ['subtype', 'Other']], | ||
]); | ||
}); | ||
|
||
test('query with `subtype: Other` should remove it from the state when it is already there.', () => { | ||
const state = [ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication', ['subtype', 'Other']], | ||
]; | ||
const query = [['type', 'Publication', ['subtype', 'Other']]]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication'], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('queries with third level filters.', () => { | ||
test('query with `subsubtype: Public reports` should be added when not in the state.', () => { | ||
const state = [ | ||
['file_type', 'png'], | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
]; | ||
const query = [['type', 'Publication', ['subtype', 'Report']]]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'png'], | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication', ['subtype', 'Report']], | ||
]); | ||
}); | ||
|
||
test('query with `subtype: Report` should remove from the state any child query with `subsubtype`', () => { | ||
const state = [ | ||
['file_type', 'png'], | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
[ | ||
'type', | ||
'Publication', | ||
['subtype', 'Report', ['subsubtype', 'Restricted']], | ||
], | ||
['type', 'Publication', ['subtype', 'Report', ['subsubtype', 'Public']]], | ||
]; | ||
const query = [['type', 'Publication', ['subtype', 'Report']]]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'png'], | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication'], | ||
]); | ||
}); | ||
|
||
test('query with `type: Publication` should remove from the state any child query with `subtype` or `subsubtype`', () => { | ||
const state = [ | ||
['file_type', 'png'], | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
[ | ||
'type', | ||
'Publication', | ||
['subtype', 'Report', ['subsubtype', 'Restricted']], | ||
], | ||
]; | ||
const query = [['type', 'Publication']]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'png'], | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
]); | ||
}); | ||
|
||
test('query with `subsubtype: Report` should remove it from the state when it is already there.', () => { | ||
const state = [ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
[ | ||
'type', | ||
'Publication', | ||
['subtype', 'Report', ['subsubtype', 'Public reports']], | ||
], | ||
]; | ||
const query = [ | ||
[ | ||
'type', | ||
'Publication', | ||
['subtype', 'Report', ['subsubtype', 'Public reports']], | ||
], | ||
]; | ||
|
||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([ | ||
['file_type', 'pdf'], | ||
['type', 'Image'], | ||
['type', 'Publication', ['subtype', 'Report']], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('user clears previous selections', () => { | ||
const state = [ | ||
['type', 'Publication', ['subtype', 'Report', ['subsubtype', 'Public']]], | ||
['file_type', 'pdf'], | ||
]; | ||
|
||
test('`type` selections are removed when user unselects it', () => { | ||
const query = [['type', 'Publication']]; | ||
const newState = updateQueryFilters(query, state); | ||
|
||
expect(newState).toEqual([['file_type', 'pdf']]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters