mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki Variable Query Editor: Fix bug when the query is updated (#66509)
* Variable Query Editor: Remove options override in effect * Add regression test
This commit is contained in:
parent
8d6314c654
commit
46742f6d96
@ -26,7 +26,14 @@ describe('LokiVariableQueryEditor', () => {
|
|||||||
onChange: () => {},
|
onChange: () => {},
|
||||||
};
|
};
|
||||||
|
|
||||||
jest.spyOn(props.datasource, 'labelNamesQuery').mockResolvedValue([]);
|
jest.spyOn(props.datasource, 'labelNamesQuery').mockResolvedValue([
|
||||||
|
{
|
||||||
|
text: 'moon',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: 'luna',
|
||||||
|
},
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Allows to create a Label names variable', async () => {
|
test('Allows to create a Label names variable', async () => {
|
||||||
@ -47,14 +54,6 @@ describe('LokiVariableQueryEditor', () => {
|
|||||||
|
|
||||||
test('Allows to create a Label values variable', async () => {
|
test('Allows to create a Label values variable', async () => {
|
||||||
const onChange = jest.fn();
|
const onChange = jest.fn();
|
||||||
jest.spyOn(props.datasource, 'labelNamesQuery').mockResolvedValue([
|
|
||||||
{
|
|
||||||
text: 'moon',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: 'luna',
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
render(<LokiVariableQueryEditor {...props} onChange={onChange} />);
|
render(<LokiVariableQueryEditor {...props} onChange={onChange} />);
|
||||||
|
|
||||||
expect(onChange).not.toHaveBeenCalled();
|
expect(onChange).not.toHaveBeenCalled();
|
||||||
@ -77,14 +76,6 @@ describe('LokiVariableQueryEditor', () => {
|
|||||||
|
|
||||||
test('Allows to create a Label values variable with custom label', async () => {
|
test('Allows to create a Label values variable with custom label', async () => {
|
||||||
const onChange = jest.fn();
|
const onChange = jest.fn();
|
||||||
jest.spyOn(props.datasource, 'labelNamesQuery').mockResolvedValue([
|
|
||||||
{
|
|
||||||
text: 'moon',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: 'luna',
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
render(<LokiVariableQueryEditor {...props} onChange={onChange} />);
|
render(<LokiVariableQueryEditor {...props} onChange={onChange} />);
|
||||||
|
|
||||||
expect(onChange).not.toHaveBeenCalled();
|
expect(onChange).not.toHaveBeenCalled();
|
||||||
@ -106,12 +97,12 @@ describe('LokiVariableQueryEditor', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('Migrates legacy string queries to LokiVariableQuery instances', async () => {
|
test('Migrates legacy string queries to LokiVariableQuery instances', async () => {
|
||||||
const query = 'label_values(log stream selector, label_selector)';
|
const query = 'label_values(log stream selector, luna)';
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
render(<LokiVariableQueryEditor {...props} onChange={() => {}} query={query} />);
|
render(<LokiVariableQueryEditor {...props} onChange={() => {}} query={query} />);
|
||||||
|
|
||||||
await waitFor(() => expect(screen.getByText('Label values')).toBeInTheDocument());
|
await waitFor(() => expect(screen.getByText('Label values')).toBeInTheDocument());
|
||||||
await waitFor(() => expect(screen.getByText('label_selector')).toBeInTheDocument());
|
await waitFor(() => expect(screen.getByText('luna')).toBeInTheDocument());
|
||||||
await waitFor(() => expect(screen.getByDisplayValue('log stream selector')).toBeInTheDocument());
|
await waitFor(() => expect(screen.getByDisplayValue('log stream selector')).toBeInTheDocument());
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -122,7 +113,7 @@ describe('LokiVariableQueryEditor', () => {
|
|||||||
onChange={() => {}}
|
onChange={() => {}}
|
||||||
query={{
|
query={{
|
||||||
type: LokiVariableQueryType.LabelValues,
|
type: LokiVariableQueryType.LabelValues,
|
||||||
label: 'label_selector',
|
label: 'luna',
|
||||||
stream: 'log stream selector',
|
stream: 'log stream selector',
|
||||||
refId,
|
refId,
|
||||||
}}
|
}}
|
||||||
@ -130,7 +121,25 @@ describe('LokiVariableQueryEditor', () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
await waitFor(() => expect(screen.getByText('Label values')).toBeInTheDocument());
|
await waitFor(() => expect(screen.getByText('Label values')).toBeInTheDocument());
|
||||||
await waitFor(() => expect(screen.getByText('label_selector')).toBeInTheDocument());
|
await waitFor(() => expect(screen.getByText('luna')).toBeInTheDocument());
|
||||||
await waitFor(() => expect(screen.getByDisplayValue('log stream selector')).toBeInTheDocument());
|
await waitFor(() => expect(screen.getByDisplayValue('log stream selector')).toBeInTheDocument());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Label options are not lost when selecting one', async () => {
|
||||||
|
const { rerender } = render(<LokiVariableQueryEditor {...props} onChange={() => {}} />);
|
||||||
|
|
||||||
|
await selectOptionInTest(screen.getByLabelText('Query type'), 'Label values');
|
||||||
|
await selectOptionInTest(screen.getByLabelText('Label'), 'luna');
|
||||||
|
|
||||||
|
const updatedQuery = {
|
||||||
|
refId: 'test',
|
||||||
|
type: LokiVariableQueryType.LabelValues,
|
||||||
|
label: 'luna',
|
||||||
|
};
|
||||||
|
rerender(<LokiVariableQueryEditor {...props} query={updatedQuery} onChange={() => {}} />);
|
||||||
|
|
||||||
|
await selectOptionInTest(screen.getByLabelText('Label'), 'moon');
|
||||||
|
await selectOptionInTest(screen.getByLabelText('Label'), 'luna');
|
||||||
|
await screen.findByText('luna');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -31,10 +31,6 @@ export const LokiVariableQueryEditor = ({ onChange, query, datasource }: Props)
|
|||||||
setType(variableQuery.type);
|
setType(variableQuery.type);
|
||||||
setLabel(variableQuery.label || '');
|
setLabel(variableQuery.label || '');
|
||||||
setStream(variableQuery.stream || '');
|
setStream(variableQuery.stream || '');
|
||||||
|
|
||||||
if (variableQuery.label) {
|
|
||||||
setLabelOptions([{ label: variableQuery.label, value: variableQuery.label }]);
|
|
||||||
}
|
|
||||||
}, [query]);
|
}, [query]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
Loading…
Reference in New Issue
Block a user