Skip to content

Commit a09d2a8

Browse files
committed
fix(virtual-repeat): better handles repeating with table row / table body
fixes #84 fixes #128
1 parent fb67af5 commit a09d2a8

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

test/utilities.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function createAssertionQueue(): Queue {
1010
let func = queue.shift();
1111
func();
1212
next();
13-
});
13+
}, 1);
1414
}
1515
};
1616

@@ -22,7 +22,11 @@ export function createAssertionQueue(): Queue {
2222
};
2323
}
2424

25-
export function validateState(virtualRepeat: VirtualRepeat, viewModel: any, itemHeight: number) {
25+
/**
26+
*
27+
* @param extraHeight height of static content that contributes to overall heigh. Happen in case of table
28+
*/
29+
export function validateState(virtualRepeat: VirtualRepeat, viewModel: any, itemHeight: number, extraHeight?: number) {
2630
let views = virtualRepeat.viewSlot.children;
2731
let expectedHeight = viewModel.items.length * itemHeight;
2832
let topBufferHeight = virtualRepeat.topBuffer.getBoundingClientRect().height;

test/virtual-repeat-integration.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { PLATFORM } from 'aurelia-pal';
77
import { createAssertionQueue, validateState, validateScrolledState } from './utilities';
88
import { VirtualRepeat } from '../src/virtual-repeat';
99

10+
PLATFORM.moduleName('src/virtual-repeat');
11+
PLATFORM.moduleName('test/noop-value-converter');
12+
PLATFORM.moduleName('src/infinite-scroll-next');
1013

1114
describe('VirtualRepeat Integration', () => {
1215

@@ -95,7 +98,7 @@ describe('VirtualRepeat Integration', () => {
9598
nq(() => done());
9699
}
97100

98-
function validateSplice(virtualRepeat, viewModel, done) {
101+
function validateSplice(virtualRepeat: VirtualRepeat, viewModel: any, done: Function) {
99102
viewModel.items.splice(2, 1, 'x', 'y');
100103
nq(() => validateState(virtualRepeat, viewModel, itemHeight));
101104
nq(() => done());
@@ -138,7 +141,7 @@ describe('VirtualRepeat Integration', () => {
138141
items.push('item' + i);
139142
}
140143
component = StageComponent
141-
.withResources(PLATFORM.moduleName('src/virtual-repeat'))
144+
.withResources('src/virtual-repeat')
142145
.inView(`<div style="height: ${itemHeight}px;" virtual-repeat.for="item of items">\${item}</div>`)
143146
.boundTo({ items: items });
144147

@@ -436,7 +439,7 @@ describe('VirtualRepeat Integration', () => {
436439
spyOn(nestedVm, 'getNextPage').and.callThrough();
437440

438441
component = StageComponent
439-
.withResources(['src/virtual-repeat', PLATFORM.moduleName('src/infinite-scroll-next')])
442+
.withResources(['src/virtual-repeat', 'src/infinite-scroll-next'])
440443
.inView(`<div id="scrollContainer" style="height: 500px; overflow-y: scroll">
441444
<div style="height: ${itemHeight}px;" virtual-repeat.for="item of items" infinite-scroll-next="getNextPage">\${item}</div>
442445
</div>`)

test/virtual-repeat-integration.table.spec.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { VirtualRepeat } from '../src/virtual-repeat';
77
PLATFORM.moduleName('src/virtual-repeat');
88
PLATFORM.moduleName('test/noop-value-converter');
99

10-
fdescribe('VirtualRepeat Integration', () => {
10+
describe('VirtualRepeat Integration', () => {
1111
const itemHeight = 100;
1212
const nq = createAssertionQueue();
1313

@@ -80,8 +80,10 @@ fdescribe('VirtualRepeat Integration', () => {
8080
});
8181

8282
afterEach(() => {
83-
if (component) {
84-
return component.cleanUp();
83+
try {
84+
component.cleanUp();
85+
} catch (ex) {
86+
console.log('Error cleaning up component');
8587
}
8688
});
8789

@@ -92,7 +94,6 @@ fdescribe('VirtualRepeat Integration', () => {
9294
virtualRepeat = component.sut;
9395
viewModel = component.viewModel;
9496
});
95-
const element = virtualRepeat['element'];
9697
const { topBuffer, bottomBuffer } = virtualRepeat;
9798
expect(topBuffer.nextElementSibling.tagName).toBe('TBODY');
9899
expect(topBuffer.tagName).toBe('TR');
@@ -127,6 +128,36 @@ fdescribe('VirtualRepeat Integration', () => {
127128
done.fail(ex);
128129
}
129130
});
131+
132+
it('works with static row', async done => {
133+
try {
134+
component.inView(
135+
// there is a small border spacing between tbodies, rows that will add up
136+
// need to add border spacing 0 for testing purposes
137+
`<table style="border-spacing: 0">
138+
<tr><td>Name</td></tr>
139+
<tbody virtual-repeat.for="item of items">
140+
<tr style="height: ${itemHeight}px;"><td>\${item}</td></tr>
141+
</tbody>
142+
</table>`);
143+
144+
await component.create().then(() => {
145+
virtualRepeat = component.sut;
146+
viewModel = component.viewModel;
147+
});
148+
const element = virtualRepeat['element'];
149+
const table = element.parentNode;
150+
expect(table.firstElementChild).toBe(virtualRepeat.topBuffer.previousElementSibling);
151+
expect(table.firstElementChild.innerHTML.trim()).toBe('<tr><td>Name</td></tr>');
152+
nq(() => validateState(virtualRepeat, viewModel, itemHeight));
153+
nq(() => validatePush(virtualRepeat, viewModel, () => {
154+
155+
done();
156+
}));
157+
} catch (ex) {
158+
done.fail(ex);
159+
}
160+
});
130161
});
131162

132163
function validatePush(virtualRepeat: VirtualRepeat, viewModel: any, done: Function) {

0 commit comments

Comments
 (0)