useEscalationState.spec.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { toRef, type ToRef } from 'vue'
  3. import { EscalationState, useEscalationState } from '../useEscalationState.ts'
  4. const getState = (value: ToRef<string | undefined | null>) => {
  5. return useEscalationState(value).value
  6. }
  7. describe('correctly returns the escalation state', () => {
  8. it('returns no state if undefined', () => {
  9. expect(getState(toRef(() => undefined))).toBe(EscalationState.None)
  10. })
  11. it('returns no state if incorrect date', () => {
  12. expect(getState(toRef(() => 't01.01.2012t'))).toBe(EscalationState.None)
  13. })
  14. it('returns warning if date is in the future', () => {
  15. expect(getState(toRef(() => new Date(2090, 1, 1).toISOString()))).toBe(
  16. EscalationState.Warning,
  17. )
  18. })
  19. it('returns escalated if date is in the past', () => {
  20. expect(getState(toRef(() => new Date(2000, 1, 1).toISOString()))).toBe(
  21. EscalationState.Escalated,
  22. )
  23. })
  24. it('reactively updates the value', async () => {
  25. const now = new Date()
  26. const nextSecond = new Date(now.getTime() + 1000).toISOString()
  27. const state = useEscalationState(toRef(() => nextSecond))
  28. expect(state.value).toBe(EscalationState.Warning)
  29. await vi.waitFor(() => {
  30. expect(state.value).toBe(EscalationState.Escalated)
  31. }, 10_000)
  32. }, 10_000)
  33. })