dateTime.spec.tsx 3.0 KB

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