PlaylistPage.test.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { render, waitFor } from '@testing-library/react';
  2. import React from 'react';
  3. import { contextSrv } from 'app/core/services/context_srv';
  4. import { locationService } from '../../../../packages/grafana-runtime/src';
  5. import { PlaylistPage, PlaylistPageProps } from './PlaylistPage';
  6. const fnMock = jest.fn();
  7. jest.mock('@grafana/runtime', () => ({
  8. ...(jest.requireActual('@grafana/runtime') as unknown as object),
  9. getBackendSrv: () => ({
  10. get: fnMock,
  11. }),
  12. }));
  13. jest.mock('app/core/services/context_srv', () => ({
  14. contextSrv: {
  15. isEditor: true,
  16. },
  17. }));
  18. function getTestContext(propOverrides?: object) {
  19. const props: PlaylistPageProps = {
  20. navModel: {
  21. main: {
  22. text: 'Playlist',
  23. },
  24. node: {
  25. text: 'playlist',
  26. },
  27. },
  28. route: {
  29. path: '/playlists',
  30. component: jest.fn(),
  31. },
  32. queryParams: { state: 'ok' },
  33. match: { params: { name: 'playlist', sourceName: 'test playlist' }, isExact: false, url: 'asdf', path: '' },
  34. history: locationService.getHistory(),
  35. location: { pathname: '', hash: '', search: '', state: '' },
  36. };
  37. Object.assign(props, propOverrides);
  38. return render(<PlaylistPage {...props} />);
  39. }
  40. describe('PlaylistPage', () => {
  41. describe('when mounted without a playlist', () => {
  42. it('page should load', () => {
  43. fnMock.mockResolvedValue([]);
  44. const { getByText } = getTestContext();
  45. expect(getByText(/loading/i)).toBeInTheDocument();
  46. });
  47. it('then show empty list', async () => {
  48. const { getByText } = getTestContext();
  49. await waitFor(() => getByText('There are no playlists created yet'));
  50. });
  51. describe('and signed in user is not a viewer', () => {
  52. it('then create playlist button should not be disabled', async () => {
  53. contextSrv.isEditor = true;
  54. const { getByRole } = getTestContext();
  55. const createPlaylistButton = await waitFor(() => getByRole('link', { name: /create playlist/i }));
  56. expect(createPlaylistButton).not.toHaveStyle('pointer-events: none');
  57. });
  58. });
  59. describe('and signed in user is a viewer', () => {
  60. it('then create playlist button should be disabled', async () => {
  61. contextSrv.isEditor = false;
  62. const { getByRole } = getTestContext();
  63. const createPlaylistButton = await waitFor(() => getByRole('link', { name: /create playlist/i }));
  64. expect(createPlaylistButton).toHaveStyle('pointer-events: none');
  65. });
  66. });
  67. });
  68. describe('when mounted with a playlist', () => {
  69. it('page should load', () => {
  70. fnMock.mockResolvedValue([
  71. {
  72. id: 0,
  73. name: 'A test playlist',
  74. interval: '10m',
  75. items: [
  76. { title: 'First item', type: 'dashboard_by_id', order: 1, value: '1' },
  77. { title: 'Middle item', type: 'dashboard_by_id', order: 2, value: '2' },
  78. { title: 'Last item', type: 'dashboard_by_tag', order: 2, value: 'Last item' },
  79. ],
  80. uid: 'playlist-0',
  81. },
  82. ]);
  83. const { getByText } = getTestContext();
  84. expect(getByText(/loading/i)).toBeInTheDocument();
  85. });
  86. describe('and signed in user is not a viewer', () => {
  87. it('then playlist title and all playlist buttons should appear on the page', async () => {
  88. contextSrv.isEditor = true;
  89. const { getByRole, getByText } = getTestContext();
  90. await waitFor(() => getByText('A test playlist'));
  91. expect(getByRole('link', { name: /New playlist/i })).toBeInTheDocument();
  92. expect(getByRole('button', { name: /Start playlist/i })).toBeInTheDocument();
  93. expect(getByRole('link', { name: /Edit playlist/i })).toBeInTheDocument();
  94. expect(getByRole('button', { name: /Delete playlist/i })).toBeInTheDocument();
  95. });
  96. });
  97. describe('and signed in user is a viewer', () => {
  98. it('then playlist title and only start playlist button should appear on the page', async () => {
  99. contextSrv.isEditor = false;
  100. const { getByRole, getByText, queryByRole } = getTestContext();
  101. await waitFor(() => getByText('A test playlist'));
  102. expect(queryByRole('link', { name: /New playlist/i })).not.toBeInTheDocument();
  103. expect(getByRole('button', { name: /Start playlist/i })).toBeInTheDocument();
  104. expect(queryByRole('link', { name: /Edit playlist/i })).not.toBeInTheDocument();
  105. expect(queryByRole('button', { name: /Delete playlist/i })).not.toBeInTheDocument();
  106. });
  107. });
  108. });
  109. });