Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Azure Monitor: Fix subscription selector when changing data sources (#…
- Loading branch information
1 parent
d797054
commit 2ce536b
Showing
2 changed files
with
120 additions
and
16 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...source/grafana-azure-monitor-datasource/components/ArgQueryEditor/ArgQueryEditor.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import createMockDatasource from '../../__mocks__/datasource'; | ||
import createMockQuery from '../../__mocks__/query'; | ||
|
||
import ArgQueryEditor from './ArgQueryEditor'; | ||
|
||
jest.mock('@grafana/runtime', () => ({ | ||
...(jest.requireActual('@grafana/runtime') as unknown as object), | ||
getTemplateSrv: () => ({ | ||
replace: (val: string) => { | ||
return val; | ||
}, | ||
}), | ||
})); | ||
|
||
const variableOptionGroup = { | ||
label: 'Template variables', | ||
options: [], | ||
}; | ||
|
||
const defaultProps = { | ||
query: createMockQuery(), | ||
datasource: createMockDatasource(), | ||
variableOptionGroup: variableOptionGroup, | ||
onChange: jest.fn(), | ||
setError: jest.fn(), | ||
}; | ||
|
||
describe('ArgQueryEditor', () => { | ||
it('should render', async () => { | ||
render(<ArgQueryEditor {...defaultProps} />); | ||
expect(await screen.findByTestId('azure-monitor-arg-query-editor-with-experimental-ui')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should select a subscription from the fetched array', async () => { | ||
const datasource = createMockDatasource({ | ||
getSubscriptions: jest.fn().mockResolvedValue([{ value: 'foo' }]), | ||
}); | ||
const onChange = jest.fn(); | ||
render(<ArgQueryEditor {...defaultProps} datasource={datasource} onChange={onChange} />); | ||
expect(await screen.findByTestId('azure-monitor-arg-query-editor-with-experimental-ui')).toBeInTheDocument(); | ||
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['foo'] })); | ||
}); | ||
|
||
it('should select a subscription from existing query', async () => { | ||
const onChange = jest.fn(); | ||
const query = createMockQuery({ | ||
subscriptions: ['bar'], | ||
}); | ||
render(<ArgQueryEditor {...defaultProps} onChange={onChange} query={query} />); | ||
expect(await screen.findByTestId('azure-monitor-arg-query-editor-with-experimental-ui')).toBeInTheDocument(); | ||
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['bar'] })); | ||
}); | ||
|
||
it('should change the subscription if the selected one is not part of the fetched array', async () => { | ||
const onChange = jest.fn(); | ||
const datasource = createMockDatasource({ | ||
getSubscriptions: jest.fn().mockResolvedValue([{ value: 'foo' }]), | ||
}); | ||
const query = createMockQuery({ | ||
subscriptions: ['bar'], | ||
}); | ||
render(<ArgQueryEditor {...defaultProps} datasource={datasource} onChange={onChange} query={query} />); | ||
expect(await screen.findByTestId('azure-monitor-arg-query-editor-with-experimental-ui')).toBeInTheDocument(); | ||
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['foo'] })); | ||
expect(onChange).not.toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['bar'] })); | ||
}); | ||
|
||
it('should keep a subset of subscriptions if the new list does not contain all the query subscriptions', async () => { | ||
const onChange = jest.fn(); | ||
const datasource = createMockDatasource({ | ||
getSubscriptions: jest.fn().mockResolvedValue([{ value: 'foo' }, { value: 'bar' }]), | ||
}); | ||
const query = createMockQuery({ | ||
subscriptions: ['foo', 'bar', 'foobar'], | ||
}); | ||
render(<ArgQueryEditor {...defaultProps} datasource={datasource} onChange={onChange} query={query} />); | ||
expect(await screen.findByTestId('azure-monitor-arg-query-editor-with-experimental-ui')).toBeInTheDocument(); | ||
expect(onChange).toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['foo', 'bar'] })); | ||
expect(onChange).not.toHaveBeenCalledWith(expect.objectContaining({ subscriptions: ['foo', 'bar', 'foobar'] })); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters