Skip to content

Commit e99e7ce

Browse files
nrobakovarkaraivanov
authored andcommitted
chore(*): add clipboard tests
1 parent 74131cd commit e99e7ce

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}

projects/igniteui-angular/src/lib/test-utils/grid-samples.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,3 +1083,19 @@ export class IgxTestExcelFilteringDatePickerComponent extends IgxGridFilteringCo
10831083
this.cd.detectChanges();
10841084
}
10851085
}
1086+
1087+
@Component({
1088+
template: `
1089+
<igx-grid [data]="data" height="500px" width="500px">
1090+
<igx-column width="100px" [field]="'ID'" [header]="'ID'"></igx-column>
1091+
<igx-column width="100px" [field]="'ProductName'" [editable]="true" [header]="'ProductNameHeader'"
1092+
[formatter]="formatter"></igx-column>
1093+
<igx-column width="100px" [field]="'Downloads'" [editable]="true" dataType="number" [header]="'Downloads'"></igx-column>
1094+
<igx-column width="100px" [field]="'Released'" [editable]="true" dataType="boolean" [header]="'Released'"></igx-column>
1095+
<igx-column width="100px" [field]="'ReleaseDate'" [header]="'ReleaseDate'" dataType="date"></igx-column>
1096+
</igx-grid>`
1097+
})
1098+
export class IgxGridClipboardComponent extends BasicGridComponent {
1099+
public data = SampleTestData.excelFilteringData();
1100+
formatter = (value: any) => `** ${value} **`;
1101+
}

0 commit comments

Comments
 (0)