mirror of https://github.com/grafana/grafana
Dashboard: Migration - EditVariable Settings: Implement Constant Variable (#80743)
Co-authored-by: Ivan Ortega <ivanortegaalba@gmail.com>pull/80787/head
parent
80395e43d8
commit
4b113f87f9
@ -0,0 +1,27 @@ |
||||
import React, { FormEvent } from 'react'; |
||||
|
||||
import { selectors } from '@grafana/e2e-selectors'; |
||||
|
||||
import { VariableLegend } from './VariableLegend'; |
||||
import { VariableTextField } from './VariableTextField'; |
||||
|
||||
interface ConstantVariableFormProps { |
||||
constantValue: string; |
||||
onChange: (event: FormEvent<HTMLInputElement>) => void; |
||||
} |
||||
|
||||
export function ConstantVariableForm({ onChange, constantValue }: ConstantVariableFormProps) { |
||||
return ( |
||||
<> |
||||
<VariableLegend>Constant options</VariableLegend> |
||||
<VariableTextField |
||||
defaultValue={constantValue} |
||||
name="Value" |
||||
placeholder="your metric prefix" |
||||
onBlur={onChange} |
||||
testId={selectors.pages.Dashboard.Settings.Variables.Edit.ConstantVariable.constantOptionsQueryInputV2} |
||||
width={30} |
||||
/> |
||||
</> |
||||
); |
||||
} |
@ -0,0 +1,50 @@ |
||||
import { render, screen } from '@testing-library/react'; |
||||
import userEvent from '@testing-library/user-event'; |
||||
import React from 'react'; |
||||
|
||||
import { ConstantVariable } from '@grafana/scenes'; |
||||
|
||||
import { ConstantVariableEditor } from './ConstantVariableEditor'; |
||||
|
||||
describe('ConstantVariableEditor', () => { |
||||
let constantVar: ConstantVariable; |
||||
beforeEach(async () => { |
||||
const result = await buildTestScene(); |
||||
constantVar = result.constantVar; |
||||
}); |
||||
|
||||
it('renders constant value', () => { |
||||
render(<ConstantVariableEditor variable={constantVar} />); |
||||
const input = screen.getByRole('textbox', { name: 'Value' }); |
||||
expect(input).toBeInTheDocument(); |
||||
expect(input).toHaveValue('constant value'); |
||||
}); |
||||
|
||||
it('changes the value', async () => { |
||||
render(<ConstantVariableEditor variable={constantVar} />); |
||||
|
||||
const input = screen.getByRole('textbox', { name: 'Value' }); |
||||
expect(input).toBeInTheDocument(); |
||||
expect(input).toHaveValue('constant value'); |
||||
|
||||
// change input value
|
||||
const newValue = 'new constant value'; |
||||
await userEvent.clear(input); |
||||
await userEvent.type(input, newValue); |
||||
|
||||
expect(input).toHaveValue(newValue); |
||||
|
||||
await userEvent.tab(); |
||||
expect(constantVar.state.value).toBe(newValue); |
||||
}); |
||||
}); |
||||
|
||||
async function buildTestScene() { |
||||
const constantVar = new ConstantVariable({ |
||||
name: 'constantVar', |
||||
type: 'constant', |
||||
value: 'constant value', |
||||
}); |
||||
|
||||
return { constantVar }; |
||||
} |
Loading…
Reference in new issue