GeneralSettings.test.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { render, screen } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import React from 'react';
  4. import { selectOptionInTest } from 'test/helpers/selectOptionInTest';
  5. import { byRole } from 'testing-library-selector';
  6. import { selectors } from '@grafana/e2e-selectors';
  7. import { setBackendSrv } from '@grafana/runtime';
  8. import { DashboardModel } from '../../state';
  9. import { GeneralSettingsUnconnected as GeneralSettings, Props } from './GeneralSettings';
  10. setBackendSrv({
  11. get: jest.fn().mockResolvedValue([]),
  12. } as any);
  13. const setupTestContext = (options: Partial<Props>) => {
  14. const defaults: Props = {
  15. dashboard: new DashboardModel(
  16. {
  17. title: 'test dashboard title',
  18. description: 'test dashboard description',
  19. timepicker: {
  20. refresh_intervals: ['5s', '10s', '30s', '1m', '5m', '15m', '30m', '1h', '2h', '1d', '2d'],
  21. time_options: ['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d'],
  22. },
  23. timezone: 'utc',
  24. },
  25. {
  26. folderId: 1,
  27. folderTitle: 'test',
  28. }
  29. ),
  30. updateTimeZone: jest.fn(),
  31. updateWeekStart: jest.fn(),
  32. };
  33. const props = { ...defaults, ...options };
  34. const { rerender } = render(<GeneralSettings {...props} />);
  35. return { rerender, props };
  36. };
  37. describe('General Settings', () => {
  38. describe('when component is mounted with timezone', () => {
  39. it('should render correctly', async () => {
  40. setupTestContext({});
  41. screen.getByDisplayValue('test dashboard title');
  42. screen.getByDisplayValue('test dashboard description');
  43. expect(await screen.findByTestId(selectors.components.TimeZonePicker.containerV2)).toHaveTextContent(
  44. 'Coordinated Universal Time'
  45. );
  46. });
  47. });
  48. describe('when timezone is changed', () => {
  49. it('should call update function', async () => {
  50. const { props } = setupTestContext({});
  51. await userEvent.click(screen.getByTestId(selectors.components.TimeZonePicker.containerV2));
  52. const timeZonePicker = screen.getByTestId(selectors.components.TimeZonePicker.containerV2);
  53. await userEvent.click(byRole('combobox').get(timeZonePicker));
  54. await selectOptionInTest(timeZonePicker, 'Browser Time');
  55. expect(props.updateTimeZone).toHaveBeenCalledWith('browser');
  56. expect(props.dashboard.timezone).toBe('browser');
  57. });
  58. });
  59. });