Browse Source

Maintenance: Fix date picker test on month switches (when the first day in the new month is on Monday).

Dominik Klein 2 years ago
parent
commit
292e15f79c

+ 19 - 4
app/frontend/shared/components/Form/fields/FieldDate/__tests__/FieldDateTime.spec.ts

@@ -1,11 +1,15 @@
 // Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
 // Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
 /* eslint-disable import/first */
 /* eslint-disable import/first */
 
 
+const now = new Date('2021-04-13T11:10:10Z')
+vi.useFakeTimers().setSystemTime(now)
+
 import { FormKit } from '@formkit/vue'
 import { FormKit } from '@formkit/vue'
 import { waitFor } from '@testing-library/vue'
 import { waitFor } from '@testing-library/vue'
 import flatpickr from 'flatpickr'
 import flatpickr from 'flatpickr'
 import { i18n } from '@shared/i18n'
 import { i18n } from '@shared/i18n'
 import { renderComponent } from '@tests/support/components'
 import { renderComponent } from '@tests/support/components'
+import { beforeAll } from 'vitest'
 
 
 const renderDateField = (props: Record<string, unknown> = {}) => {
 const renderDateField = (props: Record<string, unknown> = {}) => {
   return renderComponent(FormKit, {
   return renderComponent(FormKit, {
@@ -20,15 +24,19 @@ const renderDateField = (props: Record<string, unknown> = {}) => {
   })
   })
 }
 }
 
 
-// I've tried mocking date, but it breaks flatpickr for some reason
-// so, instead I am relying on current date
-const now = new Date()
-
 describe('Fields - FieldDate - type "date"', () => {
 describe('Fields - FieldDate - type "date"', () => {
   beforeEach(() => {
   beforeEach(() => {
     i18n.setTranslationMap(new Map())
     i18n.setTranslationMap(new Map())
   })
   })
 
 
+  beforeAll(() => {
+    vi.useFakeTimers().setSystemTime(now)
+  })
+
+  afterAll(() => {
+    vi.useRealTimers()
+  })
+
   it('renders input and allows selecting date', async () => {
   it('renders input and allows selecting date', async () => {
     const view = renderDateField()
     const view = renderDateField()
 
 
@@ -238,6 +246,8 @@ describe('Fields - FieldDate - type "date"', () => {
   })
   })
 })
 })
 
 
+// Mocking date breaks flatpickr for some reason so, instead we relying
+// on current date for the interaction tests.
 describe('Fields - FieldDate - visuals', () => {
 describe('Fields - FieldDate - visuals', () => {
   it('calendar visibility changes based on interaction', async () => {
   it('calendar visibility changes based on interaction', async () => {
     const view = renderDateField()
     const view = renderDateField()
@@ -269,7 +279,12 @@ describe('Fields - FieldDate - visuals', () => {
 })
 })
 
 
 describe('Fields - FieldDate - type "datetime"', () => {
 describe('Fields - FieldDate - type "datetime"', () => {
+  beforeAll(() => {
+    vi.useFakeTimers().setSystemTime(now)
+  })
+
   afterAll(() => {
   afterAll(() => {
+    vi.useRealTimers()
     i18n.setTranslationMap(new Map())
     i18n.setTranslationMap(new Map())
   })
   })