2023-04-11 04:51:54 -05:00
|
|
|
import { render, screen, within } from '@testing-library/react';
|
2021-03-23 01:45:04 -05:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2022-04-22 08:33:13 -05:00
|
|
|
import React from 'react';
|
2021-03-23 01:45:04 -05:00
|
|
|
|
|
|
|
import { PlaylistForm } from './PlaylistForm';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { Playlist } from './types';
|
2021-03-23 01:45:04 -05:00
|
|
|
|
2022-09-05 22:40:01 -05:00
|
|
|
jest.mock('app/core/components/TagFilter/TagFilter', () => ({
|
2022-04-08 14:18:52 -05:00
|
|
|
TagFilter: () => {
|
|
|
|
return <>mocked-tag-filter</>;
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
2022-06-14 14:32:52 -05:00
|
|
|
function getTestContext({ name, interval, items, uid }: Partial<Playlist> = {}) {
|
2021-03-23 01:45:04 -05:00
|
|
|
const onSubmitMock = jest.fn();
|
2022-06-14 14:32:52 -05:00
|
|
|
const playlist = { name, items, interval, uid } as unknown as Playlist;
|
2021-03-23 01:45:04 -05:00
|
|
|
const { rerender } = render(<PlaylistForm onSubmit={onSubmitMock} playlist={playlist} />);
|
|
|
|
|
|
|
|
return { onSubmitMock, playlist, rerender };
|
|
|
|
}
|
|
|
|
|
|
|
|
const playlist: Playlist = {
|
|
|
|
name: 'A test playlist',
|
|
|
|
interval: '10m',
|
|
|
|
items: [
|
2022-09-05 22:40:01 -05:00
|
|
|
{ type: 'dashboard_by_uid', value: 'uid_1' },
|
|
|
|
{ type: 'dashboard_by_uid', value: 'uid_2' },
|
|
|
|
{ type: 'dashboard_by_tag', value: 'tag_A' },
|
2021-03-23 01:45:04 -05:00
|
|
|
],
|
2022-06-14 14:32:52 -05:00
|
|
|
uid: 'foo',
|
2021-03-23 01:45:04 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
function rows() {
|
2022-09-05 22:40:01 -05:00
|
|
|
return screen.getAllByRole('row');
|
2021-03-23 01:45:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('PlaylistForm', () => {
|
2022-09-05 22:40:01 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
|
|
});
|
|
|
|
|
2021-03-23 01:45:04 -05:00
|
|
|
describe('when mounted without playlist', () => {
|
|
|
|
it('then it should contain name and interval fields', () => {
|
|
|
|
getTestContext();
|
|
|
|
|
|
|
|
expect(screen.getByRole('textbox', { name: /playlist name/i })).toBeInTheDocument();
|
|
|
|
expect(screen.getByRole('textbox', { name: /playlist interval/i })).toBeInTheDocument();
|
2022-09-05 22:40:01 -05:00
|
|
|
expect(screen.queryByRole('row')).not.toBeInTheDocument();
|
2021-03-23 01:45:04 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('then name field should have empty string as default value', () => {
|
|
|
|
getTestContext();
|
|
|
|
|
|
|
|
expect(screen.getByRole('textbox', { name: /playlist name/i })).toHaveValue('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('then interval field should have 5m as default value', () => {
|
|
|
|
getTestContext();
|
|
|
|
|
|
|
|
expect(screen.getByRole('textbox', { name: /playlist interval/i })).toHaveValue('5m');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when mounted with a playlist', () => {
|
|
|
|
it('then name field should have correct value', () => {
|
|
|
|
getTestContext(playlist);
|
|
|
|
|
|
|
|
expect(screen.getByRole('textbox', { name: /playlist name/i })).toHaveValue('A test playlist');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('then interval field should have correct value', () => {
|
|
|
|
getTestContext(playlist);
|
|
|
|
|
|
|
|
expect(screen.getByRole('textbox', { name: /playlist interval/i })).toHaveValue('10m');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('then items row count should be correct', () => {
|
|
|
|
getTestContext(playlist);
|
|
|
|
|
2022-09-05 22:40:01 -05:00
|
|
|
expect(screen.getAllByRole('row')).toHaveLength(3);
|
2021-03-23 01:45:04 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('then the first item row should be correct', () => {
|
|
|
|
getTestContext(playlist);
|
|
|
|
|
2022-09-05 22:40:01 -05:00
|
|
|
expectCorrectRow({ index: 0, type: 'dashboard_by_uid', value: 'uid_1' });
|
|
|
|
expectCorrectRow({ index: 1, type: 'dashboard_by_uid', value: 'uid_2' });
|
|
|
|
expectCorrectRow({ index: 2, type: 'dashboard_by_tag', value: 'tag_A' });
|
2021-03-23 01:45:04 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when deleting a playlist item', () => {
|
2022-04-21 07:15:21 -05:00
|
|
|
it('then the item should be removed and other items should be correct', async () => {
|
2021-03-23 01:45:04 -05:00
|
|
|
getTestContext(playlist);
|
|
|
|
|
|
|
|
expect(rows()).toHaveLength(3);
|
2022-04-21 07:15:21 -05:00
|
|
|
await userEvent.click(within(rows()[2]).getByRole('button', { name: /delete playlist item/i }));
|
2021-03-23 01:45:04 -05:00
|
|
|
expect(rows()).toHaveLength(2);
|
2022-09-05 22:40:01 -05:00
|
|
|
expectCorrectRow({ index: 0, type: 'dashboard_by_uid', value: 'uid_1' });
|
|
|
|
expectCorrectRow({ index: 1, type: 'dashboard_by_uid', value: 'uid_2' });
|
2021-03-23 01:45:04 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when submitting the form', () => {
|
|
|
|
it('then the correct item should be submitted', async () => {
|
|
|
|
const { onSubmitMock } = getTestContext(playlist);
|
|
|
|
|
2022-06-08 04:39:36 -05:00
|
|
|
await userEvent.click(screen.getByRole('button', { name: /save/i }));
|
|
|
|
expect(onSubmitMock).toHaveBeenCalledTimes(1);
|
2022-06-14 14:32:52 -05:00
|
|
|
expect(onSubmitMock).toHaveBeenCalledWith({
|
2023-10-11 11:19:13 -05:00
|
|
|
uid: 'foo',
|
2022-06-14 14:32:52 -05:00
|
|
|
name: 'A test playlist',
|
|
|
|
interval: '10m',
|
|
|
|
items: [
|
2022-09-05 22:40:01 -05:00
|
|
|
{ type: 'dashboard_by_uid', value: 'uid_1' },
|
|
|
|
{ type: 'dashboard_by_uid', value: 'uid_2' },
|
|
|
|
{ type: 'dashboard_by_tag', value: 'tag_A' },
|
2022-06-14 14:32:52 -05:00
|
|
|
],
|
|
|
|
});
|
2021-03-23 01:45:04 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('and name is missing', () => {
|
|
|
|
it('then an alert should appear and nothing should be submitted', async () => {
|
|
|
|
const { onSubmitMock } = getTestContext({ ...playlist, name: undefined });
|
|
|
|
|
2022-06-08 04:39:36 -05:00
|
|
|
await userEvent.click(screen.getByRole('button', { name: /save/i }));
|
|
|
|
expect(screen.getAllByRole('alert')).toHaveLength(1);
|
2021-03-23 01:45:04 -05:00
|
|
|
expect(onSubmitMock).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('and interval is missing', () => {
|
|
|
|
it('then an alert should appear and nothing should be submitted', async () => {
|
|
|
|
const { onSubmitMock } = getTestContext(playlist);
|
|
|
|
|
2022-04-21 07:15:21 -05:00
|
|
|
await userEvent.clear(screen.getByRole('textbox', { name: /playlist interval/i }));
|
2022-06-08 04:39:36 -05:00
|
|
|
await userEvent.click(screen.getByRole('button', { name: /save/i }));
|
|
|
|
expect(screen.getAllByRole('alert')).toHaveLength(1);
|
2021-03-23 01:45:04 -05:00
|
|
|
expect(onSubmitMock).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when items are missing', () => {
|
|
|
|
it('then save button is disabled', async () => {
|
|
|
|
getTestContext({ ...playlist, items: [] });
|
|
|
|
|
|
|
|
expect(screen.getByRole('button', { name: /save/i })).toBeDisabled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
interface ExpectCorrectRowArgs {
|
|
|
|
index: number;
|
2022-09-05 22:40:01 -05:00
|
|
|
type: 'dashboard_by_tag' | 'dashboard_by_uid';
|
|
|
|
value: string;
|
2021-03-23 01:45:04 -05:00
|
|
|
}
|
|
|
|
|
2022-09-05 22:40:01 -05:00
|
|
|
function expectCorrectRow({ index, type, value }: ExpectCorrectRowArgs) {
|
2021-03-23 01:45:04 -05:00
|
|
|
const row = within(rows()[index]);
|
2022-09-05 22:40:01 -05:00
|
|
|
const cell = `Playlist item, ${type}, ${value}`;
|
2021-03-23 01:45:04 -05:00
|
|
|
const regex = new RegExp(cell, 'i');
|
|
|
|
expect(row.getByRole('cell', { name: regex })).toBeInTheDocument();
|
|
|
|
}
|