Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
2887f3f6
Unverified
Commit
2887f3f6
authored
Nov 09, 2020
by
Marcus Andersson
Committed by
GitHub
Nov 09, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Variables: make sure that we support both old and new syntax for custom variables. (#28896)
parent
aa8d07f5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
170 additions
and
9 deletions
+170
-9
public/app/features/variables/custom/reducer.test.ts
+166
-6
public/app/features/variables/custom/reducer.ts
+4
-3
No files found.
public/app/features/variables/custom/reducer.test.ts
View file @
2887f3f6
...
...
@@ -10,8 +10,48 @@ import { CustomVariableModel } from '../types';
describe
(
'customVariableReducer'
,
()
=>
{
const
adapter
=
createCustomVariableAdapter
();
describe
(
'when createCustomOptionsFromQuery is dispatched'
,
()
=>
{
it
(
'then state should be correct'
,
()
=>
{
describe
(
'when createCustomOptionsFromQuery is dispatched with key/value syntax'
,
()
=>
{
it
(
'should then mutate state correctly'
,
()
=>
{
const
query
=
'a,b,c,d : e'
;
const
id
=
'0'
;
const
{
initialState
}
=
getVariableTestContext
(
adapter
,
{
id
,
query
});
const
payload
=
toVariablePayload
({
id
:
'0'
,
type
:
'custom'
});
reducerTester
<
VariablesState
>
()
.
givenReducer
(
customVariableReducer
,
cloneDeep
(
initialState
))
.
whenActionIsDispatched
(
createCustomOptionsFromQuery
(
payload
))
.
thenStateShouldEqual
({
[
id
]:
{
...
initialState
[
id
],
options
:
[
{
text
:
'a'
,
value
:
'a'
,
selected
:
false
,
},
{
text
:
'b'
,
value
:
'b'
,
selected
:
false
,
},
{
text
:
'c'
,
value
:
'c'
,
selected
:
false
,
},
{
text
:
'd'
,
value
:
'e'
,
selected
:
false
,
},
],
}
as
CustomVariableModel
,
});
});
});
describe
(
'when createCustomOptionsFromQuery is dispatched without key/value syntax'
,
()
=>
{
it
(
'should then mutate state correctly'
,
()
=>
{
const
query
=
'a,b,c,d:e'
;
const
id
=
'0'
;
const
{
initialState
}
=
getVariableTestContext
(
adapter
,
{
id
,
query
});
...
...
@@ -40,6 +80,46 @@ describe('customVariableReducer', () => {
selected
:
false
,
},
{
text
:
'd:e'
,
value
:
'd:e'
,
selected
:
false
,
},
],
}
as
CustomVariableModel
,
});
});
});
describe
(
'when createCustomOptionsFromQuery is dispatched and query with key/value syntax contains spaces'
,
()
=>
{
it
(
'should then mutate state correctly'
,
()
=>
{
const
query
=
'a, b, c, d : e '
;
const
id
=
'0'
;
const
{
initialState
}
=
getVariableTestContext
(
adapter
,
{
id
,
query
});
const
payload
=
toVariablePayload
({
id
:
'0'
,
type
:
'constant'
});
reducerTester
<
VariablesState
>
()
.
givenReducer
(
customVariableReducer
,
cloneDeep
(
initialState
))
.
whenActionIsDispatched
(
createCustomOptionsFromQuery
(
payload
))
.
thenStateShouldEqual
({
[
id
]:
{
...
initialState
[
id
],
options
:
[
{
text
:
'a'
,
value
:
'a'
,
selected
:
false
,
},
{
text
:
'b'
,
value
:
'b'
,
selected
:
false
,
},
{
text
:
'c'
,
value
:
'c'
,
selected
:
false
,
},
{
text
:
'd'
,
value
:
'e'
,
selected
:
false
,
...
...
@@ -50,8 +130,8 @@ describe('customVariableReducer', () => {
});
});
describe
(
'when createCustomOptionsFromQuery is dispatched and query contains spaces'
,
()
=>
{
it
(
'
then state should be correct
'
,
()
=>
{
describe
(
'when createCustomOptionsFromQuery is dispatched and query
without key/value syntax
contains spaces'
,
()
=>
{
it
(
'
should then mutate state correctly
'
,
()
=>
{
const
query
=
'a, b, c, d : e'
;
const
id
=
'0'
;
const
{
initialState
}
=
getVariableTestContext
(
adapter
,
{
id
,
query
});
...
...
@@ -90,9 +170,89 @@ describe('customVariableReducer', () => {
});
});
describe
(
'when createCustomOptionsFromQuery is dispatched and query without key/value syntax contains urls'
,
()
=>
{
it
(
'should then mutate state correctly'
,
()
=>
{
const
query
=
'a, b,http://www.google.com/, http://www.amazon.com/'
;
const
id
=
'0'
;
const
{
initialState
}
=
getVariableTestContext
(
adapter
,
{
id
,
query
});
const
payload
=
toVariablePayload
({
id
:
'0'
,
type
:
'constant'
});
reducerTester
<
VariablesState
>
()
.
givenReducer
(
customVariableReducer
,
cloneDeep
(
initialState
))
.
whenActionIsDispatched
(
createCustomOptionsFromQuery
(
payload
))
.
thenStateShouldEqual
({
[
id
]:
{
...
initialState
[
id
],
options
:
[
{
text
:
'a'
,
value
:
'a'
,
selected
:
false
,
},
{
text
:
'b'
,
value
:
'b'
,
selected
:
false
,
},
{
text
:
'http://www.google.com/'
,
value
:
'http://www.google.com/'
,
selected
:
false
,
},
{
text
:
'http://www.amazon.com/'
,
value
:
'http://www.amazon.com/'
,
selected
:
false
,
},
],
}
as
CustomVariableModel
,
});
});
});
describe
(
'when createCustomOptionsFromQuery is dispatched and query with key/value syntax contains urls'
,
()
=>
{
it
(
'should then mutate state correctly'
,
()
=>
{
const
query
=
'a, b, google : http://www.google.com/, amazon : http://www.amazon.com/'
;
const
id
=
'0'
;
const
{
initialState
}
=
getVariableTestContext
(
adapter
,
{
id
,
query
});
const
payload
=
toVariablePayload
({
id
:
'0'
,
type
:
'constant'
});
reducerTester
<
VariablesState
>
()
.
givenReducer
(
customVariableReducer
,
cloneDeep
(
initialState
))
.
whenActionIsDispatched
(
createCustomOptionsFromQuery
(
payload
))
.
thenStateShouldEqual
({
[
id
]:
{
...
initialState
[
id
],
options
:
[
{
text
:
'a'
,
value
:
'a'
,
selected
:
false
,
},
{
text
:
'b'
,
value
:
'b'
,
selected
:
false
,
},
{
text
:
'google'
,
value
:
'http://www.google.com/'
,
selected
:
false
,
},
{
text
:
'amazon'
,
value
:
'http://www.amazon.com/'
,
selected
:
false
,
},
],
}
as
CustomVariableModel
,
});
});
});
describe
(
'when createCustomOptionsFromQuery is dispatched and includeAll is true'
,
()
=>
{
it
(
'
then state should be correct
'
,
()
=>
{
const
query
=
'a,b,c,d
:
e'
;
it
(
'
should then mutate state correctly
'
,
()
=>
{
const
query
=
'a,b,c,d
:
e'
;
const
id
=
'0'
;
const
{
initialState
}
=
getVariableTestContext
(
adapter
,
{
id
,
query
,
includeAll
:
true
});
const
payload
=
toVariablePayload
({
id
:
'0'
,
type
:
'constant'
});
...
...
public/app/features/variables/custom/reducer.ts
View file @
2887f3f6
...
...
@@ -22,12 +22,13 @@ export const customVariableSlice = createSlice({
createCustomOptionsFromQuery
:
(
state
:
VariablesState
,
action
:
PayloadAction
<
VariablePayload
>
)
=>
{
const
instanceState
=
getInstanceState
<
CustomVariableModel
>
(
state
,
action
.
payload
.
id
);
const
{
includeAll
,
query
}
=
instanceState
;
const
match
=
query
.
match
(
/
(?:\\
,|
[^
,
])
+/g
)
??
[];
const
options
=
match
.
map
(
text
=>
{
text
=
text
.
replace
(
/
\\
,/g
,
','
);
const
textMatch
=
text
.
match
(
/
(?:\\
:|
[^
:
])
+/g
)
??
[];
if
(
textMatch
.
length
>
1
)
{
const
[
key
,
value
]
=
textMatch
;
const
textMatch
=
/^
(
.+
)\s
:
\s(
.+
)
$/g
.
exec
(
text
)
??
[];
if
(
textMatch
.
length
===
3
)
{
const
[
,
key
,
value
]
=
textMatch
;
return
{
text
:
key
.
trim
(),
value
:
value
.
trim
(),
selected
:
false
};
}
else
{
return
{
text
:
text
.
trim
(),
value
:
text
.
trim
(),
selected
:
false
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment