Skip to content

Commit fe9f433

Browse files
committed
fix(tests): update/add tests, add comments, rename variables for better readability
1 parent 163ed8e commit fe9f433

File tree

6 files changed

+243
-73
lines changed

6 files changed

+243
-73
lines changed

src/array-virtual-repeat-strategy.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class ArrayVirtualRepeatStrategy extends ArrayRepeatStrategy implements I
4646
}
4747

4848
/**@internal */
49-
_inPlaceProcessItems(repeat: VirtualRepeat, items: any[], first: number): void {
49+
_inPlaceProcessItems(repeat: VirtualRepeat, items: any[], firstIndex: number): void {
5050
const currItemCount = items.length;
5151
if (currItemCount === 0) {
5252
repeat.removeAllViews(/*return to cache?*/true, /*skip animation?*/false);
@@ -70,17 +70,17 @@ export class ArrayVirtualRepeatStrategy extends ArrayRepeatStrategy implements I
7070
}
7171
const local = repeat.local;
7272
const lastIndex = currItemCount - 1;
73-
if (first + realViewsCount > lastIndex) {
73+
if (firstIndex + realViewsCount > lastIndex) {
7474
// first = currItemCount - realViewsCount instead of: first = currItemCount - 1 - realViewsCount;
7575
// this is because during view update
7676
// view(i) starts at 0 and ends at less than last
77-
first = Math.max(0, currItemCount - realViewsCount);
77+
firstIndex = Math.max(0, currItemCount - realViewsCount);
7878
}
7979

80-
repeat._first = first;
80+
repeat._first = firstIndex;
8181
// re-evaluate bindings on existing views.
8282
for (let i = 0; i < realViewsCount; i++) {
83-
const currIndex = i + first;
83+
const currIndex = i + firstIndex;
8484
const view = repeat.view(i);
8585
const last = currIndex === currItemCount - 1;
8686
const middle = currIndex !== 0 && !last;

src/virtual-repeat.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class VirtualRepeat extends AbstractRepeater {
7070
* @internal
7171
* First view index, for proper follow up calculations
7272
*/
73-
_first = 0;
73+
_first: number = 0;
7474

7575
/**
7676
* @internal
@@ -376,6 +376,8 @@ export class VirtualRepeat extends AbstractRepeater {
376376
* - handle scroll as if scroll event happened
377377
*/
378378
itemsChanged(): void {
379+
// the current collection subscription may be irrelevant
380+
// unsubscribe and resubscribe later
379381
this._unsubscribeCollection();
380382
// still bound? and still attached?
381383
if (!this.scope || !this._isAttached) {
@@ -385,11 +387,11 @@ export class VirtualRepeat extends AbstractRepeater {
385387
let previousLastViewIndex = this._getIndexOfLastView();
386388

387389
const items = this.items;
388-
const currentItemCount = items.length;
389390
const shouldCalculateSize = !!items;
390391
const strategy = this.strategy = this.strategyLocator.getStrategy(items);
391-
392+
392393
if (shouldCalculateSize) {
394+
const currentItemCount = items.length;
393395
if (currentItemCount > 0 && this.viewCount() === 0) {
394396
strategy.createFirstItem(this);
395397
}
@@ -412,6 +414,7 @@ export class VirtualRepeat extends AbstractRepeater {
412414
strategy.instanceChanged(this, items, this._first);
413415

414416
if (shouldCalculateSize) {
417+
const currentItemCount = items.length;
415418
// Reset rebinding
416419
this._lastRebind = this._first;
417420

@@ -534,31 +537,31 @@ export class VirtualRepeat extends AbstractRepeater {
534537
this._skipNextScrollHandle = false;
535538
return;
536539
}
537-
if (!this.items) {
540+
const items = this.items;
541+
if (!items) {
538542
return;
539543
}
540-
let itemHeight = this.itemHeight;
541-
let scrollTop = this._fixedHeightContainer
544+
const itemHeight = this.itemHeight;
545+
const scrollTop = this._fixedHeightContainer
542546
? this.scrollContainer.scrollTop
543547
: (pageYOffset - this.distanceToTop);
544548

545549
// Calculate the index of first view
546550
// Using Math floor to ensure it has correct space for both small and large calculation
547-
let firstViewIndex = itemHeight > 0 ? Math.floor(scrollTop / itemHeight) : 0;
548-
this._first = firstViewIndex < 0 ? 0 : firstViewIndex;
551+
let firstIndex = itemHeight > 0 ? Math.floor(scrollTop / itemHeight) : 0;
552+
this._first = firstIndex < 0 ? 0 : firstIndex;
549553
// if first index starts somewhere after the last view
550554
// then readjust based on the delta
551-
if (this._first > this.items.length - this.elementsInView) {
552-
firstViewIndex = this.items.length - this.elementsInView;
553-
this._first = firstViewIndex < 0 ? 0 : firstViewIndex;
555+
if (firstIndex > items.length - this.elementsInView) {
556+
this._first = Math.max(0, items.length - this.elementsInView);
554557
}
555558

556559
// Check scrolling states and adjust flags
557560
this._checkScrolling();
558561

559562
// store buffers' heights into local variables
560-
let currentTopBufferHeight = this._topBufferHeight;
561-
let currentBottomBufferHeight = this._bottomBufferHeight;
563+
const currentTopBufferHeight = this._topBufferHeight;
564+
const currentBottomBufferHeight = this._bottomBufferHeight;
562565

563566
// TODO if and else paths do almost same thing, refactor?
564567
if (this._scrollingDown) {

test/interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export declare class ITestAppInterface<T> {
2+
items: T[];
3+
}

test/utilities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ export async function scrollToIndex(virtualRepeat: VirtualRepeat, itemIndex: num
128128
}
129129

130130
/**
131-
* Wait for a small time for repeat to finish processing
131+
* Wait for a small time for repeat to finish processing.
132+
*
133+
* Default to 10
132134
*/
133135
export async function ensureScrolled(time: number = 10): Promise<void> {
134136
await waitForTimeout(time);

test/virtual-repeat-integration.spec.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import './setup';
22
import { StageComponent } from './component-tester';
33
import { PLATFORM } from 'aurelia-pal';
4-
import { createAssertionQueue, validateState, validateScrolledState, AsyncQueue } from './utilities';
4+
import { createAssertionQueue, validateState, validateScrolledState, AsyncQueue, waitForTimeout } from './utilities';
55
import { VirtualRepeat } from '../src/virtual-repeat';
66

77
PLATFORM.moduleName('src/virtual-repeat');
@@ -123,7 +123,7 @@ describe('VirtualRepeat Integration', () => {
123123

124124
describe('iterating div', () => {
125125
let component;
126-
let virtualRepeat;
126+
let virtualRepeat: VirtualRepeat;
127127
let viewModel;
128128
let create;
129129
let items;
@@ -308,19 +308,17 @@ describe('VirtualRepeat Integration', () => {
308308
create.then(() => validateArrayChange(virtualRepeat, viewModel, done));
309309
});
310310

311-
it('handles array changes with null / undefined', done => {
312-
create.then(() => {
313-
viewModel.items = null;
311+
it('handles array changes with null / undefined', async (done) => {
312+
await create;
313+
viewModel.items = null;
314+
await waitForTimeout(50);
314315

315-
setTimeout(() => {
316-
let topBufferHeight = virtualRepeat.topBuffer.getBoundingClientRect().height;
317-
let bottomBufferHeight = virtualRepeat.bottomBuffer.getBoundingClientRect().height;
316+
let topBufferHeight = virtualRepeat.topBufferEl.getBoundingClientRect().height;
317+
let bottomBufferHeight = virtualRepeat.bottomBufferEl.getBoundingClientRect().height;
318318

319-
expect(topBufferHeight + bottomBufferHeight).toBe(0);
319+
expect(topBufferHeight + bottomBufferHeight).toBe(0);
320320

321-
validateArrayChange(virtualRepeat, viewModel, done);
322-
}, 1000);
323-
});
321+
validateArrayChange(virtualRepeat, viewModel, done);
324322
});
325323
});
326324

0 commit comments

Comments
 (0)