ReportsSettingsPage.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { render, fireEvent, screen, waitFor } from '@testing-library/react';
  2. import React from 'react';
  3. import { NavModel } from '@grafana/data';
  4. import { Props, ReportsSettingsPage } from './ReportsSettingsPage';
  5. beforeEach(() => {
  6. jest.clearAllMocks();
  7. });
  8. const mockPost = jest.fn(() => {
  9. return Promise.resolve([]);
  10. });
  11. jest.mock('@grafana/runtime', () => {
  12. return {
  13. getBackendSrv() {
  14. return {
  15. get: (url: string) => {
  16. return {
  17. branding: {
  18. emailFooterLink: 'https://footer-link.com',
  19. emailFooterMode: 'sent-by',
  20. emailFooterText: 'Test',
  21. emailLogoUrl: 'https://email-logo.jpg',
  22. reportLogoUrl: 'https://report-logo.jpg',
  23. },
  24. id: 0,
  25. orgId: 1,
  26. userId: 1,
  27. };
  28. },
  29. post: mockPost,
  30. };
  31. },
  32. locationService: {
  33. push: jest.fn(),
  34. },
  35. config: {
  36. buildInfo: {},
  37. licenseInfo: {},
  38. rendererAvailable: true,
  39. },
  40. };
  41. });
  42. jest.mock('@grafana/runtime/src/config', () => ({
  43. config: {
  44. buildInfo: {},
  45. licenseInfo: {},
  46. rendererAvailable: true,
  47. },
  48. }));
  49. jest.mock('app/core/core', () => {
  50. return {
  51. contextSrv: {
  52. hasPermission: () => true,
  53. },
  54. };
  55. });
  56. const setup = (propOverrides?: Partial<Props>) => {
  57. const props: Props = {
  58. navModel: { node: {}, main: {} } as NavModel,
  59. };
  60. Object.assign(props, propOverrides);
  61. render(<ReportsSettingsPage {...props} />);
  62. };
  63. describe('ReportsSettingPage', () => {
  64. it('should render existing settings', async () => {
  65. setup();
  66. expect(await screen.findAllByPlaceholderText('http://your.site/logo.png')).toHaveLength(2);
  67. expect(screen.getByLabelText(/report logo/i)).toHaveAttribute('value', 'https://report-logo.jpg');
  68. expect(screen.getByLabelText(/email logo/i)).toHaveAttribute('value', 'https://email-logo.jpg');
  69. expect(screen.getByRole('radio', { name: /sent by/i })).toBeChecked();
  70. expect(screen.getByRole('textbox', { name: /footer link text/i })).toHaveAttribute('value', 'Test');
  71. expect(screen.getByRole('textbox', { name: /footer link url/i })).toHaveAttribute(
  72. 'value',
  73. 'https://footer-link.com'
  74. );
  75. });
  76. it('should hide footer link and text if footer mode is None', async () => {
  77. setup();
  78. expect(await screen.findByRole('radio', { name: /sent by/i })).toBeChecked();
  79. expect(screen.getByRole('textbox', { name: /footer link text/i })).toBeInTheDocument();
  80. expect(screen.getByRole('textbox', { name: /footer link url/i })).toBeInTheDocument();
  81. const none = screen.getByRole('radio', { name: /none/i });
  82. fireEvent.click(none);
  83. expect(await screen.findByRole('radio', { name: /none/i })).toBeChecked();
  84. expect(screen.queryByRole('textbox', { name: /footer link text/i })).not.toBeInTheDocument();
  85. expect(screen.queryByRole('textbox', { name: /footer link url/i })).not.toBeInTheDocument();
  86. });
  87. it('should update the form fields on change', async () => {
  88. setup();
  89. const reportLogo = await screen.findByLabelText(/report logo/i);
  90. fireEvent.input(reportLogo, { target: { value: 'http://new-logo.png' } });
  91. fireEvent.input(screen.getByRole('textbox', { name: /footer link text/i }), { target: { value: 'New company' } });
  92. fireEvent.submit(screen.getByRole('button', { name: 'Save' }));
  93. await waitFor(() => {
  94. expect(mockPost).toHaveBeenCalledWith(
  95. '/api/reports/settings',
  96. expect.objectContaining({
  97. branding: {
  98. emailFooterLink: 'https://footer-link.com',
  99. emailFooterMode: 'sent-by',
  100. emailFooterText: 'New company',
  101. emailLogoUrl: 'https://email-logo.jpg',
  102. reportLogoUrl: 'http://new-logo.png',
  103. },
  104. })
  105. );
  106. });
  107. });
  108. });