-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlist-view-tests.spec.ts
More file actions
189 lines (175 loc) · 6.12 KB
/
list-view-tests.spec.ts
File metadata and controls
189 lines (175 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { Component, Input, NO_ERRORS_SCHEMA, ViewChild } from '@angular/core';
import { TestBed, waitForAsync } from '@angular/core/testing';
import { ListViewComponent, NativeScriptCommonModule, TemplateKeyDirective } from '@nativescript/angular';
// import trace = require("trace");
// trace.setCategories("ns-list-view, " + trace.categories.Navigation);
// trace.enable();
class DataItem {
constructor(
public id: number,
public name: string,
) {}
}
const ITEMS = [new DataItem(0, 'data item 0'), new DataItem(1, 'data item 1'), new DataItem(2, 'data item 2')];
let testTemplates: { first: number; second: number };
@Component({
selector: 'list-view-setupItemView',
template: `
<GridLayout>
<ListView [items]="myItems" (setupItemView)="onSetupItemView($event)">
<ng-template let-item="item">
<Label [text]="'[' + item.id + '] ' + item.name"></Label>
</ng-template>
</ListView>
</GridLayout>
`,
imports: [ListViewComponent, NativeScriptCommonModule],
schemas: [NO_ERRORS_SCHEMA],
})
export class TestListViewComponent {
public myItems: Array<DataItem> = ITEMS;
public counter = 0;
onSetupItemView(args) {
this.counter++;
}
}
@Component({
selector: 'item-component',
template: `<Label text="template"></Label>`,
imports: [NativeScriptCommonModule],
schemas: [NO_ERRORS_SCHEMA],
})
export class ItemTemplateComponent {
@Input() set templateName(value: any) {
if (value === 'first') {
testTemplates.first = testTemplates.first + 1;
} else if (value === 'second') {
testTemplates.second = testTemplates.second + 1;
} else {
throw new Error('Unexpected templateName: ' + value);
}
}
}
@Component({
selector: 'list-with-template-selector',
template: `
<GridLayout>
<ListView [items]="myItems" [itemTemplateSelector]="templateSelector">
<ng-template nsTemplateKey="first">
<item-component templateName="first"></item-component>
</ng-template>
<ng-template nsTemplateKey="second" let-item="item">
<item-component templateName="second"></item-component>
</ng-template>
</ListView>
</GridLayout>
`,
imports: [ListViewComponent, TemplateKeyDirective, ItemTemplateComponent, NativeScriptCommonModule],
schemas: [NO_ERRORS_SCHEMA],
})
export class TestListViewSelectorComponent {
public myItems: Array<DataItem> = ITEMS;
public templateSelector = (item: DataItem, index: number, items: any) => {
return item.id % 2 === 0 ? 'first' : 'second';
};
constructor() {
testTemplates = { first: 0, second: 0 };
}
}
@Component({
selector: 'list-with-template-selector-with-events',
template: `
<GridLayout>
<ListView [items]="myItems" [itemTemplateSelector]="templateSelector" (itemLoading)="dummyEvent($event)">
<ng-template nsTemplateKey="first" let-item="item">
<StackLayout>
<Label [text]="item.name" (loaded)="dummyEvent($event)"></Label>
<item-component templateName="first"></item-component>
</StackLayout>
</ng-template>
<ng-template nsTemplateKey="second" let-item="item">
<StackLayout>
<Label [text]="item.name" (loaded)="dummyEvent($event)"></Label>
<item-component templateName="second"></item-component>
</StackLayout>
</ng-template>
</ListView>
</GridLayout>
`,
imports: [ListViewComponent, TemplateKeyDirective, ItemTemplateComponent, NativeScriptCommonModule],
schemas: [NO_ERRORS_SCHEMA],
})
export class TestListViewSelectorWithEventsComponent {
public myItems: Array<DataItem> = ITEMS;
public templateSelector = (item: DataItem, index: number, items: any) => {
return item.id % 2 === 0 ? 'first' : 'second';
};
constructor() {
testTemplates = { first: 0, second: 0 };
}
dummyEvent(evt) {
//
}
}
@Component({
selector: 'list-view-default-item-template',
template: `
<GridLayout>
<ListView #listView [items]="myItems"></ListView>
</GridLayout>
`,
imports: [ListViewComponent],
schemas: [NO_ERRORS_SCHEMA],
})
export class TestDefaultItemTemplateComponent {
public myItems: Array<DataItem>;
constructor() {
this.myItems = new Array<DataItem>();
for (let i = 0; i < 100; i++) {
this.myItems.push(new DataItem(i, 'Name ' + i));
}
}
@ViewChild('listView', { static: false }) listViewElement: ListViewComponent;
onScrollListViewTo() {
this.listViewElement.nativeElement.scrollToIndex(100);
}
}
const declarations = [TestListViewComponent, TestListViewSelectorComponent, ItemTemplateComponent, TestDefaultItemTemplateComponent, TestListViewSelectorWithEventsComponent];
describe('ListView-tests', () => {
beforeEach(() =>
TestBed.configureTestingModule({
imports: declarations,
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents(),
);
it('setupItemView is called for every item', waitForAsync(async () => {
const fixture = TestBed.createComponent(TestListViewComponent);
fixture.detectChanges();
const component = fixture.componentRef.instance;
await fixture.whenRenderingDone();
expect(component.counter).toBe(3);
}));
it('itemTemplateSelector selects templates', waitForAsync(async () => {
const fixture = TestBed.createComponent(TestListViewSelectorComponent);
fixture.detectChanges();
await fixture.whenRenderingDone();
expect(testTemplates).toEqual({ first: 2, second: 1 });
}));
it("itemTemplateSelector doesn't break with events", waitForAsync(async () => {
try {
const fixture = TestBed.createComponent(TestListViewSelectorWithEventsComponent);
fixture.autoDetectChanges(true);
await fixture.whenRenderingDone();
expect(testTemplates).toEqual({ first: 2, second: 1 });
} catch (e) {
fail(e);
}
}));
it("'defaultTemplate' does not throw when list-view is scrolled", waitForAsync(async () => {
const fixture = TestBed.createComponent(TestDefaultItemTemplateComponent);
fixture.detectChanges();
const component = fixture.componentRef.instance;
await fixture.whenRenderingDone();
expect(component.onScrollListViewTo.bind(component)).not.toThrow();
}));
});