-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGleapBannerManager.test.js
More file actions
105 lines (86 loc) · 3.25 KB
/
Copy pathGleapBannerManager.test.js
File metadata and controls
105 lines (86 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* @jest-environment jsdom
*/
import { bootstrapGleapFrame } from './GleapHelper';
import GleapBannerManager from './GleapBannerManager';
// Stub the barrel so the real GleapBannerManager loads without the full SDK.
jest.mock('./Gleap', () => ({
__esModule: true,
default: {},
GleapFrameManager: { getInstance: jest.fn(() => ({ urlHandler: jest.fn() })) },
}));
jest.mock('./GleapHelper', () => ({
bootstrapGleapFrame: jest.fn(),
}));
const HOME = 'https://communitise.com/';
const SUBPAGE = 'https://communitise.com/communities/123/feed';
const homeOnlyBanner = () => ({
format: 'inline',
pageRules: [{ pageFilter: HOME, pageFilterType: 'is' }],
pageFilter: HOME,
pageFilterType: 'is',
});
const bannerVisible = () => document.body.querySelector('.gleap-b') !== null;
beforeEach(() => {
document.body.innerHTML = '';
document.body.className = '';
GleapBannerManager.instance = null;
bootstrapGleapFrame.mockClear();
});
describe('banner page rules across SPA navigations', () => {
test('banner without page rules shows immediately and stays', () => {
const bm = GleapBannerManager.getInstance();
bm.showBanner({ format: 'inline' });
expect(bannerVisible()).toBe(true);
bm.checkPageRulesForUrl(SUBPAGE);
expect(bannerVisible()).toBe(true);
});
test('page-ruled banner hides when navigating to an excluded page and re-appears on an allowed one', () => {
const bm = GleapBannerManager.getInstance();
bm.showBanner(homeOnlyBanner());
// jsdom URL (http://localhost/) fails the "is" rule, so it starts pending.
expect(bannerVisible()).toBe(false);
bm.checkPageRulesForUrl(HOME);
expect(bannerVisible()).toBe(true);
bm.checkPageRulesForUrl(SUBPAGE);
expect(bannerVisible()).toBe(false);
bm.checkPageRulesForUrl(HOME);
expect(bannerVisible()).toBe(true);
});
test('banner arriving on an excluded page is kept pending, not consumed', () => {
const bm = GleapBannerManager.getInstance();
bm.showBanner(homeOnlyBanner());
expect(bannerVisible()).toBe(false);
expect(bm.bannerData).not.toBeNull();
bm.checkPageRulesForUrl(HOME);
expect(bannerVisible()).toBe(true);
});
test('same URL is only evaluated once per change', () => {
const bm = GleapBannerManager.getInstance();
bm.showBanner(homeOnlyBanner());
bm.checkPageRulesForUrl(HOME);
expect(bannerVisible()).toBe(true);
const injectSpy = jest.spyOn(bm, 'injectBannerUI');
bm.checkPageRulesForUrl(HOME);
bm.checkPageRulesForUrl(HOME);
expect(injectSpy).not.toHaveBeenCalled();
});
test('user dismissal is final — navigation does not resurrect the banner', () => {
const bm = GleapBannerManager.getInstance();
bm.showBanner(homeOnlyBanner());
bm.checkPageRulesForUrl(HOME);
expect(bannerVisible()).toBe(true);
// Simulate the banner iframe posting banner-close (user clicked X).
window.dispatchEvent(
new MessageEvent('message', {
data: JSON.stringify({ type: 'BANNER', name: 'banner-close' }),
origin: 'https://outboundmedia.gleap.io',
})
);
expect(bannerVisible()).toBe(false);
expect(bm.bannerData).toBeNull();
bm.checkPageRulesForUrl(SUBPAGE);
bm.checkPageRulesForUrl(HOME);
expect(bannerVisible()).toBe(false);
});
});