@ -2,7 +2,7 @@ import { lastValueFrom, Observable, of } from 'rxjs';
import { ALL_VARIABLE_TEXT , ALL_VARIABLE_VALUE } from 'app/features/variables/constants' ;
import { SceneVariableValueChangedEvent , VariableValueOption } from '../types' ;
import { SceneVariableValueChangedEvent , VariableValueCustom , VariableValue Option } from '../types' ;
import { MultiValueVariable , MultiValueVariableState , VariableGetOptionsArgs } from '../variants/MultiValueVariable' ;
export interface ExampleVariableState extends MultiValueVariableState {
@ -46,6 +46,22 @@ describe('MultiValueVariable', () => {
expect ( variable . state . text ) . toBe ( 'B' ) ;
} ) ;
it ( 'Should pick All value when defaultToAll is true' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
options : [ ] ,
optionsToReturn : [
{ label : 'B' , value : 'B' } ,
{ label : 'C' , value : 'C' } ,
] ,
defaultToAll : true ,
} ) ;
await lastValueFrom ( variable . validateAndUpdate ( ) ) ;
expect ( variable . state . value ) . toBe ( ALL_VARIABLE_VALUE ) ;
} ) ;
it ( 'Should keep current value if current value is valid' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
@ -99,6 +115,26 @@ describe('MultiValueVariable', () => {
expect ( variable . state . text ) . toEqual ( [ 'A' ] ) ;
} ) ;
it ( 'Should select All option if none of the current values are valid' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
options : [ ] ,
isMulti : true ,
defaultToAll : true ,
optionsToReturn : [
{ label : 'A' , value : 'A' } ,
{ label : 'C' , value : 'C' } ,
] ,
value : [ 'D' , 'E' ] ,
text : [ 'E' , 'E' ] ,
} ) ;
await lastValueFrom ( variable . validateAndUpdate ( ) ) ;
expect ( variable . state . value ) . toEqual ( [ ALL_VARIABLE_VALUE ] ) ;
expect ( variable . state . text ) . toEqual ( [ ALL_VARIABLE_TEXT ] ) ;
} ) ;
it ( 'Should handle $__all value and send change event even when value is still $__all' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
@ -123,6 +159,60 @@ describe('MultiValueVariable', () => {
} ) ;
} ) ;
describe ( 'changeValueTo' , ( ) = > {
it ( 'Should set default empty state to all value if defaultToAll multi' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
options : [ ] ,
isMulti : true ,
defaultToAll : true ,
optionsToReturn : [ ] ,
value : [ '1' ] ,
text : [ 'A' ] ,
} ) ;
variable . changeValueTo ( [ ] ) ;
expect ( variable . state . value ) . toEqual ( [ ALL_VARIABLE_VALUE ] ) ;
} ) ;
it ( 'When changing to all value' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
options : [
{ label : 'A' , value : '1' } ,
{ label : 'B' , value : '2' } ,
] ,
isMulti : true ,
defaultToAll : true ,
optionsToReturn : [ ] ,
value : [ '1' ] ,
text : [ 'A' ] ,
} ) ;
variable . changeValueTo ( [ '1' , ALL_VARIABLE_VALUE ] ) ;
// Should clear the value so only all value is set
expect ( variable . state . value ) . toEqual ( [ ALL_VARIABLE_VALUE ] ) ;
} ) ;
it ( 'When changing from all value' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
options : [
{ label : 'A' , value : '1' } ,
{ label : 'B' , value : '2' } ,
] ,
isMulti : true ,
defaultToAll : true ,
optionsToReturn : [ ] ,
} ) ;
variable . changeValueTo ( [ ALL_VARIABLE_VALUE , '1' ] ) ;
// Should remove the all value so only the new value is present
expect ( variable . state . value ) . toEqual ( [ '1' ] ) ;
} ) ;
} ) ;
describe ( 'getValue and getValueText' , ( ) = > {
it ( 'GetValueText should return text' , async ( ) = > {
const variable = new ExampleVariable ( {
@ -163,6 +253,63 @@ describe('MultiValueVariable', () => {
expect ( variable . getValue ( ) ) . toEqual ( [ '1' , '2' ] ) ;
} ) ;
it ( 'GetValue should return allValue when value is $__all' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
options : [ ] ,
optionsToReturn : [ ] ,
value : ALL_VARIABLE_VALUE ,
allValue : '.*' ,
text : 'A' ,
} ) ;
const value = variable . getValue ( ) as VariableValueCustom ;
expect ( value . isCustomValue ) . toBe ( true ) ;
expect ( value . toString ( ) ) . toBe ( '.*' ) ;
} ) ;
} ) ;
describe ( 'getOptionsForSelect' , ( ) = > {
it ( 'Should return options' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
options : [ { label : 'A' , value : '1' } ] ,
optionsToReturn : [ ] ,
value : '1' ,
text : 'A' ,
} ) ;
expect ( variable . getOptionsForSelect ( ) ) . toEqual ( [ { label : 'A' , value : '1' } ] ) ;
} ) ;
it ( 'Should return include All option when includeAll is true' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
options : [ { label : 'A' , value : '1' } ] ,
optionsToReturn : [ ] ,
includeAll : true ,
value : '1' ,
text : 'A' ,
} ) ;
expect ( variable . getOptionsForSelect ( ) ) . toEqual ( [
{ label : ALL_VARIABLE_TEXT , value : ALL_VARIABLE_VALUE } ,
{ label : 'A' , value : '1' } ,
] ) ;
} ) ;
it ( 'Should add current value if not found' , async ( ) = > {
const variable = new ExampleVariable ( {
name : 'test' ,
options : [ ] ,
optionsToReturn : [ ] ,
value : '1' ,
text : 'A' ,
} ) ;
expect ( variable . getOptionsForSelect ( ) ) . toEqual ( [ { label : 'A' , value : '1' } ] ) ;
} ) ;
} ) ;
describe ( 'Url syncing' , ( ) = > {