1import {
2  containsVariable,
3  ensureStringValues,
4  findTemplateVarChanges,
5  getCurrentText,
6  getVariableRefresh,
7  isAllVariable,
8} from './utils';
9import { VariableRefresh } from './types';
10import { UrlQueryMap } from '@grafana/data';
11
12describe('isAllVariable', () => {
13  it.each`
14    variable                                         | expected
15    ${null}                                          | ${false}
16    ${undefined}                                     | ${false}
17    ${{}}                                            | ${false}
18    ${{ current: {} }}                               | ${false}
19    ${{ current: { text: '' } }}                     | ${false}
20    ${{ current: { text: null } }}                   | ${false}
21    ${{ current: { text: undefined } }}              | ${false}
22    ${{ current: { text: 'Alll' } }}                 | ${false}
23    ${{ current: { text: 'All' } }}                  | ${true}
24    ${{ current: { text: [] } }}                     | ${false}
25    ${{ current: { text: [null] } }}                 | ${false}
26    ${{ current: { text: [undefined] } }}            | ${false}
27    ${{ current: { text: ['Alll'] } }}               | ${false}
28    ${{ current: { text: ['Alll', 'All'] } }}        | ${false}
29    ${{ current: { text: ['All'] } }}                | ${true}
30    ${{ current: { text: ['All', 'Alll'] } }}        | ${true}
31    ${{ current: { text: { prop1: 'test' } } }}      | ${false}
32    ${{ current: { value: '' } }}                    | ${false}
33    ${{ current: { value: null } }}                  | ${false}
34    ${{ current: { value: undefined } }}             | ${false}
35    ${{ current: { value: '$__alll' } }}             | ${false}
36    ${{ current: { value: '$__all' } }}              | ${true}
37    ${{ current: { value: [] } }}                    | ${false}
38    ${{ current: { value: [null] } }}                | ${false}
39    ${{ current: { value: [undefined] } }}           | ${false}
40    ${{ current: { value: ['$__alll'] } }}           | ${false}
41    ${{ current: { value: ['$__alll', '$__all'] } }} | ${false}
42    ${{ current: { value: ['$__all'] } }}            | ${true}
43    ${{ current: { value: ['$__all', '$__alll'] } }} | ${true}
44    ${{ current: { value: { prop1: 'test' } } }}     | ${false}
45    ${{ current: { value: '', text: '' } }}          | ${false}
46    ${{ current: { value: '', text: 'All' } }}       | ${true}
47    ${{ current: { value: '$__all', text: '' } }}    | ${true}
48    ${{ current: { value: '', text: ['All'] } }}     | ${true}
49    ${{ current: { value: ['$__all'], text: '' } }}  | ${true}
50  `("when called with params: 'variable': '$variable' then result should be '$expected'", ({ variable, expected }) => {
51    expect(isAllVariable(variable)).toEqual(expected);
52  });
53});
54
55describe('getCurrentText', () => {
56  it.each`
57    variable                                    | expected
58    ${null}                                     | ${''}
59    ${undefined}                                | ${''}
60    ${{}}                                       | ${''}
61    ${{ current: {} }}                          | ${''}
62    ${{ current: { text: '' } }}                | ${''}
63    ${{ current: { text: null } }}              | ${''}
64    ${{ current: { text: undefined } }}         | ${''}
65    ${{ current: { text: 'A' } }}               | ${'A'}
66    ${{ current: { text: 'All' } }}             | ${'All'}
67    ${{ current: { text: [] } }}                | ${''}
68    ${{ current: { text: [null] } }}            | ${''}
69    ${{ current: { text: [undefined] } }}       | ${''}
70    ${{ current: { text: ['A'] } }}             | ${'A'}
71    ${{ current: { text: ['A', 'All'] } }}      | ${'A,All'}
72    ${{ current: { text: ['All'] } }}           | ${'All'}
73    ${{ current: { text: { prop1: 'test' } } }} | ${''}
74  `("when called with params: 'variable': '$variable' then result should be '$expected'", ({ variable, expected }) => {
75    expect(getCurrentText(variable)).toEqual(expected);
76  });
77});
78
79describe('getVariableRefresh', () => {
80  it.each`
81    variable                                           | expected
82    ${null}                                            | ${VariableRefresh.never}
83    ${undefined}                                       | ${VariableRefresh.never}
84    ${{}}                                              | ${VariableRefresh.never}
85    ${{ refresh: VariableRefresh.never }}              | ${VariableRefresh.never}
86    ${{ refresh: VariableRefresh.onTimeRangeChanged }} | ${VariableRefresh.onTimeRangeChanged}
87    ${{ refresh: VariableRefresh.onDashboardLoad }}    | ${VariableRefresh.onDashboardLoad}
88    ${{ refresh: 'invalid' }}                          | ${VariableRefresh.never}
89  `("when called with params: 'variable': '$variable' then result should be '$expected'", ({ variable, expected }) => {
90    expect(getVariableRefresh(variable)).toEqual(expected);
91  });
92});
93
94describe('findTemplateVarChanges', () => {
95  it('detect adding/removing a variable', () => {
96    const a: UrlQueryMap = {};
97    const b: UrlQueryMap = {
98      'var-xyz': 'hello',
99      aaa: 'ignore me',
100    };
101
102    expect(findTemplateVarChanges(b, a)).toEqual({ 'var-xyz': { value: 'hello' } });
103    expect(findTemplateVarChanges(a, b)).toEqual({ 'var-xyz': { value: '', removed: true } });
104  });
105
106  it('then should ignore equal values', () => {
107    const a: UrlQueryMap = {
108      'var-xyz': 'hello',
109      bbb: 'ignore me',
110    };
111    const b: UrlQueryMap = {
112      'var-xyz': 'hello',
113      aaa: 'ignore me',
114    };
115
116    expect(findTemplateVarChanges(b, a)).toBeUndefined();
117    expect(findTemplateVarChanges(a, b)).toBeUndefined();
118  });
119
120  it('then should ignore equal values with empty values', () => {
121    const a: UrlQueryMap = {
122      'var-xyz': '',
123      bbb: 'ignore me',
124    };
125    const b: UrlQueryMap = {
126      'var-xyz': '',
127      aaa: 'ignore me',
128    };
129
130    expect(findTemplateVarChanges(b, a)).toBeUndefined();
131    expect(findTemplateVarChanges(a, b)).toBeUndefined();
132  });
133
134  it('then should ignore empty array values', () => {
135    const a: UrlQueryMap = {
136      'var-adhoc': [],
137    };
138    const b: UrlQueryMap = {};
139
140    expect(findTemplateVarChanges(b, a)).toBeUndefined();
141    expect(findTemplateVarChanges(a, b)).toBeUndefined();
142  });
143
144  it('Should handle array values with one value same as just value', () => {
145    const a: UrlQueryMap = {
146      'var-test': ['test'],
147    };
148    const b: UrlQueryMap = {
149      'var-test': 'test',
150    };
151
152    expect(findTemplateVarChanges(b, a)).toBeUndefined();
153    expect(findTemplateVarChanges(a, b)).toBeUndefined();
154  });
155
156  it('Should detect change in array value and return array with single value', () => {
157    const a: UrlQueryMap = {
158      'var-test': ['test'],
159    };
160    const b: UrlQueryMap = {
161      'var-test': 'asd',
162    };
163
164    expect(findTemplateVarChanges(a, b)!['var-test']).toEqual({ value: ['test'] });
165  });
166});
167
168describe('ensureStringValues', () => {
169  it.each`
170    value              | expected
171    ${null}            | ${''}
172    ${undefined}       | ${''}
173    ${{}}              | ${''}
174    ${{ current: {} }} | ${''}
175    ${1}               | ${'1'}
176    ${[1, 2]}          | ${['1', '2']}
177    ${'1'}             | ${'1'}
178    ${['1', '2']}      | ${['1', '2']}
179    ${true}            | ${'true'}
180  `('when called with value:$value then result should be:$expected', ({ value, expected }) => {
181    expect(ensureStringValues(value)).toEqual(expected);
182  });
183});
184
185describe('containsVariable', () => {
186  it.each`
187    value                               | expected
188    ${''}                               | ${false}
189    ${'$var'}                           | ${true}
190    ${{ thing1: '${var}' }}             | ${true}
191    ${{ thing1: ['1', '${var}'] }}      | ${true}
192    ${{ thing1: { thing2: '${var}' } }} | ${true}
193  `('when called with value:$value then result should be:$expected', ({ value, expected }) => {
194    expect(containsVariable(value, 'var')).toEqual(expected);
195  });
196});
197