|
|
|
@ -17,6 +17,15 @@ import { ResourceRowType } from './types'; |
|
|
|
|
|
|
|
|
|
import ResourcePicker from '.'; |
|
|
|
|
|
|
|
|
|
jest.mock('@grafana/runtime', () => ({ |
|
|
|
|
...(jest.requireActual('@grafana/runtime') as unknown as object), |
|
|
|
|
getTemplateSrv: () => ({ |
|
|
|
|
replace: (val: string) => { |
|
|
|
|
return val; |
|
|
|
|
}, |
|
|
|
|
}), |
|
|
|
|
})); |
|
|
|
|
|
|
|
|
|
const noResourceURI = ''; |
|
|
|
|
const singleSubscriptionSelectionURI = '/subscriptions/def-456'; |
|
|
|
|
const singleResourceGroupSelectionURI = '/subscriptions/def-456/resourceGroups/dev-3'; |
|
|
|
@ -50,7 +59,7 @@ const queryType: ResourcePickerQueryType = 'logs'; |
|
|
|
|
|
|
|
|
|
const defaultProps = { |
|
|
|
|
templateVariables: [], |
|
|
|
|
resource: noResourceURI, |
|
|
|
|
resources: [noResourceURI], |
|
|
|
|
resourcePickerData: createMockResourcePickerData(), |
|
|
|
|
onCancel: noop, |
|
|
|
|
onApply: noop, |
|
|
|
@ -68,7 +77,7 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
window.HTMLElement.prototype.scrollIntoView = jest.fn(); |
|
|
|
|
}); |
|
|
|
|
it('should pre-load subscriptions when there is no existing selection', async () => { |
|
|
|
|
render(<ResourcePicker {...defaultProps} resource={noResourceURI} />); |
|
|
|
|
render(<ResourcePicker {...defaultProps} resources={[noResourceURI]} />); |
|
|
|
|
const subscriptionCheckbox = await screen.findByLabelText('Primary Subscription'); |
|
|
|
|
expect(subscriptionCheckbox).toBeInTheDocument(); |
|
|
|
|
expect(subscriptionCheckbox).not.toBeChecked(); |
|
|
|
@ -77,7 +86,7 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should show a subscription as selected if there is one saved', async () => { |
|
|
|
|
render(<ResourcePicker {...defaultProps} resource={singleSubscriptionSelectionURI} />); |
|
|
|
|
render(<ResourcePicker {...defaultProps} resources={[singleSubscriptionSelectionURI]} />); |
|
|
|
|
const subscriptionCheckboxes = await screen.findAllByLabelText('Dev Subscription'); |
|
|
|
|
expect(subscriptionCheckboxes.length).toBe(2); |
|
|
|
|
expect(subscriptionCheckboxes[0]).toBeChecked(); |
|
|
|
@ -85,7 +94,7 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should show a resourceGroup as selected if there is one saved', async () => { |
|
|
|
|
render(<ResourcePicker {...defaultProps} resource={singleResourceGroupSelectionURI} />); |
|
|
|
|
render(<ResourcePicker {...defaultProps} resources={[singleResourceGroupSelectionURI]} />); |
|
|
|
|
const resourceGroupCheckboxes = await screen.findAllByLabelText('A Great Resource Group'); |
|
|
|
|
expect(resourceGroupCheckboxes.length).toBe(2); |
|
|
|
|
expect(resourceGroupCheckboxes[0]).toBeChecked(); |
|
|
|
@ -93,7 +102,7 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should show scroll down to a resource and mark it as selected if there is one saved', async () => { |
|
|
|
|
render(<ResourcePicker {...defaultProps} resource={singleResourceSelectionURI} />); |
|
|
|
|
render(<ResourcePicker {...defaultProps} resources={[singleResourceSelectionURI]} />); |
|
|
|
|
const resourceCheckboxes = await screen.findAllByLabelText('db-server'); |
|
|
|
|
expect(resourceCheckboxes.length).toBe(2); |
|
|
|
|
expect(resourceCheckboxes[0]).toBeChecked(); |
|
|
|
@ -101,7 +110,7 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('opens the selected nested resources', async () => { |
|
|
|
|
render(<ResourcePicker {...defaultProps} resource={singleResourceSelectionURI} />); |
|
|
|
|
render(<ResourcePicker {...defaultProps} resources={[singleResourceSelectionURI]} />); |
|
|
|
|
const collapseSubscriptionBtn = await screen.findByLabelText('Collapse Dev Subscription'); |
|
|
|
|
expect(collapseSubscriptionBtn).toBeInTheDocument(); |
|
|
|
|
const collapseResourceGroupBtn = await screen.findByLabelText('Collapse A Great Resource Group'); |
|
|
|
@ -109,7 +118,7 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('scrolls down to the selected resource', async () => { |
|
|
|
|
render(<ResourcePicker {...defaultProps} resource={singleResourceSelectionURI} />); |
|
|
|
|
render(<ResourcePicker {...defaultProps} resources={[singleResourceSelectionURI]} />); |
|
|
|
|
await screen.findByLabelText('Collapse A Great Resource Group'); |
|
|
|
|
expect(window.HTMLElement.prototype.scrollIntoView).toBeCalledTimes(1); |
|
|
|
|
}); |
|
|
|
@ -133,12 +142,40 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
const applyButton = screen.getByRole('button', { name: 'Apply' }); |
|
|
|
|
applyButton.click(); |
|
|
|
|
expect(onApply).toBeCalledTimes(1); |
|
|
|
|
expect(onApply).toBeCalledWith('/subscriptions/def-123'); |
|
|
|
|
expect(onApply).toBeCalledWith(['/subscriptions/def-123']); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should call onApply removing an element', async () => { |
|
|
|
|
const onApply = jest.fn(); |
|
|
|
|
render(<ResourcePicker {...defaultProps} resources={['/subscriptions/def-123']} onApply={onApply} />); |
|
|
|
|
const subscriptionCheckbox = await screen.findAllByLabelText('Primary Subscription'); |
|
|
|
|
expect(subscriptionCheckbox).toHaveLength(2); |
|
|
|
|
expect(subscriptionCheckbox.at(0)).toBeChecked(); |
|
|
|
|
subscriptionCheckbox.at(0)?.click(); |
|
|
|
|
const applyButton = screen.getByRole('button', { name: 'Apply' }); |
|
|
|
|
applyButton.click(); |
|
|
|
|
expect(onApply).toBeCalledTimes(1); |
|
|
|
|
expect(onApply).toBeCalledWith([]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should call onApply removing an element ignoring the case', async () => { |
|
|
|
|
const onApply = jest.fn(); |
|
|
|
|
render( |
|
|
|
|
<ResourcePicker {...defaultProps} resources={['/subscriptions/def-456/resourceGroups/DEV-3']} onApply={onApply} /> |
|
|
|
|
); |
|
|
|
|
const subscriptionCheckbox = await screen.findAllByLabelText('A Great Resource Group'); |
|
|
|
|
expect(subscriptionCheckbox).toHaveLength(2); |
|
|
|
|
expect(subscriptionCheckbox.at(0)).toBeChecked(); |
|
|
|
|
subscriptionCheckbox.at(0)?.click(); |
|
|
|
|
const applyButton = screen.getByRole('button', { name: 'Apply' }); |
|
|
|
|
applyButton.click(); |
|
|
|
|
expect(onApply).toBeCalledTimes(1); |
|
|
|
|
expect(onApply).toBeCalledWith([]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should call onApply with a new subscription when a user clicks on the checkbox in the row', async () => { |
|
|
|
|
const onApply = jest.fn(); |
|
|
|
|
render(<ResourcePicker {...defaultProps} onApply={onApply} resource={{}} />); |
|
|
|
|
render(<ResourcePicker {...defaultProps} onApply={onApply} resources={[]} />); |
|
|
|
|
const subscriptionCheckbox = await screen.findByLabelText('Primary Subscription'); |
|
|
|
|
expect(subscriptionCheckbox).toBeInTheDocument(); |
|
|
|
|
expect(subscriptionCheckbox).not.toBeChecked(); |
|
|
|
@ -146,7 +183,20 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
const applyButton = screen.getByRole('button', { name: 'Apply' }); |
|
|
|
|
applyButton.click(); |
|
|
|
|
expect(onApply).toBeCalledTimes(1); |
|
|
|
|
expect(onApply).toBeCalledWith({ subscription: 'def-123' }); |
|
|
|
|
expect(onApply).toBeCalledWith([{ subscription: 'def-123' }]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should call onApply removing a resource element', async () => { |
|
|
|
|
const onApply = jest.fn(); |
|
|
|
|
render(<ResourcePicker {...defaultProps} onApply={onApply} resources={[{ subscription: 'def-123' }]} />); |
|
|
|
|
const subscriptionCheckbox = await screen.findAllByLabelText('Primary Subscription'); |
|
|
|
|
expect(subscriptionCheckbox).toHaveLength(2); |
|
|
|
|
expect(subscriptionCheckbox.at(0)).toBeChecked(); |
|
|
|
|
subscriptionCheckbox.at(0)?.click(); |
|
|
|
|
const applyButton = screen.getByRole('button', { name: 'Apply' }); |
|
|
|
|
applyButton.click(); |
|
|
|
|
expect(onApply).toBeCalledTimes(1); |
|
|
|
|
expect(onApply).toBeCalledWith([]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should call onApply with a new subscription uri when a user types it in the selection box', async () => { |
|
|
|
@ -166,12 +216,12 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
applyButton.click(); |
|
|
|
|
|
|
|
|
|
expect(onApply).toBeCalledTimes(1); |
|
|
|
|
expect(onApply).toBeCalledWith('/subscriptions/def-123'); |
|
|
|
|
expect(onApply).toBeCalledWith(['/subscriptions/def-123']); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should call onApply with a new subscription when a user types it in the selection box', async () => { |
|
|
|
|
const onApply = jest.fn(); |
|
|
|
|
render(<ResourcePicker {...defaultProps} onApply={onApply} resource={{}} />); |
|
|
|
|
render(<ResourcePicker {...defaultProps} onApply={onApply} resources={[{}]} />); |
|
|
|
|
const subscriptionCheckbox = await screen.findByLabelText('Primary Subscription'); |
|
|
|
|
expect(subscriptionCheckbox).toBeInTheDocument(); |
|
|
|
|
expect(subscriptionCheckbox).not.toBeChecked(); |
|
|
|
@ -186,11 +236,11 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
applyButton.click(); |
|
|
|
|
|
|
|
|
|
expect(onApply).toBeCalledTimes(1); |
|
|
|
|
expect(onApply).toBeCalledWith({ subscription: 'def-123' }); |
|
|
|
|
expect(onApply).toBeCalledWith([{ subscription: 'def-123' }]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('should show unselect a subscription if the value is manually edited', async () => { |
|
|
|
|
render(<ResourcePicker {...defaultProps} resource={{ subscription: 'def-456' }} />); |
|
|
|
|
render(<ResourcePicker {...defaultProps} resources={[{ subscription: 'def-456' }]} />); |
|
|
|
|
const subscriptionCheckboxes = await screen.findAllByLabelText('Dev Subscription'); |
|
|
|
|
expect(subscriptionCheckboxes.length).toBe(2); |
|
|
|
|
expect(subscriptionCheckboxes[0]).toBeChecked(); |
|
|
|
@ -264,7 +314,7 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('resets result when the user clears their search', async () => { |
|
|
|
|
render(<ResourcePicker {...defaultProps} resource={noResourceURI} />); |
|
|
|
|
render(<ResourcePicker {...defaultProps} resources={[noResourceURI]} />); |
|
|
|
|
const subscriptionCheckboxBeforeSearch = await screen.findByLabelText('Primary Subscription'); |
|
|
|
|
expect(subscriptionCheckboxBeforeSearch).toBeInTheDocument(); |
|
|
|
|
|
|
|
|
@ -295,7 +345,7 @@ describe('AzureMonitor ResourcePicker', () => { |
|
|
|
|
{...defaultProps} |
|
|
|
|
queryType={'metrics'} |
|
|
|
|
resourcePickerData={resourcePickerData} |
|
|
|
|
resource={noResourceURI} |
|
|
|
|
resources={[noResourceURI]} |
|
|
|
|
/> |
|
|
|
|
); |
|
|
|
|
const subscriptionExpand = await screen.findByLabelText('Expand Primary Subscription'); |
|
|
|
|