|
1 | 1 | import { expect } from '@vaadin/chai-plugins';
|
2 | 2 | import { click, fixtureSync, listenOnce, mousedown, nextFrame, nextRender } from '@vaadin/testing-helpers';
|
3 |
| -import { sendKeys } from '@web/test-runner-commands'; |
| 3 | +import { resetMouse, sendKeys, sendMouse } from '@web/test-runner-commands'; |
4 | 4 | import sinon from 'sinon';
|
5 | 5 | import {
|
6 | 6 | fire,
|
@@ -856,4 +856,157 @@ describe('multi selection column', () => {
|
856 | 856 | expect(grid.$.table.scrollTop).to.be.eq(prevScrollTop);
|
857 | 857 | });
|
858 | 858 | });
|
| 859 | + |
| 860 | + describe('item-toggle event', () => { |
| 861 | + let itemSelectionSpy, rows, checkboxes; |
| 862 | + |
| 863 | + async function mouseClick(element) { |
| 864 | + const { x, y, width, height } = element.getBoundingClientRect(); |
| 865 | + await sendMouse({ |
| 866 | + type: 'click', |
| 867 | + position: [x + width / 2, y + height / 2].map(Math.floor), |
| 868 | + }); |
| 869 | + } |
| 870 | + |
| 871 | + function assertEvent(detail) { |
| 872 | + expect(itemSelectionSpy).to.be.calledOnce; |
| 873 | + expect(itemSelectionSpy.args[0][0].detail).to.eql(detail); |
| 874 | + itemSelectionSpy.resetHistory(); |
| 875 | + } |
| 876 | + |
| 877 | + beforeEach(async () => { |
| 878 | + grid = fixtureSync(` |
| 879 | + <vaadin-grid style="width: 200px; height: 450px;"> |
| 880 | + <vaadin-grid-selection-column></vaadin-grid-selection-column> |
| 881 | + <vaadin-grid-column path="name"></vaadin-grid-column> |
| 882 | + </vaadin-grid> |
| 883 | + `); |
| 884 | + grid.items = [{ name: 'Item 0' }, { name: 'Item 1' }, { name: 'Item 2' }]; |
| 885 | + await nextRender(); |
| 886 | + |
| 887 | + rows = getRows(grid.$.items); |
| 888 | + checkboxes = [...grid.querySelectorAll('vaadin-checkbox[aria-label="Select Row"]')]; |
| 889 | + |
| 890 | + itemSelectionSpy = sinon.spy(); |
| 891 | + grid.addEventListener('item-toggle', itemSelectionSpy); |
| 892 | + }); |
| 893 | + |
| 894 | + afterEach(async () => { |
| 895 | + await resetMouse(); |
| 896 | + }); |
| 897 | + |
| 898 | + it('should fire the event when toggling an item with click', async () => { |
| 899 | + await mouseClick(checkboxes[0]); |
| 900 | + assertEvent({ item: grid.items[0], selected: true, shiftKey: false }); |
| 901 | + |
| 902 | + await mouseClick(checkboxes[0]); |
| 903 | + assertEvent({ item: grid.items[0], selected: false, shiftKey: false }); |
| 904 | + }); |
| 905 | + |
| 906 | + it('should fire the event when toggling an item with Shift + click', async () => { |
| 907 | + await sendKeys({ down: 'Shift' }); |
| 908 | + await mouseClick(checkboxes[0]); |
| 909 | + await sendKeys({ up: 'Shift' }); |
| 910 | + assertEvent({ item: grid.items[0], selected: true, shiftKey: true }); |
| 911 | + |
| 912 | + await sendKeys({ down: 'Shift' }); |
| 913 | + await mouseClick(checkboxes[0]); |
| 914 | + await sendKeys({ up: 'Shift' }); |
| 915 | + assertEvent({ item: grid.items[0], selected: false, shiftKey: true }); |
| 916 | + }); |
| 917 | + |
| 918 | + it('should fire the event when toggling an item with Space', async () => { |
| 919 | + checkboxes[0].focus(); |
| 920 | + |
| 921 | + await sendKeys({ press: 'Space' }); |
| 922 | + assertEvent({ item: grid.items[0], selected: true, shiftKey: false }); |
| 923 | + |
| 924 | + await sendKeys({ press: 'Space' }); |
| 925 | + assertEvent({ item: grid.items[0], selected: false, shiftKey: false }); |
| 926 | + }); |
| 927 | + |
| 928 | + it('should fire the event when toggling an item with Shift + Space', async () => { |
| 929 | + checkboxes[0].focus(); |
| 930 | + |
| 931 | + await sendKeys({ down: 'Shift' }); |
| 932 | + await sendKeys({ press: 'Space' }); |
| 933 | + await sendKeys({ up: 'Shift' }); |
| 934 | + assertEvent({ item: grid.items[0], selected: true, shiftKey: true }); |
| 935 | + |
| 936 | + await sendKeys({ down: 'Shift' }); |
| 937 | + await sendKeys({ press: 'Space' }); |
| 938 | + await sendKeys({ up: 'Shift' }); |
| 939 | + assertEvent({ item: grid.items[0], selected: false, shiftKey: true }); |
| 940 | + }); |
| 941 | + |
| 942 | + describe('autoSelect', () => { |
| 943 | + beforeEach(() => { |
| 944 | + const selectionColumn = grid.querySelector('vaadin-grid-selection-column'); |
| 945 | + selectionColumn.autoSelect = true; |
| 946 | + }); |
| 947 | + |
| 948 | + it('should fire the event when toggling an item with click', async () => { |
| 949 | + await mouseClick(rows[0]); |
| 950 | + assertEvent({ item: grid.items[0], selected: true, shiftKey: false }); |
| 951 | + |
| 952 | + await mouseClick(rows[0]); |
| 953 | + assertEvent({ item: grid.items[0], selected: false, shiftKey: false }); |
| 954 | + }); |
| 955 | + |
| 956 | + it('should fire the event when toggling an item with Shift + click', async () => { |
| 957 | + await sendKeys({ down: 'Shift' }); |
| 958 | + await mouseClick(rows[0]); |
| 959 | + await sendKeys({ up: 'Shift' }); |
| 960 | + assertEvent({ item: grid.items[0], selected: true, shiftKey: true }); |
| 961 | + |
| 962 | + await sendKeys({ down: 'Shift' }); |
| 963 | + await mouseClick(rows[0]); |
| 964 | + await sendKeys({ up: 'Shift' }); |
| 965 | + assertEvent({ item: grid.items[0], selected: false, shiftKey: true }); |
| 966 | + }); |
| 967 | + |
| 968 | + it('should fire the event when toggling an item with Space', async () => { |
| 969 | + getRowCells(rows[0])[1].focus(); |
| 970 | + |
| 971 | + await sendKeys({ press: 'Space' }); |
| 972 | + assertEvent({ item: grid.items[0], selected: true, shiftKey: false }); |
| 973 | + |
| 974 | + await sendKeys({ press: 'Space' }); |
| 975 | + assertEvent({ item: grid.items[0], selected: false, shiftKey: false }); |
| 976 | + }); |
| 977 | + |
| 978 | + it('should fire the event when toggling an item with Shift + Space', async () => { |
| 979 | + getRowCells(rows[0])[1].focus(); |
| 980 | + |
| 981 | + await sendKeys({ down: 'Shift' }); |
| 982 | + await sendKeys({ press: 'Space' }); |
| 983 | + await sendKeys({ up: 'Shift' }); |
| 984 | + assertEvent({ item: grid.items[0], selected: true, shiftKey: true }); |
| 985 | + |
| 986 | + await sendKeys({ down: 'Shift' }); |
| 987 | + await sendKeys({ press: 'Space' }); |
| 988 | + await sendKeys({ up: 'Shift' }); |
| 989 | + assertEvent({ item: grid.items[0], selected: false, shiftKey: true }); |
| 990 | + }); |
| 991 | + |
| 992 | + it('should prevent text selection when selecting a range of items with Shift + click', async () => { |
| 993 | + await mouseClick(rows[0]); |
| 994 | + await sendKeys({ down: 'Shift' }); |
| 995 | + await mouseClick(rows[1]); |
| 996 | + await sendKeys({ up: 'Shift' }); |
| 997 | + expect(document.getSelection().toString()).to.be.empty; |
| 998 | + }); |
| 999 | + |
| 1000 | + it('should allow text selection after selecting a range of items with Shift + click', async () => { |
| 1001 | + await mouseClick(rows[0]); |
| 1002 | + await sendKeys({ down: 'Shift' }); |
| 1003 | + await mouseClick(rows[1]); |
| 1004 | + await sendKeys({ up: 'Shift' }); |
| 1005 | + |
| 1006 | + const row2CellContent1 = getBodyCellContent(grid, 2, 1); |
| 1007 | + document.getSelection().selectAllChildren(row2CellContent1); |
| 1008 | + expect(document.getSelection().toString()).to.be.not.empty; |
| 1009 | + }); |
| 1010 | + }); |
| 1011 | + }); |
859 | 1012 | });
|
0 commit comments