|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +import GleapTours from './GleapTours'; |
| 5 | + |
| 6 | +// jsdom has no layout, so every element reports no client rects. Treat an |
| 7 | +// element as rendered unless it or an ancestor is display:none, which is what |
| 8 | +// a browser does for v-show / [hidden] / collapsed-menu labels. |
| 9 | +const isHiddenInTest = (el) => { |
| 10 | + for (let node = el; node && node.style; node = node.parentElement) { |
| 11 | + if (node.style.display === 'none') { |
| 12 | + return true; |
| 13 | + } |
| 14 | + } |
| 15 | + return false; |
| 16 | +}; |
| 17 | + |
| 18 | +describe('GleapTours step target resolution', () => { |
| 19 | + let tour; |
| 20 | + let onElementNotFound; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + jest.useFakeTimers(); |
| 24 | + jest.spyOn(Element.prototype, 'getClientRects').mockImplementation(function () { |
| 25 | + return isHiddenInTest(this) ? [] : [{ top: 0, left: 0, width: 40, height: 20 }]; |
| 26 | + }); |
| 27 | + onElementNotFound = jest.fn(() => tour.destroy()); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + if (tour) { |
| 32 | + tour.destroy(); |
| 33 | + } |
| 34 | + tour = null; |
| 35 | + jest.runOnlyPendingTimers(); |
| 36 | + jest.useRealTimers(); |
| 37 | + jest.restoreAllMocks(); |
| 38 | + document.body.innerHTML = ''; |
| 39 | + }); |
| 40 | + |
| 41 | + const startTour = (selector) => { |
| 42 | + tour = GleapTours({ |
| 43 | + animate: false, |
| 44 | + allowClose: false, |
| 45 | + onElementNotFound, |
| 46 | + steps: [{ element: selector, popover: { description: 'Click on Library', showButtons: [] } }], |
| 47 | + }); |
| 48 | + tour.drive(); |
| 49 | + }; |
| 50 | + |
| 51 | + it('highlights a rendered target right away', () => { |
| 52 | + document.body.innerHTML = '<nav><a class="menu-item"><span class="label">Library</span></a></nav>'; |
| 53 | + startTour('.menu-item > .label'); |
| 54 | + |
| 55 | + expect(tour.getActiveElement()).toBe(document.querySelector('.label')); |
| 56 | + expect(onElementNotFound).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('treats a target that only matches a display:none element as not found (#185122)', () => { |
| 60 | + // Collapsed sidebar: the label the step was recorded on is hidden via v-show. |
| 61 | + document.body.innerHTML = |
| 62 | + '<nav><a class="menu-item"><i class="icon"></i><span class="label" style="display: none">Library</span></a></nav>'; |
| 63 | + startTour('.menu-item > .label'); |
| 64 | + |
| 65 | + expect(tour.getActiveElement()).toBeUndefined(); |
| 66 | + expect(document.querySelector('.label').classList.contains('gleap-tour-active-element')).toBe(false); |
| 67 | + |
| 68 | + jest.advanceTimersByTime(5200); |
| 69 | + |
| 70 | + expect(onElementNotFound).toHaveBeenCalledTimes(1); |
| 71 | + expect(onElementNotFound.mock.calls[0][0].element).toBe('.menu-item > .label'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('skips hidden matches and highlights the first rendered one', () => { |
| 75 | + // Responsive layouts render the same menu twice and hide one of them. |
| 76 | + document.body.innerHTML = |
| 77 | + '<div class="mobile" style="display: none"><a class="menu-item"><span class="label">Library</span></a></div>' + |
| 78 | + '<div class="desktop"><a class="menu-item"><span class="label">Library</span></a></div>'; |
| 79 | + startTour('.menu-item > .label'); |
| 80 | + |
| 81 | + expect(tour.getActiveElement()).toBe(document.querySelector('.desktop .label')); |
| 82 | + expect(onElementNotFound).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('keeps waiting for a hidden target that becomes visible within the timeout', () => { |
| 86 | + document.body.innerHTML = '<nav><a class="menu-item"><span class="label" style="display: none">Library</span></a></nav>'; |
| 87 | + startTour('.menu-item > .label'); |
| 88 | + |
| 89 | + jest.advanceTimersByTime(1000); |
| 90 | + document.querySelector('.label').style.display = ''; |
| 91 | + jest.advanceTimersByTime(200); |
| 92 | + |
| 93 | + expect(tour.getActiveElement()).toBe(document.querySelector('.label')); |
| 94 | + jest.advanceTimersByTime(5000); |
| 95 | + expect(onElementNotFound).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('still quits when the selector matches nothing', () => { |
| 99 | + document.body.innerHTML = '<nav></nav>'; |
| 100 | + startTour('.menu-item > .label'); |
| 101 | + |
| 102 | + jest.advanceTimersByTime(5200); |
| 103 | + |
| 104 | + expect(onElementNotFound).toHaveBeenCalledTimes(1); |
| 105 | + }); |
| 106 | +}); |
0 commit comments