Files
grafana/public/app/core/components/Layers/LayerName.test.tsx
Ashley Harrison 47f8717149 React: Use new JSX transform (#88802)
* update eslint, tsconfig + esbuild to handle new jsx transform

* remove thing that breaks the new jsx transform

* remove react imports

* adjust grafana-icons build

* is this the correct syntax?

* try this

* well this was much easier than expected...

* change grafana-plugin-configs webpack config

* fixes

* fix lockfile

* fix 2 more violations

* use path.resolve instead of require.resolve

* remove react import

* fix react imports

* more fixes

* remove React import

* remove import React from docs

* remove another react import
2024-06-25 12:43:47 +01:00

61 lines
1.8 KiB
TypeScript

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { LayerNameProps, LayerName } from './LayerName';
describe('LayerName', () => {
it('Can edit title', async () => {
const scenario = renderScenario({});
await userEvent.click(screen.getByTestId('layer-name-div'));
const input = screen.getByTestId('layer-name-input');
await userEvent.clear(input);
await userEvent.type(input, 'new name');
// blur the element
await userEvent.click(document.body);
expect(jest.mocked(scenario.props.onChange).mock.calls[0][0]).toBe('new name');
});
it('Show error when empty name is specified', async () => {
renderScenario({});
await userEvent.click(screen.getByTestId('layer-name-div'));
const input = screen.getByTestId('layer-name-input');
await userEvent.clear(input);
const alert = await screen.findByRole('alert');
expect(alert.textContent).toBe('An empty layer name is not allowed');
});
it('Show error when other layer with same name exists', async () => {
renderScenario({});
await userEvent.click(screen.getByTestId('layer-name-div'));
const input = screen.getByTestId('layer-name-input');
await userEvent.clear(input);
await userEvent.type(input, 'Layer 2');
const alert = await screen.findByRole('alert');
expect(alert.textContent).toBe('Layer name already exists');
});
function renderScenario(overrides: Partial<LayerNameProps>) {
const props: LayerNameProps = {
name: 'Layer 1',
onChange: jest.fn(),
verifyLayerNameUniqueness: (nameToCheck: string) => {
const names = new Set(['Layer 1', 'Layer 2']);
return !names.has(nameToCheck);
},
};
Object.assign(props, overrides);
return {
props,
renderResult: render(<LayerName {...props} />),
};
}
});