utils.spec.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { ApolloError } from '@apollo/client/errors'
  3. import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'
  4. import UserError from '#shared/errors/UserError.ts'
  5. import { handleUserErrors } from '#shared/errors/utils.ts'
  6. describe('errpr utils', () => {
  7. describe('handleUserErrors', () => {
  8. beforeEach(() => {
  9. useNotifications().clearAllNotifications()
  10. })
  11. it('displays a error toast for a UserError', () => {
  12. const userErrors = [
  13. {
  14. field: null,
  15. message: 'Example error message',
  16. },
  17. {
  18. field: 'id',
  19. message: 'Id field is wrong',
  20. },
  21. ]
  22. const userErrorObject = new UserError(userErrors)
  23. handleUserErrors(userErrorObject)
  24. const { notifications } = useNotifications()
  25. expect(notifications.value.length).toBe(1)
  26. expect(notifications.value[0].message).toBe('Example error message')
  27. })
  28. it('ignore npne UserErrors', () => {
  29. handleUserErrors(new ApolloError({ errorMessage: 'Some error' }))
  30. const { notifications } = useNotifications()
  31. expect(notifications.value.length).toBe(0)
  32. })
  33. })
  34. })