Skip to content
Snippets Groups Projects
Commit 69f17ec3 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Filter bar unit tests

parent 8f8c11a2
No related branches found
No related tags found
1 merge request!1078Filter bar unit tests
......@@ -35,7 +35,7 @@ export class Filter {
*/
constructor (name, options = {}) {
this.name = name
this.displayName = options.displayName ?? name.charAt(0).toUpperCase() + name.slice(1).toLowerCase().replace('_', ' ')
this.displayName = options.displayName ?? name.charAt(0).toUpperCase() + name.slice(1).toLowerCase().replace(/_/g, ' ')
this.placeholder = options.placeholder ?? 'Filter value'
this.operatorName = options.operatorName
this.operatorPlaceholder = options.operatorPlaceholder ?? 'Select an operator'
......@@ -101,7 +101,7 @@ export class Filter {
* A simple name filter, allowing any value.
* @extends {Filter<string>}
*/
class NameFilter extends Filter {
export class NameFilter extends Filter {
constructor () {
super('name', { operators: { eq: 'contains' } })
}
......@@ -112,7 +112,7 @@ class NameFilter extends Filter {
* @typedef {{id: string, slug: string, display_name: string, folder: boolean}} ElementType
* @extends {Filter<ElementType>}
*/
class TypeFilter extends Filter {
export class TypeFilter extends Filter {
constructor (store) {
super('type')
this.store = store
......@@ -153,7 +153,7 @@ class TypeFilter extends Filter {
* @typedef {boolean | MLClass} BestClass
* @extends {Filter<BestClass>}
*/
class BestClassFilter extends Filter {
export class BestClassFilter extends Filter {
constructor (store) {
super('best_class')
this.store = store
......@@ -188,11 +188,14 @@ class BestClassFilter extends Filter {
}
async validate (input) {
const validInputs = {
'any best class': true,
'no best class': false,
...Object.fromEntries((await this.fetchClasses()).map(mlclass => [mlclass.name.toLowerCase(), mlclass]))
}
// Early returns for simple values with no API request necessary
if (input.toLowerCase() === 'any best class') return true
if (input.toLowerCase() === 'no best class') return false
const validInputs = Object.fromEntries(
(await this.fetchClasses())
.map(mlclass => [mlclass.name.toLowerCase(), mlclass])
)
const found = validInputs[input.toLowerCase()]
if (found) return found
throw new Error(`Unknown class '${input}'`)
......@@ -218,7 +221,7 @@ class BestClassFilter extends Filter {
* @typedef {{id: string, worker: {id: string, name: string}, revision: {hash: string}}} WorkerVersion
* @extends {Filter<WorkerVersion | boolean>}
*/
class WorkerVersionFilter extends Filter {
export class WorkerVersionFilter extends Filter {
constructor (store) {
super('worker_version')
this.store = store
......@@ -266,7 +269,7 @@ class WorkerVersionFilter extends Filter {
* but allows any name to be typed, to handle metadatas created by workers or admins.
* @extends {Filter<string>}
*/
class MetadataNameFilter extends Filter {
export class MetadataNameFilter extends Filter {
constructor (store) {
super('metadata_name')
this.store = store
......@@ -290,8 +293,8 @@ class MetadataNameFilter extends Filter {
* Allows filtering with arithmetic operators, in which case the value must be a number.
* @extends {Filter<string | number>}
*/
class MetadataValueFilter extends Filter {
constructor (store) {
export class MetadataValueFilter extends Filter {
constructor () {
super('metadata_value', {
operators: {
eq: '=',
......@@ -303,7 +306,6 @@ class MetadataValueFilter extends Filter {
},
operatorName: 'metadata_operator'
})
this.store = store
}
deserialize (input, operator) {
......@@ -324,7 +326,7 @@ class MetadataValueFilter extends Filter {
* Filter methods that are common to every strictly boolean filter
* @extends {Filter<boolean>}
*/
class BooleanFilter extends Filter {
export class BooleanFilter extends Filter {
autocomplete () {
return [true, false]
}
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment