dateTime.spec.jsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {User} from 'fixtures/js-stubs/user';
  2. import {render, screen} from 'sentry-test/reactTestingLibrary';
  3. import DateTime from 'sentry/components/dateTime';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. describe('DateTime', () => {
  6. const user = {
  7. ...User(),
  8. options: {
  9. clock24Hours: false,
  10. timezone: 'America/Los_Angeles',
  11. },
  12. };
  13. beforeAll(() => {
  14. ConfigStore.loadInitialData({user});
  15. });
  16. it('renders a date', () => {
  17. render(<DateTime date={new Date()} />);
  18. expect(screen.getByText('Oct 16, 7:41 PM')).toBeInTheDocument();
  19. });
  20. it('renders a date and shows the year if it is outside the current year', () => {
  21. const date = new Date();
  22. date.setYear(2016);
  23. date.setMonth(11);
  24. date.setDate(31);
  25. render(<DateTime date={date} />);
  26. expect(screen.getByText('Dec 31, 2016 7:41 PM')).toBeInTheDocument();
  27. });
  28. it('renders only the time', () => {
  29. render(<DateTime date={new Date()} timeOnly />);
  30. expect(screen.getByText('7:41 PM')).toBeInTheDocument();
  31. });
  32. it('renders only the date', () => {
  33. render(<DateTime date={new Date()} dateOnly />);
  34. expect(screen.getByText('Oct 16')).toBeInTheDocument();
  35. });
  36. it('renders a date with year', () => {
  37. render(<DateTime date={new Date()} year />);
  38. expect(screen.getByText('Oct 16, 2017 7:41 PM')).toBeInTheDocument();
  39. });
  40. it('renders a date with seconds', () => {
  41. render(<DateTime date={new Date()} seconds />);
  42. expect(screen.getByText('Oct 16, 7:41:20 PM')).toBeInTheDocument();
  43. });
  44. it('renders a date with the time zone', () => {
  45. render(<DateTime date={new Date()} timeZone />);
  46. expect(screen.getByText('Oct 16, 7:41 PM PDT')).toBeInTheDocument();
  47. });
  48. it('renders date with forced utc', () => {
  49. render(<DateTime date={new Date()} utc />);
  50. expect(screen.getByText('Oct 17, 2:41 AM UTC')).toBeInTheDocument();
  51. });
  52. describe('24 Hours', () => {
  53. beforeAll(() => {
  54. user.options.clock24Hours = true;
  55. ConfigStore.set('user', user);
  56. });
  57. afterAll(() => {
  58. user.options.clock24Hours = false;
  59. ConfigStore.set('user', user);
  60. });
  61. it('renders a date', () => {
  62. render(<DateTime date={new Date()} />);
  63. expect(screen.getByText('Oct 16, 19:41')).toBeInTheDocument();
  64. });
  65. it('renders only the time', () => {
  66. render(<DateTime date={new Date()} timeOnly />);
  67. expect(screen.getByText('19:41')).toBeInTheDocument();
  68. });
  69. it('renders date with forced utc', () => {
  70. render(<DateTime date={new Date()} utc />);
  71. expect(screen.getByText('Oct 17, 02:41 UTC')).toBeInTheDocument();
  72. });
  73. });
  74. });