|
| 1 | +import { expect } from '@esm-bundle/chai'; |
| 2 | +import { escKeyDown, fixtureSync, focusin, focusout, mousedown, nextFrame, tabKeyDown } from '@vaadin/testing-helpers'; |
| 3 | +import sinon from 'sinon'; |
| 4 | +import '@vaadin/grid/vaadin-grid.js'; |
| 5 | +import '@vaadin/tooltip/vaadin-tooltip.js'; |
| 6 | +import { flushGrid, getCell } from '@vaadin/grid/test/helpers.js'; |
| 7 | +import { mouseenter, mouseleave } from '@vaadin/tooltip/test/helpers.js'; |
| 8 | + |
| 9 | +function getHeaderCell(grid, index = 0) { |
| 10 | + return grid.$.header.querySelectorAll('[part~="cell"]')[index]; |
| 11 | +} |
| 12 | + |
| 13 | +describe('tooltip', () => { |
| 14 | + let grid, tooltip; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + grid = fixtureSync(` |
| 18 | + <vaadin-grid> |
| 19 | + <vaadin-grid-column path="firstName"></vaadin-grid-column> |
| 20 | + <vaadin-grid-column path="lastName"></vaadin-grid-column> |
| 21 | + <vaadin-tooltip slot="tooltip"></vaadin-tooltip> |
| 22 | + </vaadin-grid> |
| 23 | + `); |
| 24 | + grid.items = [ |
| 25 | + { firstName: 'John', lastName: 'Doe' }, |
| 26 | + { firstName: 'Jane', lastName: 'Doe' }, |
| 27 | + ]; |
| 28 | + flushGrid(grid); |
| 29 | + |
| 30 | + tooltip = grid.querySelector('vaadin-tooltip'); |
| 31 | + tooltip.textGenerator = ({ column, item }) => { |
| 32 | + return column && column.path && item ? `${column.path}: ${item[column.path]}` : ''; |
| 33 | + }; |
| 34 | + }); |
| 35 | + |
| 36 | + it('should set manual on the tooltip to true', () => { |
| 37 | + expect(tooltip.manual).to.be.true; |
| 38 | + }); |
| 39 | + |
| 40 | + it('should show tooltip on cell mouseenter', () => { |
| 41 | + const cell = getCell(grid, 0); |
| 42 | + mouseenter(cell); |
| 43 | + expect(tooltip.opened).to.be.true; |
| 44 | + }); |
| 45 | + |
| 46 | + it('should hide tooltip on cell mouseleave', () => { |
| 47 | + const cell = getCell(grid, 0); |
| 48 | + mouseenter(cell); |
| 49 | + mouseleave(cell); |
| 50 | + expect(tooltip.opened).to.be.false; |
| 51 | + }); |
| 52 | + |
| 53 | + it('should set tooltip target on cell mouseenter', () => { |
| 54 | + const cell = getCell(grid, 0); |
| 55 | + mouseenter(cell); |
| 56 | + expect(tooltip.target).to.be.equal(cell); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should set tooltip context on cell mouseenter', () => { |
| 60 | + const cell = getCell(grid, 0); |
| 61 | + mouseenter(cell); |
| 62 | + expect(tooltip.context).to.be.instanceOf(Object); |
| 63 | + expect(tooltip.context.item.firstName).to.equal('John'); |
| 64 | + expect(tooltip.context.item.lastName).to.equal('Doe'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should change tooltip context on header cell mouseenter', () => { |
| 68 | + const cell = getCell(grid, 0); |
| 69 | + mouseenter(cell); |
| 70 | + |
| 71 | + mouseleave(cell); |
| 72 | + mouseenter(getHeaderCell(grid, 0)); |
| 73 | + |
| 74 | + expect(tooltip.context.item).to.be.not.ok; |
| 75 | + }); |
| 76 | + |
| 77 | + it('should hide tooltip on cell mousedown', () => { |
| 78 | + const cell = getCell(grid, 0); |
| 79 | + mouseenter(cell); |
| 80 | + mousedown(cell); |
| 81 | + expect(tooltip.opened).to.be.false; |
| 82 | + }); |
| 83 | + |
| 84 | + it('should show tooltip on cell keyboard focus', () => { |
| 85 | + const cell = getCell(grid, 0); |
| 86 | + tabKeyDown(document.body); |
| 87 | + focusin(cell); |
| 88 | + expect(tooltip.opened).to.be.true; |
| 89 | + }); |
| 90 | + |
| 91 | + it('should set tooltip target on cell keyboard focus', () => { |
| 92 | + const cell = getCell(grid, 0); |
| 93 | + tabKeyDown(document.body); |
| 94 | + focusin(cell); |
| 95 | + expect(tooltip.target).to.be.equal(cell); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should set tooltip context on cell keyboard focus', () => { |
| 99 | + const cell = getCell(grid, 0); |
| 100 | + tabKeyDown(document.body); |
| 101 | + focusin(cell); |
| 102 | + expect(tooltip.context).to.be.instanceOf(Object); |
| 103 | + expect(tooltip.context.item.firstName).to.equal('John'); |
| 104 | + expect(tooltip.context.item.lastName).to.equal('Doe'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should hide tooltip on grid focusout', () => { |
| 108 | + const cell = getCell(grid, 0); |
| 109 | + tabKeyDown(document.body); |
| 110 | + focusin(cell); |
| 111 | + focusout(grid); |
| 112 | + expect(tooltip.opened).to.be.false; |
| 113 | + }); |
| 114 | + |
| 115 | + it('should hide tooltip on grid cell content Esc', () => { |
| 116 | + const cell = getCell(grid, 0); |
| 117 | + tabKeyDown(document.body); |
| 118 | + focusin(cell._content); |
| 119 | + escKeyDown(cell._content); |
| 120 | + expect(tooltip.opened).to.be.false; |
| 121 | + }); |
| 122 | + |
| 123 | + it('should set tooltip opened to false when the grid is removed', () => { |
| 124 | + const cell = getCell(grid, 0); |
| 125 | + mouseenter(cell); |
| 126 | + |
| 127 | + grid.remove(); |
| 128 | + |
| 129 | + expect(tooltip.opened).to.be.false; |
| 130 | + }); |
| 131 | + |
| 132 | + it('should not set tooltip target if there is no tooltip', async () => { |
| 133 | + const spy = sinon.spy(grid._tooltipController, 'setTarget'); |
| 134 | + |
| 135 | + tooltip.remove(); |
| 136 | + await nextFrame(); |
| 137 | + |
| 138 | + const cell = getCell(grid, 0); |
| 139 | + mouseenter(cell); |
| 140 | + |
| 141 | + expect(spy.calledOnce).to.be.false; |
| 142 | + }); |
| 143 | + |
| 144 | + it('should not set tooltip context if there is no tooltip', async () => { |
| 145 | + const spy = sinon.spy(grid._tooltipController, 'setContext'); |
| 146 | + |
| 147 | + tooltip.remove(); |
| 148 | + await nextFrame(); |
| 149 | + |
| 150 | + const cell = getCell(grid, 0); |
| 151 | + mouseenter(cell); |
| 152 | + |
| 153 | + expect(spy.calledOnce).to.be.false; |
| 154 | + }); |
| 155 | + |
| 156 | + it('should not set tooltip opened if there is no tooltip', async () => { |
| 157 | + const spy = sinon.spy(grid._tooltipController, 'setOpened'); |
| 158 | + |
| 159 | + tooltip.remove(); |
| 160 | + await nextFrame(); |
| 161 | + |
| 162 | + const cell = getCell(grid, 0); |
| 163 | + mouseenter(cell); |
| 164 | + |
| 165 | + expect(spy.calledOnce).to.be.false; |
| 166 | + }); |
| 167 | +}); |
0 commit comments