useAppMaintenanceCheck.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useNotifications } from '#shared/components/CommonNotifications/index.ts'
  3. import { ApplicationBuildChecksumDocument } from '#shared/graphql/queries/applicationBuildChecksum.api.ts'
  4. import { AppMaintenanceDocument } from '#shared/graphql/subscriptions/appMaintenance.api.ts'
  5. import { EnumAppMaintenanceType } from '#shared/graphql/types.ts'
  6. import { renderComponent } from '#tests/support/components/index.ts'
  7. import {
  8. type ExtendedIMockSubscription,
  9. mockGraphQLApi,
  10. mockGraphQLSubscription,
  11. } from '#tests/support/mock-graphql-api.ts'
  12. import useAppMaintenanceCheck from '../useAppMaintenanceCheck.ts'
  13. let subscriptionAppMaintenance: ExtendedIMockSubscription
  14. describe('useAppMaintenanceCheck', () => {
  15. beforeAll(() => {
  16. mockGraphQLApi(ApplicationBuildChecksumDocument).willResolve({
  17. applicationBuildChecksum: {
  18. applicationBuildChecksum: 'initial-checksum',
  19. },
  20. })
  21. subscriptionAppMaintenance = mockGraphQLSubscription(AppMaintenanceDocument)
  22. renderComponent(
  23. {
  24. template: '<div>App Maintenance Check</div>',
  25. setup() {
  26. useAppMaintenanceCheck()
  27. },
  28. },
  29. {
  30. router: true,
  31. unmount: false,
  32. },
  33. )
  34. })
  35. afterEach(() => {
  36. useNotifications().clearAllNotifications()
  37. })
  38. it('reacts to config_updated message', async () => {
  39. await subscriptionAppMaintenance.next({
  40. data: {
  41. appMaintenance: {
  42. type: EnumAppMaintenanceType.ConfigChanged,
  43. },
  44. },
  45. })
  46. const { notifications } = useNotifications()
  47. expect(notifications.value[0].message).toBe(
  48. 'The configuration of Zammad has changed. Please reload at your earliest.',
  49. )
  50. })
  51. it('reacts to app_version message', async () => {
  52. await subscriptionAppMaintenance.next({
  53. data: {
  54. appMaintenance: {
  55. type: EnumAppMaintenanceType.AppVersion,
  56. },
  57. },
  58. })
  59. const { notifications } = useNotifications()
  60. expect(notifications.value[0].message).toBe(
  61. 'A newer version of the app is available. Please reload at your earliest.',
  62. )
  63. })
  64. it('reacts to reload_auto message', async () => {
  65. await subscriptionAppMaintenance.next({
  66. data: {
  67. appMaintenance: {
  68. type: EnumAppMaintenanceType.RestartAuto,
  69. },
  70. },
  71. })
  72. const { notifications } = useNotifications()
  73. expect(notifications.value[0].message).toBe(
  74. 'A newer version of the app is available. Please reload at your earliest.',
  75. )
  76. })
  77. it('reacts to reload_manual message', async () => {
  78. await subscriptionAppMaintenance.next({
  79. data: {
  80. appMaintenance: {
  81. type: EnumAppMaintenanceType.RestartManual,
  82. },
  83. },
  84. })
  85. const { notifications } = useNotifications()
  86. expect(notifications.value[0].message).toBe(
  87. 'A newer version of the app is available. Please reload at your earliest.',
  88. )
  89. })
  90. it('does not raise unhandled exceptions if the payload structure is wrong', async (context) => {
  91. context.skipConsole = true
  92. vi.spyOn(console, 'log').mockClear()
  93. await subscriptionAppMaintenance.next({
  94. data: {
  95. pushMessages: {
  96. title: null,
  97. text: null,
  98. },
  99. },
  100. })
  101. expect(console.log).not.toHaveBeenCalledWith(
  102. 'Uncaught Exception',
  103. new TypeError("Cannot read properties of undefined (reading 'type')"),
  104. )
  105. })
  106. })