install-pwa.spec.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed } from 'vue'
  3. import { visitView } from '#tests/support/components/visitView.ts'
  4. import * as utilsBrowser from '#shared/utils/browser.ts'
  5. import * as utilsPWA from '#shared/utils/pwa.ts'
  6. import type { Mock } from 'vitest'
  7. const utilsPWAmock = vi.mocked(utilsPWA)
  8. const utilsBrowsermock = vi.mocked(utilsBrowser)
  9. vi.mock('#shared/utils/browser.ts')
  10. vi.mock('#shared/utils/pwa.ts')
  11. const mockPWA = ({
  12. canInstallPWA = false,
  13. isStandalone = false,
  14. installPWA = vi.fn(),
  15. }: {
  16. canInstallPWA?: boolean
  17. isStandalone?: boolean
  18. installPWA?: Mock
  19. }) => {
  20. utilsPWAmock.usePWASupport.mockReturnValue({
  21. canInstallPWA: computed(() => canInstallPWA),
  22. installPWA,
  23. })
  24. vi.spyOn(utilsPWA, 'isStandalone').mockReturnValue(isStandalone)
  25. }
  26. describe('Installing Zammad as PWA', () => {
  27. test("cannot install zammad as PWA, so user doesn't see a button", async () => {
  28. mockPWA({ canInstallPWA: false, isStandalone: false })
  29. const view = await visitView('/account')
  30. expect(view.queryByText('Install')).not.toBeInTheDocument()
  31. })
  32. test("already opened as PWA, so user doesn't see a button", async () => {
  33. mockPWA({ canInstallPWA: false, isStandalone: true })
  34. const view = await visitView('/account')
  35. expect(view.queryByText('Install')).not.toBeInTheDocument()
  36. })
  37. test('installing PWA, when prompt event is available', async () => {
  38. const installPWA = vi.fn()
  39. mockPWA({ canInstallPWA: true, isStandalone: false, installPWA })
  40. const view = await visitView('/account')
  41. const install = view.getByText('Install App')
  42. await view.events.click(install)
  43. expect(installPWA).toHaveBeenCalled()
  44. })
  45. test('installing PWA on iOS - show instructions', async () => {
  46. utilsBrowsermock.browser.name = 'Safari'
  47. utilsBrowsermock.os.name = 'iOS'
  48. mockPWA({ canInstallPWA: false, isStandalone: false })
  49. const view = await visitView('/account')
  50. const install = view.getByText('Install App')
  51. await view.events.click(install)
  52. expect(
  53. view.getByText(/To install Zammad as an app, press/),
  54. ).toBeInTheDocument()
  55. expect(view.getByIconName('ios-share')).toBeInTheDocument()
  56. expect(view.getByIconName('add-square')).toBeInTheDocument()
  57. })
  58. })