-
Notifications
You must be signed in to change notification settings - Fork 3
/
paginate.directive.spec.ts
105 lines (90 loc) · 2.85 KB
/
paginate.directive.spec.ts
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
import { Component, DebugElement } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { PaginateDirective } from './paginate.directive';
import { click, expectText, findEls } from './spec-helpers/element.spec-helper';
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
@Component({
template: `
<ul>
<li
*appPaginate="let item of items; perPage: 3; controls: controls"
data-testid="item"
>
{{ item }}
</li>
</ul>
<ng-template
#controls
let-previousPage="previousPage"
let-page="page"
let-pages="pages"
let-nextPage="nextPage"
>
<button (click)="previousPage()" data-testid="previousPage">
Previous page
</button>
<span data-testid="page">{{ page }}</span>
/
<span data-testid="pages">{{ pages }}</span>
<button (click)="nextPage()" data-testid="nextPage">Next page</button>
</ng-template>
`,
})
class HostComponent {
public items = items;
}
function expectItems(elements: DebugElement[], expectedItems: number[]): void {
elements.forEach((element, index) => {
const actualText = element.nativeElement.textContent.trim();
expect(actualText).toBe(String(expectedItems[index]));
});
}
describe('PaginateDirective', () => {
let fixture: ComponentFixture<HostComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [PaginateDirective, HostComponent],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(HostComponent);
fixture.detectChanges();
});
it('renders the items of the first page', () => {
const els = findEls(fixture, 'item');
expect(els.length).toBe(3);
expectItems(els, [1, 2, 3]);
});
it('renders the current page and total pages', () => {
expectText(fixture, 'page', '1');
expectText(fixture, 'pages', '4');
});
it('shows the next page', () => {
click(fixture, 'nextPage');
fixture.detectChanges();
const els = findEls(fixture, 'item');
expect(els.length).toBe(3);
expectItems(els, [4, 5, 6]);
});
it('shows the previous page', () => {
click(fixture, 'nextPage');
click(fixture, 'previousPage');
fixture.detectChanges();
const els = findEls(fixture, 'item');
expect(els.length).toBe(3);
expectItems(els, [1, 2, 3]);
});
it('checks the pages bounds', () => {
click(fixture, 'nextPage'); // -> 2
click(fixture, 'nextPage'); // -> 3
click(fixture, 'nextPage'); // -> 4
click(fixture, 'previousPage'); // -> 3
click(fixture, 'previousPage'); // -> 2
click(fixture, 'previousPage'); // -> 1
fixture.detectChanges();
// Expect that the first page is visible again
const els = findEls(fixture, 'item');
expect(els.length).toBe(3);
expectItems(els, [1, 2, 3]);
});
});