|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +import Gleap, { GleapSession } from './Gleap'; |
| 5 | + |
| 6 | +const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); |
| 7 | + |
| 8 | +// A checklist step starts a tour on another page: the SDK redirects to the tour's start URL |
| 9 | +// and the tour starts there from the gleap_tour query param (#229004). |
| 10 | +describe('Gleap.startProductTour redirect to the start URL', () => { |
| 11 | + const originalLocation = window.location; |
| 12 | + let configSpy; |
| 13 | + |
| 14 | + const startFrom = async (currentUrl, config) => { |
| 15 | + delete window.location; |
| 16 | + window.location = { href: currentUrl }; |
| 17 | + configSpy = jest.spyOn(GleapSession.getInstance(), 'startProductTourConfig').mockResolvedValue(config); |
| 18 | + Gleap.startProductTour('tour-1', true); |
| 19 | + await flush(); |
| 20 | + return new URL(window.location.href); |
| 21 | + }; |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + configSpy.mockRestore(); |
| 25 | + window.location = originalLocation; |
| 26 | + }); |
| 27 | + |
| 28 | + it('adds the tour params to a start URL that has no query', async () => { |
| 29 | + const target = await startFrom('https://app.example.com/home', { |
| 30 | + startURL: 'https://app.example.com/workspaces', |
| 31 | + baseURL: 'https://app.example.com/workspaces', |
| 32 | + }); |
| 33 | + |
| 34 | + expect(target.pathname).toBe('/workspaces'); |
| 35 | + expect(target.searchParams.get('gleap_tour')).toBe('tour-1'); |
| 36 | + expect(target.searchParams.get('gleap_tour_delay')).toBe('1'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('keeps the query of a start URL that already has one', async () => { |
| 40 | + const target = await startFrom('https://app.example.com/home', { |
| 41 | + startURL: 'https://app.example.com/workspaces?view=list', |
| 42 | + baseURL: 'https://app.example.com/workspaces?view=list', |
| 43 | + }); |
| 44 | + |
| 45 | + expect(target.searchParams.get('view')).toBe('list'); |
| 46 | + expect(target.searchParams.get('gleap_tour')).toBe('tour-1'); |
| 47 | + expect(target.searchParams.get('gleap_tour_delay')).toBe('1'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('keeps the hash of the start URL after the tour params', async () => { |
| 51 | + const target = await startFrom('https://app.example.com/home', { |
| 52 | + startURL: 'https://app.example.com/workspaces?view=list#board', |
| 53 | + baseURL: 'https://app.example.com/workspaces', |
| 54 | + }); |
| 55 | + |
| 56 | + expect(target.searchParams.get('view')).toBe('list'); |
| 57 | + expect(target.searchParams.get('gleap_tour')).toBe('tour-1'); |
| 58 | + expect(target.hash).toBe('#board'); |
| 59 | + }); |
| 60 | +}); |
0 commit comments