import { describe, it, expect, vi, beforeEach } from 'vitest' import { useLiveChat } from '#lib/composables' import { useDevice, useRuntimeConfig } from '#imports' vi.mock('#imports', () => ({ useDevice: vi.fn(), useRuntimeConfig: vi.fn(), persistedState: vi.fn().mockReturnValue({ localStorage: {}, }), })) describe('useLiveChat', () => { let isMobile: boolean let runTimeConfig: any beforeEach(() => { isMobile = false runTimeConfig = { public: { LIVE_CHAT_LINK: 'https://livechat.example.com', }, } vi.mocked(useDevice).mockReturnValue({ isMobile }) vi.mocked(useRuntimeConfig).mockReturnValue(runTimeConfig) }) it('should open live chat in a new tab on mobile devices', () => { isMobile = true vi.mocked(useDevice).mockReturnValue({ isMobile }) global.window = Object.create(window) const openSpy = vi.spyOn(window, 'open').mockReturnValue({ location: { href: '' }, }) const { openLiveChat } = useLiveChat() openLiveChat() expect(openSpy).toHaveBeenCalledWith('about:blank', '_blank') expect(openSpy().location.href).toBe(runTimeConfig.public.LIVE_CHAT_LINK) }) it('should maximize the live chat widget on non-mobile devices', () => { isMobile = false vi.mocked(useDevice).mockReturnValue({ isMobile }) // Mock the LiveChatWidget call function global.window = Object.create(window) const liveChatSpy = vi.fn() global.window.LiveChatWidget = { call: liveChatSpy, } const { openLiveChat } = useLiveChat() openLiveChat() expect(liveChatSpy).toHaveBeenCalledWith('maximize') }) it('should not crash if window.open fails on mobile devices', () => { isMobile = true vi.mocked(useDevice).mockReturnValue({ isMobile }) global.window = Object.create(window) const openSpy = vi.spyOn(window, 'open').mockReturnValue(null) const { openLiveChat } = useLiveChat() openLiveChat() expect(openSpy).toHaveBeenCalledWith('about:blank', '_blank') // No further assertions needed since we're checking for crash resistance }) })