|
| 1 | +import { async, fakeAsync, TestBed, tick} from '@angular/core/testing'; |
| 2 | +import { By } from '@angular/platform-browser'; |
| 3 | +import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations'; |
| 4 | +import { |
| 5 | + IgxGridModule, IgxGridComponent |
| 6 | +} from './index'; |
| 7 | +import { configureTestSuite } from '../../test-utils/configure-suite'; |
| 8 | +import { IgxGridClipboardComponent } from '../../test-utils/grid-samples.spec'; |
| 9 | +import { CancelableEventArgs } from '../../core/utils'; |
| 10 | +import { take } from 'rxjs/operators'; |
| 11 | + |
| 12 | +describe('IgxGrid - Clipboard', () => { |
| 13 | + configureTestSuite(); |
| 14 | + let fix; |
| 15 | + let grid: IgxGridComponent; |
| 16 | + beforeEach(async(() => { |
| 17 | + TestBed.configureTestingModule({ |
| 18 | + declarations: [ |
| 19 | + IgxGridClipboardComponent |
| 20 | + ], |
| 21 | + imports: [BrowserAnimationsModule, IgxGridModule, NoopAnimationsModule] |
| 22 | + }) |
| 23 | + .compileComponents(); |
| 24 | + })); |
| 25 | + |
| 26 | + beforeEach(fakeAsync(/** height/width setter rAF */() => { |
| 27 | + fix = TestBed.createComponent(IgxGridClipboardComponent); |
| 28 | + fix.detectChanges(); |
| 29 | + grid = fix.componentInstance.grid; |
| 30 | + })); |
| 31 | + |
| 32 | + it('Copy data with default settings', () => { |
| 33 | + const copySpy = spyOn<any>(grid.onGridCopy, 'emit').and.callThrough(); |
| 34 | + const range = { rowStart: 0, rowEnd: 1, columnStart: 1, columnEnd: 3 }; |
| 35 | + grid.selectRange(range); |
| 36 | + fix.detectChanges(); |
| 37 | + |
| 38 | + const eventData = dispatchCopyEventOnGridBody(fix); |
| 39 | + expect(copySpy).toHaveBeenCalledTimes(1); |
| 40 | + expect(eventData) |
| 41 | + .toEqual('ProductName\tDownloads\tReleased\r\n** Ignite UI for JavaScript **\t254\tfalse\r\n** NetAdvantage **\t127\ttrue\r\n'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('Copy data when there are no selected cells', () => { |
| 45 | + const copySpy = spyOn<any>(grid.onGridCopy, 'emit').and.callThrough(); |
| 46 | + const eventData = dispatchCopyEventOnGridBody(fix); |
| 47 | + expect(copySpy).toHaveBeenCalledTimes(1); |
| 48 | + expect(copySpy).toHaveBeenCalledWith({ |
| 49 | + data: [], |
| 50 | + cancel: false |
| 51 | + }); |
| 52 | + expect(eventData).toEqual(''); |
| 53 | + }); |
| 54 | + |
| 55 | + it('Copy data with different separator', () => { |
| 56 | + const copySpy = spyOn<any>(grid.onGridCopy, 'emit').and.callThrough(); |
| 57 | + grid.clipboardOptions.separator = ';'; |
| 58 | + grid.selectRange({ rowStart: 0, rowEnd: 0, columnStart: 0, columnEnd: 0 }); |
| 59 | + grid.selectRange({ rowStart: 1, rowEnd: 1, columnStart: 1, columnEnd: 1 }); |
| 60 | + fix.detectChanges(); |
| 61 | + |
| 62 | + let eventData = dispatchCopyEventOnGridBody(fix); |
| 63 | + expect(copySpy).toHaveBeenCalledTimes(1); |
| 64 | + expect(eventData).toEqual('ID;ProductName\r\n1;\r\n;** NetAdvantage **\r\n'); |
| 65 | + |
| 66 | + grid.clipboardOptions.separator = ','; |
| 67 | + fix.detectChanges(); |
| 68 | + |
| 69 | + eventData = dispatchCopyEventOnGridBody(fix); |
| 70 | + expect(copySpy).toHaveBeenCalledTimes(2); |
| 71 | + expect(eventData).toEqual('ID,ProductName\r\n1,\r\n,** NetAdvantage **\r\n'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('Copy data without headers', () => { |
| 75 | + const copySpy = spyOn<any>(grid.onGridCopy, 'emit').and.callThrough(); |
| 76 | + grid.clipboardOptions.copyHeaders = false; |
| 77 | + grid.selectRange({ rowStart: 1, rowEnd: 2, columnStart: 2, columnEnd: 3 }); |
| 78 | + fix.detectChanges(); |
| 79 | + |
| 80 | + let eventData = dispatchCopyEventOnGridBody(fix); |
| 81 | + expect(copySpy).toHaveBeenCalledTimes(1); |
| 82 | + expect(eventData).toEqual('127\ttrue\r\n20\t\r\n'); |
| 83 | + |
| 84 | + grid.clipboardOptions.copyHeaders = true; |
| 85 | + fix.detectChanges(); |
| 86 | + |
| 87 | + eventData = dispatchCopyEventOnGridBody(fix); |
| 88 | + expect(copySpy).toHaveBeenCalledTimes(2); |
| 89 | + expect(eventData).toEqual('Downloads\tReleased\r\n127\ttrue\r\n20\t\r\n'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('Disable clipboardOptions', () => { |
| 93 | + const copySpy = spyOn<any>(grid.onGridCopy, 'emit').and.callThrough(); |
| 94 | + grid.clipboardOptions.enabled = false; |
| 95 | + grid.selectRange({ rowStart: 0, rowEnd: 2, columnStart: 0, columnEnd: 3 }); |
| 96 | + fix.detectChanges(); |
| 97 | + |
| 98 | + const eventData = dispatchCopyEventOnGridBody(fix); |
| 99 | + expect(copySpy).toHaveBeenCalledTimes(0); |
| 100 | + expect(eventData).toEqual(''); |
| 101 | + }); |
| 102 | + |
| 103 | + it('Disable clipboardOptions', () => { |
| 104 | + const copySpy = spyOn<any>(grid.onGridCopy, 'emit').and.callThrough(); |
| 105 | + grid.clipboardOptions.enabled = false; |
| 106 | + grid.selectRange({ rowStart: 0, rowEnd: 2, columnStart: 0, columnEnd: 3 }); |
| 107 | + fix.detectChanges(); |
| 108 | + |
| 109 | + const eventData = dispatchCopyEventOnGridBody(fix); |
| 110 | + expect(copySpy).toHaveBeenCalledTimes(0); |
| 111 | + expect(eventData).toEqual(''); |
| 112 | + }); |
| 113 | + |
| 114 | + it('Disable copyFormatters', () => { |
| 115 | + const copySpy = spyOn<any>(grid.onGridCopy, 'emit').and.callThrough(); |
| 116 | + grid.clipboardOptions.copyFormatters = false; |
| 117 | + grid.selectRange({ rowStart: 1, rowEnd: 3, columnStart: 1, columnEnd: 1 }); |
| 118 | + fix.detectChanges(); |
| 119 | + |
| 120 | + let eventData = dispatchCopyEventOnGridBody(fix); |
| 121 | + expect(copySpy).toHaveBeenCalledTimes(1); |
| 122 | + expect(eventData).toEqual('ProductName\r\nNetAdvantage\r\nIgnite UI for Angular\r\n\r\n'); |
| 123 | + grid.clipboardOptions.copyFormatters = true; |
| 124 | + fix.detectChanges(); |
| 125 | + |
| 126 | + eventData = dispatchCopyEventOnGridBody(fix); |
| 127 | + expect(copySpy).toHaveBeenCalledTimes(2); |
| 128 | + expect(eventData).toEqual('ProductName\r\n** NetAdvantage **\r\n** Ignite UI for Angular **\r\n** null **\r\n'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('Cancel onGridCopy event ', () => { |
| 132 | + const copySpy = spyOn<any>(grid.onGridCopy, 'emit').and.callThrough(); |
| 133 | + grid.onGridCopy.pipe(take(1)).subscribe((e: CancelableEventArgs) => e.cancel = true); |
| 134 | + grid.selectRange({ rowStart: 1, rowEnd: 3, columnStart: 0, columnEnd: 3 }); |
| 135 | + fix.detectChanges(); |
| 136 | + |
| 137 | + const eventData = dispatchCopyEventOnGridBody(fix); |
| 138 | + expect(copySpy).toHaveBeenCalledTimes(1); |
| 139 | + expect(copySpy).toHaveBeenCalledWith({ |
| 140 | + data: grid.getSelectedData(true), |
| 141 | + cancel: true |
| 142 | + }); |
| 143 | + expect(eventData).toEqual(''); |
| 144 | + }); |
| 145 | + |
| 146 | + it('Copy when there is a cell in edit mode', fakeAsync(() => { |
| 147 | + const copySpy = spyOn<any>(grid.onGridCopy, 'emit').and.callThrough(); |
| 148 | + const cell = grid.getCellByColumn(0, 'ProductName'); |
| 149 | + cell.nativeElement.dispatchEvent( new Event('dblclick')); |
| 150 | + tick(16); |
| 151 | + fix.detectChanges(); |
| 152 | + expect(cell.editMode).toBe(true); |
| 153 | + |
| 154 | + grid.selectRange({ rowStart: 1, rowEnd: 3, columnStart: 0, columnEnd: 3 }); |
| 155 | + tick(16); |
| 156 | + fix.detectChanges(); |
| 157 | + |
| 158 | + expect(cell.editMode).toBe(true); |
| 159 | + |
| 160 | + const eventData = dispatchCopyEventOnGridBody(fix); |
| 161 | + expect(copySpy).toHaveBeenCalledTimes(0); |
| 162 | + expect(eventData).toEqual(''); |
| 163 | + })); |
| 164 | +}); |
| 165 | + |
| 166 | +function dispatchCopyEventOnGridBody(fixture) { |
| 167 | + const gridBody = fixture.debugElement.query(By.css('.igx-grid__tbody')).nativeElement; |
| 168 | + const ev = new ClipboardEvent('copy', {clipboardData: new DataTransfer}); |
| 169 | + gridBody.dispatchEvent(ev); |
| 170 | + fixture.detectChanges(); |
| 171 | + return ev.clipboardData.getData('text/plain'); |
| 172 | +} |
0 commit comments