Skip to content

Commit

Permalink
Added test.
Browse files Browse the repository at this point in the history
  • Loading branch information
bosskmk committed Jun 6, 2022
1 parent 1f55268 commit a9e6597
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 6 deletions.
17 changes: 12 additions & 5 deletions lib/src/manager/pluto_grid_state_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class PlutoGridStateManager extends PlutoGridState {
List<PlutoRow> refRows, {
bool forceApplySortIdx = false,
bool increase = true,
int? start = 0,
int start = 0,
}) {
if (refColumns.isEmpty || refRows.isEmpty) {
return refRows;
Expand All @@ -114,7 +114,7 @@ class PlutoGridStateManager extends PlutoGridState {
_ApplyRowForSortIdx(
forceApply: forceApplySortIdx,
increase: increase,
start: start ?? 0,
start: start,
firstRow: refRows.first,
),
]);
Expand All @@ -137,17 +137,21 @@ class PlutoGridStateManager extends PlutoGridState {
List<PlutoRow> refRows, {
bool forceApplySortIdx = false,
bool increase = true,
int? start = 0,
int start = 0,
int chunkSize = 100,
Duration duration = const Duration(milliseconds: 1),
}) {
if (refColumns.isEmpty || refRows.isEmpty) {
return Future.value(refRows);
}

assert(chunkSize > 0);

final Completer<List<PlutoRow>> completer = Completer();

SplayTreeMap<int, List<PlutoRow>> _rows = SplayTreeMap();

final Iterable<List<PlutoRow>> chunks = refRows.slices(100);
final Iterable<List<PlutoRow>> chunks = refRows.slices(chunkSize);

final chunksLength = chunks.length;

Expand All @@ -156,7 +160,7 @@ class PlutoGridStateManager extends PlutoGridState {
(index) => index,
);

Timer.periodic(const Duration(milliseconds: 1), (timer) {
Timer.periodic(duration, (timer) {
if (chunksIndexes.isEmpty) {
return;
}
Expand All @@ -169,6 +173,9 @@ class PlutoGridStateManager extends PlutoGridState {
return PlutoGridStateManager.initializeRows(
refColumns,
chunk,
forceApplySortIdx: forceApplySortIdx,
increase: increase,
start: start + (chunkIndex * chunkSize),
);
}).then((value) {
_rows[chunkIndex] = value;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/manager/state/row_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ mixin RowState implements IPlutoGridState {
PlutoGridStateManager.initializeRows(
refColumns,
rows,
start: sortIdx,
start: sortIdx ?? 0,
);

for (var i = 0; i < refRows.originalLength; i += 1) {
Expand Down
170 changes: 170 additions & 0 deletions test/src/manager/pluto_grid_state_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:mockito/mockito.dart';
import 'package:pluto_grid/pluto_grid.dart';

import '../../helper/column_helper.dart';
import '../../helper/row_helper.dart';

class _MockScrollController extends Mock implements ScrollController {}

Expand Down Expand Up @@ -387,4 +388,173 @@ void main() {
expect(rows[2].cells['field']!.column, columns.first);
});
});

group('initializeRowsAsync', () {
test('chunkSize 가 0 이면 assert 가 발생 되어야 한다.', () async {
final List<PlutoColumn> columns = ColumnHelper.textColumn('title');

final List<PlutoRow> rows = RowHelper.count(1, columns);

expect(
() async {
await PlutoGridStateManager.initializeRowsAsync(
columns,
rows,
chunkSize: 0,
);
},
throwsAssertionError,
);
});

test('chunkSize 가 -1 이면 assert 가 발생 되어야 한다.', () async {
final List<PlutoColumn> columns = ColumnHelper.textColumn('title');

final List<PlutoRow> rows = RowHelper.count(1, columns);

expect(
() async {
await PlutoGridStateManager.initializeRowsAsync(
columns,
rows,
chunkSize: -1,
);
},
throwsAssertionError,
);
});

test(
'sortIdx 가 null 로 설정 된 rows 를 sortIdx 시작 값을 변경하여 실행하면, '
'sortIdx 값을 0 으로 변경 하면 rows 의 sortIdx 가 변경 되고, '
'원래 순서대로 리턴 되어야 한다.', () async {
final List<PlutoColumn> columns = ColumnHelper.textColumn('title');

final List<PlutoRow> rows = RowHelper.count(100, columns);

for (var value in rows) {
value.sortIdx = null;
}

final Iterable<Key> rowKeys = rows.map((e) => e.key);

expect(rows.first.sortIdx, null);
expect(rows.last.sortIdx, null);

final initializedRows = await PlutoGridStateManager.initializeRowsAsync(
columns,
rows,
forceApplySortIdx: true,
start: 0,
chunkSize: 10,
duration: const Duration(milliseconds: 1),
);

for (int i = 0; i < initializedRows.length; i += 1) {
expect(initializedRows[i].sortIdx, i);
expect(initializedRows[i].key, rowKeys.elementAt(i));
}
});

test(
'sortIdx 가 0부터 설정 된 rows 를 sortIdx 시작 값을 변경하여 실행하면, '
'sortIdx 값을 10 으로 변경 하면 rows 의 sortIdx 가 변경 되고, '
'원래 순서대로 리턴 되어야 한다.', () async {
final List<PlutoColumn> columns = ColumnHelper.textColumn('title');

final List<PlutoRow> rows = RowHelper.count(100, columns);

final Iterable<Key> rowKeys = rows.map((e) => e.key);

expect(rows.first.sortIdx, 0);
expect(rows.last.sortIdx, 99);

final initializedRows = await PlutoGridStateManager.initializeRowsAsync(
columns,
rows,
forceApplySortIdx: true,
start: 10,
chunkSize: 10,
duration: const Duration(milliseconds: 1),
);

for (int i = 0; i < initializedRows.length; i += 1) {
expect(initializedRows[i].sortIdx, 10 + i);
expect(initializedRows[i].key, rowKeys.elementAt(i));
}
});

test(
'sortIdx 가 0부터 설정 된 rows 를 sortIdx 시작 값을 변경하여 실행하면, '
'sortIdx 값을 -10 으로 변경 하면 rows 의 sortIdx 가 변경 되고, '
'원래 순서대로 리턴 되어야 한다.', () async {
final List<PlutoColumn> columns = ColumnHelper.textColumn('title');

final List<PlutoRow> rows = RowHelper.count(100, columns);

final Iterable<Key> rowKeys = rows.map((e) => e.key);

expect(rows.first.sortIdx, 0);
expect(rows.last.sortIdx, 99);

final initializedRows = await PlutoGridStateManager.initializeRowsAsync(
columns,
rows,
forceApplySortIdx: true,
start: -10,
chunkSize: 10,
duration: const Duration(milliseconds: 1),
);

for (int i = 0; i < initializedRows.length; i += 1) {
expect(initializedRows[i].sortIdx, -10 + i);
expect(initializedRows[i].key, rowKeys.elementAt(i));
}
});

test(
'sortIdx 가 0부터 설정 된 rows 를 sortIdx 시작 값을 변경하여 실행하면, '
'sortIdx 값을 -10 으로 변경 하면 rows 의 sortIdx 가 변경 되고, '
'원래 순서대로 리턴 되어야 한다.', () async {
final List<PlutoColumn> columns = ColumnHelper.textColumn('title');

final List<PlutoRow> rows = RowHelper.count(100, columns);

final Iterable<Key> rowKeys = rows.map((e) => e.key);

expect(rows.first.sortIdx, 0);
expect(rows.last.sortIdx, 99);

final initializedRows = await PlutoGridStateManager.initializeRowsAsync(
columns,
rows,
forceApplySortIdx: true,
start: -10,
chunkSize: 10,
duration: const Duration(milliseconds: 1),
);

for (int i = 0; i < initializedRows.length; i += 1) {
expect(initializedRows[i].sortIdx, -10 + i);
expect(initializedRows[i].key, rowKeys.elementAt(i));
}
});

test('한개의 행을 전달하고 chunkSize 가 10 인경우 정상적으로 리턴 되어야 한다.', () async {
final List<PlutoColumn> columns = ColumnHelper.textColumn('title');

final List<PlutoRow> rows = RowHelper.count(1, columns);

final initializedRows = await PlutoGridStateManager.initializeRowsAsync(
columns,
rows,
forceApplySortIdx: true,
start: 99,
chunkSize: 10,
duration: const Duration(milliseconds: 1),
);

expect(initializedRows.first.sortIdx, 99);
});
});
}

0 comments on commit a9e6597

Please sign in to comment.