CommonDateTime.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { nextTick } from 'vue'
  3. import { renderComponent } from '#tests/support/components/index.ts'
  4. import { useApplicationStore } from '#shared/stores/application.ts'
  5. import CommonDateTime, { type Props } from '../CommonDateTime.vue'
  6. vi.hoisted(() => {
  7. vi.useFakeTimers().setSystemTime(new Date('2020-10-11T10:10:10Z'))
  8. })
  9. const dateTime = '2020-10-10T10:10:10Z'
  10. const renderDateTime = (props: Props, slots = {}) => {
  11. return renderComponent(CommonDateTime, {
  12. props: {
  13. ...props,
  14. },
  15. slots,
  16. store: true,
  17. })
  18. }
  19. describe('CommonDateTime.vue', () => {
  20. it('renders with type relative', async () => {
  21. const view = renderDateTime({ dateTime, type: 'relative' })
  22. expect(view.container).toHaveTextContent('1 day ago')
  23. })
  24. it('renders with prefix', async () => {
  25. const view = renderDateTime(
  26. { dateTime, type: 'relative' },
  27. { prefix: 'prefix-slot' },
  28. )
  29. expect(view.container).toHaveTextContent('prefix-slot 1 day ago')
  30. })
  31. it('renders with type absolute + absolute format "datetime" (default)', async () => {
  32. const view = renderDateTime({
  33. dateTime,
  34. type: 'absolute',
  35. })
  36. expect(view.container).toHaveTextContent('2020-10-10 10:10')
  37. })
  38. it('renders with type absolute + absolute format "datetime" (set via prop)', async () => {
  39. const view = renderDateTime({
  40. dateTime,
  41. type: 'absolute',
  42. absoluteFormat: 'datetime',
  43. })
  44. expect(view.container).toHaveTextContent('2020-10-10 10:10')
  45. })
  46. it('renders with type absolute + absolute format "date"', async () => {
  47. const view = renderDateTime({
  48. dateTime,
  49. type: 'absolute',
  50. absoluteFormat: 'date',
  51. })
  52. expect(view.container).toHaveTextContent('2020-10-10')
  53. })
  54. it('renders with type absolute (application store)', async () => {
  55. const view = renderDateTime({ dateTime, type: 'absolute' })
  56. useApplicationStore().config.pretty_date_format = 'absolute'
  57. await nextTick()
  58. expect(view.container).toHaveTextContent('2020-10-10')
  59. })
  60. it('renders with type configured + absolute format "date" (application store)', async () => {
  61. const view = renderDateTime({ dateTime, type: 'configured' })
  62. useApplicationStore().config.pretty_date_format = 'absolute'
  63. await nextTick()
  64. expect(view.container).toHaveTextContent('2020-10-10')
  65. })
  66. it('renders with type timestamp (application store)', async () => {
  67. const view = renderDateTime({ dateTime, type: 'configured' })
  68. useApplicationStore().config.pretty_date_format = 'timestamp'
  69. await nextTick()
  70. expect(view.container).toHaveTextContent('2020-10-10 10:10')
  71. })
  72. it('renders with type relative (application store)', async () => {
  73. const view = renderDateTime({ dateTime, type: 'configured' })
  74. useApplicationStore().config.pretty_date_format = 'relative'
  75. await nextTick()
  76. expect(view.container).toHaveTextContent('1 day ago')
  77. })
  78. })