fix(dashboard): improve performance with OnPush change detection (#440)
* fix(dashboard): improve performance with OnPush change detection * fix(dashboard): refactor book card component for improved readability and performance * fix(tests): mock matchMedia for improved test compatibility * fix(tests): matchMedia mock for better CI compatibility and support * fix(dashboard): add clearAll method to BookBrowserScrollService and update tests * fix(dashboard): improve performance with content visibility and refactor styles * fix(dashboard): improve scroll performance with estimated total height and refactor layout * fix(tests): add mock for getCurrentUser in userService for improved test coverage * fix(dashboard): implement infinite scroll for book loading and scroll restoration * fix(tests): fix lint error * fix(tests): mock ResizeObserver and IntersectionObserver * fix(tests): mock ResizeObserver and IntersectionObserver (again) * fix(dashboard): improve accessibility and performance by updating aria attributes and optimizing animations * fix(dashboard): improve submenu accessibility and optimize virtual scroller performance * fix(dashboard): clean up imports and improve component structure * fix(dashboard): correct mobile check in author browser component * fix(dashboard): add OnDestroy lifecycle hook to BookBrowserComponent
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { chunk } from './array.util';
|
||||
|
||||
describe('ArrayUtils', () => {
|
||||
describe('chunk', () => {
|
||||
it('should split an array into chunks of the specified size', () => {
|
||||
const input = [1, 2, 3, 4, 5];
|
||||
const result = chunk(input, 2);
|
||||
expect(result).toEqual([[1, 2], [3, 4], [5]]);
|
||||
});
|
||||
|
||||
it('should return an empty array if the input array is empty', () => {
|
||||
const input: number[] = [];
|
||||
const result = chunk(input, 2);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return the original array in a single chunk if the size is larger than the array length', () => {
|
||||
const input = [1, 2, 3];
|
||||
const result = chunk(input, 5);
|
||||
expect(result).toEqual([[1, 2, 3]]);
|
||||
});
|
||||
|
||||
it('should handle a chunk size of 1', () => {
|
||||
const input = [1, 2, 3];
|
||||
const result = chunk(input, 1);
|
||||
expect(result).toEqual([[1], [2], [3]]);
|
||||
});
|
||||
|
||||
it('should return the entire array in a single chunk if the size is 0 or negative', () => {
|
||||
const input = [1, 2, 3];
|
||||
expect(chunk(input, 0)).toEqual([[1, 2, 3]]);
|
||||
expect(chunk(input, -1)).toEqual([[1, 2, 3]]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Splits an array into chunks of a given size.
|
||||
*
|
||||
* @param array The array to split.
|
||||
* @param size The size of each chunk.
|
||||
* @returns An array of chunks.
|
||||
*/
|
||||
export function chunk<T>(array: T[], size: number): T[][] {
|
||||
if (size <= 0) {
|
||||
return array.length > 0 ? [[...array]] : [];
|
||||
}
|
||||
|
||||
const chunks: T[][] = [];
|
||||
for (let i = 0; i < array.length; i += size) {
|
||||
chunks.push(array.slice(i, i + size));
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export function computeGridColumns(containerWidth: number, minColumnWidth: number, gap: number = 21): number {
|
||||
if (containerWidth <= 0 || minColumnWidth <= 0) {
|
||||
return 1;
|
||||
}
|
||||
return Math.max(1, Math.floor((containerWidth + gap) / (minColumnWidth + gap)));
|
||||
}
|
||||
Reference in New Issue
Block a user