|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +import Gleap, { |
| 5 | + GleapAudioManager, |
| 6 | + GleapConfigManager, |
| 7 | + GleapFeedbackButtonManager, |
| 8 | + GleapFrameManager, |
| 9 | + GleapNotificationManager, |
| 10 | + GleapSession, |
| 11 | +} from './Gleap'; |
| 12 | +import * as cache from './GleapHelper'; |
| 13 | + |
| 14 | +const news = (extra = {}) => ({ |
| 15 | + outbound: 'product-announcement', |
| 16 | + createdAt: new Date().toISOString(), |
| 17 | + cachedForGleapId: 'signup-visitor', |
| 18 | + data: { news: { id: 'news-1' }, text: 'Product announcement' }, |
| 19 | + ...extra, |
| 20 | +}); |
| 21 | + |
| 22 | +const mountNotifications = () => { |
| 23 | + const manager = GleapNotificationManager.getInstance(); |
| 24 | + manager.notificationContainer = document.createElement('div'); |
| 25 | + document.body.appendChild(manager.notificationContainer); |
| 26 | + return manager; |
| 27 | +}; |
| 28 | + |
| 29 | +const visibleCards = (manager) => manager.notificationContainer.querySelectorAll('.gleap-notification-card'); |
| 30 | + |
| 31 | +beforeEach(() => { |
| 32 | + // Exercise the real public setter and notification renderer, without SDK startup. |
| 33 | + jest.spyOn(Gleap, 'getInstance').mockReturnValue({ disableInAppNotifications: false }); |
| 34 | + jest.spyOn(GleapSession, 'getInstance').mockReturnValue({ |
| 35 | + session: { gleapId: 'signup-visitor' }, |
| 36 | + getName: () => 'Visitor', |
| 37 | + }); |
| 38 | + jest.spyOn(GleapConfigManager, 'getInstance').mockReturnValue({ getFlowConfig: () => ({}) }); |
| 39 | + jest.spyOn(GleapAudioManager, 'ping').mockImplementation(() => {}); |
| 40 | + jest.spyOn(cache, 'loadFromGleapCache').mockReturnValue(null); |
| 41 | + jest.spyOn(cache, 'saveToGleapCache').mockImplementation(() => {}); |
| 42 | + GleapNotificationManager.instance = undefined; |
| 43 | +}); |
| 44 | + |
| 45 | +afterEach(() => { |
| 46 | + jest.restoreAllMocks(); |
| 47 | + GleapNotificationManager.instance = undefined; |
| 48 | + document.body.innerHTML = ''; |
| 49 | +}); |
| 50 | + |
| 51 | +test('disabling before startup keeps cached announcements hidden without deleting them', () => { |
| 52 | + Gleap.setDisableInAppNotifications(true); |
| 53 | + const manager = mountNotifications(); |
| 54 | + const cached = news(); |
| 55 | + cache.loadFromGleapCache.mockReturnValue([cached]); |
| 56 | + |
| 57 | + manager.reloadNotificationsFromCache(); |
| 58 | + |
| 59 | + expect(visibleCards(manager)).toHaveLength(0); |
| 60 | + expect(manager.notifications).toEqual([cached]); |
| 61 | + expect(cache.saveToGleapCache).not.toHaveBeenCalled(); |
| 62 | + |
| 63 | + Gleap.setDisableInAppNotifications(false); |
| 64 | + expect(visibleCards(manager)).toHaveLength(1); |
| 65 | +}); |
| 66 | + |
| 67 | +test('disabling immediately hides an existing card and keeps it hidden on cache reload', () => { |
| 68 | + const manager = mountNotifications(); |
| 69 | + const cached = news(); |
| 70 | + cache.loadFromGleapCache.mockReturnValue([cached]); |
| 71 | + manager.reloadNotificationsFromCache(); |
| 72 | + expect(visibleCards(manager)).toHaveLength(1); |
| 73 | + |
| 74 | + Gleap.setDisableInAppNotifications(true); |
| 75 | + expect(manager.notificationContainer.children).toHaveLength(0); |
| 76 | + |
| 77 | + // Closing the messenger reloads cached notifications through the same path. |
| 78 | + manager.reloadNotificationsFromCache(); |
| 79 | + expect(manager.notificationContainer.children).toHaveLength(0); |
| 80 | + expect(manager.notifications).toEqual([cached]); |
| 81 | + |
| 82 | + Gleap.setDisableInAppNotifications(false); |
| 83 | + expect(visibleCards(manager)).toHaveLength(1); |
| 84 | + expect(cache.saveToGleapCache).not.toHaveBeenCalled(); |
| 85 | +}); |
| 86 | + |
| 87 | +test('a directly delivered notification stays silent and hidden while disabled', () => { |
| 88 | + const manager = mountNotifications(); |
| 89 | + Gleap.setDisableInAppNotifications(true); |
| 90 | + |
| 91 | + Gleap.showNotification(news({ sound: true })); |
| 92 | + |
| 93 | + expect(visibleCards(manager)).toHaveLength(0); |
| 94 | + expect(GleapAudioManager.ping).not.toHaveBeenCalled(); |
| 95 | + expect(manager.notifications).toHaveLength(1); |
| 96 | + |
| 97 | + Gleap.setDisableInAppNotifications(false); |
| 98 | + expect(visibleCards(manager)).toHaveLength(1); |
| 99 | + expect(GleapAudioManager.ping).not.toHaveBeenCalled(); |
| 100 | +}); |
| 101 | + |
| 102 | +test('reenabling notifications still respects their page exclusions', () => { |
| 103 | + const manager = mountNotifications(); |
| 104 | + manager.notifications = [news({ pageRules: [{ pageFilter: '/', pageFilterType: 'notcontains' }] })]; |
| 105 | + |
| 106 | + Gleap.setDisableInAppNotifications(true); |
| 107 | + Gleap.setDisableInAppNotifications(false); |
| 108 | + |
| 109 | + expect(visibleCards(manager)).toHaveLength(0); |
| 110 | + expect(manager.notifications).toHaveLength(1); |
| 111 | +}); |
| 112 | + |
| 113 | +test('support chat can still open while notification cards are disabled', () => { |
| 114 | + const frame = { setAppMode: jest.fn(), showWidget: jest.fn(), hideWidget: jest.fn() }; |
| 115 | + jest.spyOn(GleapFrameManager, 'getInstance').mockReturnValue(frame); |
| 116 | + const buttonManager = jest.spyOn(GleapFeedbackButtonManager, 'getInstance'); |
| 117 | + |
| 118 | + Gleap.setDisableInAppNotifications(true); |
| 119 | + Gleap.open(); |
| 120 | + |
| 121 | + expect(frame.setAppMode).toHaveBeenCalledWith('widget'); |
| 122 | + expect(frame.showWidget).toHaveBeenCalledTimes(1); |
| 123 | + expect(frame.hideWidget).not.toHaveBeenCalled(); |
| 124 | + expect(buttonManager).not.toHaveBeenCalled(); |
| 125 | +}); |
0 commit comments