feat: Persist label state after get all labels
This commit is contained in:
@@ -4,170 +4,199 @@ import 'package:labels/model/label.dart';
|
||||
import 'package:tmail_ui_user/features/labels/presentation/utils/label_utils.dart';
|
||||
|
||||
void main() {
|
||||
Label createLabel(String id, String name) {
|
||||
return Label(id: Id(id), displayName: name);
|
||||
}
|
||||
|
||||
group('LabelUtils.applyLabelChanges', () {
|
||||
test('should add new labels when created list is not empty', () {
|
||||
final currentLabels = <Label>[];
|
||||
final newLabel = Label(id: Id('1'), displayName: 'Work');
|
||||
group('Create Operations', () {
|
||||
test('should add new labels when created list is not empty', () {
|
||||
final currentLabels = <Label>[];
|
||||
final newLabel = createLabel('1', 'Work');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [newLabel],
|
||||
updated: [],
|
||||
destroyedIds: [],
|
||||
);
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [newLabel],
|
||||
updated: [],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.id?.value, '1');
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.id?.value, '1');
|
||||
});
|
||||
|
||||
test(
|
||||
'should replace existing label if the same ID appears in the created list (Overwrite/Sync Fix)',
|
||||
() {
|
||||
final currentLabels = [createLabel('1', 'Old Version')];
|
||||
final newVersionLabel = createLabel('1', 'New Version');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [newVersionLabel],
|
||||
updated: [],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1,
|
||||
reason: 'List should not contain duplicates');
|
||||
expect(currentLabels.first.displayName, 'New Version');
|
||||
});
|
||||
});
|
||||
|
||||
test(
|
||||
'should update existing label when an updated label with the same ID is provided',
|
||||
() {
|
||||
final oldLabel = Label(id: Id('1'), displayName: 'Old Name');
|
||||
final currentLabels = <Label>[oldLabel];
|
||||
group('Update Operations', () {
|
||||
test('should update existing label when provided in updated list', () {
|
||||
final currentLabels = [createLabel('1', 'Old Name')];
|
||||
final updatedLabel = createLabel('1', 'New Name');
|
||||
|
||||
final updatedLabel = Label(id: Id('1'), displayName: 'New Name');
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [updatedLabel],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [updatedLabel],
|
||||
destroyedIds: [],
|
||||
);
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'New Name');
|
||||
});
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'New Name');
|
||||
test(
|
||||
'should treat updated labels as new additions if they do not exist (Upsert behavior)',
|
||||
() {
|
||||
final currentLabels = <Label>[];
|
||||
final labelToUpdate = createLabel('1', 'Ghost Label');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [labelToUpdate],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'Ghost Label');
|
||||
});
|
||||
|
||||
test('should move updated labels to the end of the list', () {
|
||||
final currentLabels = [
|
||||
createLabel('1', 'A'),
|
||||
createLabel('2', 'B'),
|
||||
createLabel('3', 'C'),
|
||||
];
|
||||
|
||||
final updatedLabelA = createLabel('1', 'A Updated');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [updatedLabelA],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 3);
|
||||
expect(currentLabels[0].id?.value, '2');
|
||||
expect(currentLabels[1].id?.value, '3');
|
||||
expect(currentLabels[2].id?.value, '1');
|
||||
expect(currentLabels[2].displayName, 'A Updated');
|
||||
});
|
||||
});
|
||||
|
||||
test('should remove labels when their ID is present in destroyedIds', () {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'To Delete'),
|
||||
Label(id: Id('2'), displayName: 'Keep Me'),
|
||||
];
|
||||
group('Destroy Operations', () {
|
||||
test('should remove labels when their ID is present in destroyedIds', () {
|
||||
final currentLabels = [
|
||||
createLabel('1', 'To Delete'),
|
||||
createLabel('2', 'Keep Me'),
|
||||
];
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [],
|
||||
destroyedIds: [Id('1')],
|
||||
);
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [],
|
||||
destroyedIds: [Id('1')],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.any((l) => l.id?.value == '1'), isFalse);
|
||||
expect(currentLabels.any((l) => l.id?.value == '2'), isTrue);
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.any((l) => l.id?.value == '1'), isFalse);
|
||||
expect(currentLabels.first.id?.value, '2');
|
||||
});
|
||||
|
||||
test('should handle destroying IDs that do not exist gracefully', () {
|
||||
final currentLabels = [createLabel('1', 'Keep Me')];
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [],
|
||||
destroyedIds: [Id('999')],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.id?.value, '1');
|
||||
});
|
||||
});
|
||||
|
||||
test('should handle create, update, and destroy operations simultaneously',
|
||||
() {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'Old'),
|
||||
Label(id: Id('2'), displayName: 'Remove'),
|
||||
];
|
||||
group('Complex Scenarios & Edge Cases', () {
|
||||
test(
|
||||
'should handle create, update, and destroy operations simultaneously',
|
||||
() {
|
||||
final currentLabels = [
|
||||
createLabel('1', 'To Update'),
|
||||
createLabel('2', 'To Destroy'),
|
||||
createLabel('3', 'To Replace via Create'),
|
||||
];
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [Label(id: Id('3'), displayName: 'New')],
|
||||
updated: [Label(id: Id('1'), displayName: 'Updated')],
|
||||
destroyedIds: [Id('2')],
|
||||
);
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [
|
||||
createLabel('4', 'New 4'),
|
||||
createLabel('3', 'Replaced 3'),
|
||||
],
|
||||
updated: [
|
||||
createLabel('1', 'Updated 1'),
|
||||
],
|
||||
destroyedIds: [Id('2')],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 2);
|
||||
expect(currentLabels.any((l) => l.displayName == 'Updated'), isTrue);
|
||||
expect(currentLabels.any((l) => l.displayName == 'New'), isTrue);
|
||||
expect(currentLabels.any((l) => l.id?.value == '2'), isFalse);
|
||||
});
|
||||
expect(currentLabels.length, 3);
|
||||
expect(currentLabels.any((l) => l.id?.value == '2'), isFalse);
|
||||
expect(currentLabels.any((l) => l.displayName == 'Updated 1'), isTrue);
|
||||
expect(currentLabels.any((l) => l.displayName == 'New 4'), isTrue);
|
||||
expect(currentLabels.any((l) => l.displayName == 'Replaced 3'), isTrue);
|
||||
expect(
|
||||
currentLabels.any((l) => l.displayName == 'To Replace via Create'),
|
||||
isFalse);
|
||||
});
|
||||
|
||||
test('should do nothing when all change lists are empty', () {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'Existing'),
|
||||
];
|
||||
test('should do nothing when all change lists are empty', () {
|
||||
final currentLabels = [createLabel('1', 'Existing')];
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [],
|
||||
destroyedIds: [],
|
||||
);
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'Existing');
|
||||
});
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'Existing');
|
||||
});
|
||||
|
||||
test(
|
||||
'should handle destroying IDs that do not exist in currentLabels gracefully',
|
||||
() {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'Keep Me'),
|
||||
];
|
||||
test(
|
||||
'should prioritize update over destroy if an ID appears in both lists',
|
||||
() {
|
||||
final currentLabels = [createLabel('1', 'Original')];
|
||||
final updatedLabel = createLabel('1', 'Revived');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [],
|
||||
destroyedIds: [Id('999')],
|
||||
);
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [updatedLabel],
|
||||
destroyedIds: [Id('1')],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.id?.value, '1');
|
||||
});
|
||||
|
||||
test(
|
||||
'should treat updated labels as new additions if they do not exist in currentLabels (Upsert behavior)',
|
||||
() {
|
||||
final currentLabels = <Label>[];
|
||||
final labelToUpdate = Label(id: Id('1'), displayName: 'Ghost Label');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [labelToUpdate],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'Ghost Label');
|
||||
});
|
||||
|
||||
test('should move updated labels to the end of the list', () {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'A'),
|
||||
Label(id: Id('2'), displayName: 'B'),
|
||||
Label(id: Id('3'), displayName: 'C'),
|
||||
];
|
||||
|
||||
final updatedLabelA = Label(id: Id('1'), displayName: 'A Updated');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [updatedLabelA],
|
||||
destroyedIds: [],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 3);
|
||||
expect(currentLabels[0].id?.value, '2');
|
||||
expect(currentLabels[1].id?.value, '3');
|
||||
expect(currentLabels[2].id?.value, '1');
|
||||
expect(currentLabels[2].displayName, 'A Updated');
|
||||
});
|
||||
|
||||
test('should prioritize update over destroy if an ID appears in both lists',
|
||||
() {
|
||||
final currentLabels = <Label>[
|
||||
Label(id: Id('1'), displayName: 'Original'),
|
||||
];
|
||||
|
||||
final updatedLabel = Label(id: Id('1'), displayName: 'Revived');
|
||||
|
||||
LabelUtils.applyLabelChanges(
|
||||
currentLabels: currentLabels,
|
||||
created: [],
|
||||
updated: [updatedLabel],
|
||||
destroyedIds: [Id('1')],
|
||||
);
|
||||
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'Revived');
|
||||
expect(currentLabels.length, 1);
|
||||
expect(currentLabels.first.displayName, 'Revived');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user