Skip to content

Commit

Permalink
feat: user item list provider with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumetavernier committed May 19, 2024
1 parent 2122904 commit 61d1da8
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
52 changes: 52 additions & 0 deletions lib/greencode/providers/user_greencode_item_list_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:myecl/greencode/class/greencode_item.dart';
import 'package:myecl/greencode/repositories/greencode_item_repository.dart';
import 'package:myecl/tools/providers/list_notifier.dart';
import 'package:myecl/tools/token_expire_wrapper.dart';

class UserGreenCodeItemListNotifier extends ListNotifier<GreenCodeItem> {
final GreenCodeItemRepository greencodeItemRepository;
UserGreenCodeItemListNotifier({required this.greencodeItemRepository})
: super(const AsyncValue.loading());

Future<AsyncValue<List<GreenCodeItem>>>
loadCurrentUserGreenCodeItemList() async {
return await loadList(greencodeItemRepository.getCurrentUserGreenCodeItems);
}

Future<bool> addCurrentUserToGreenCodeItem(
GreenCodeItem greencodeItem,
) async {
return await add(
(GreenCodeItem item) async {
await greencodeItemRepository.addCurrentUserToGreenCodeItem(item.id);
return item;
},
greencodeItem,
);
}

Future<bool> removeCurrentUserFromGreenCodeItem(
GreenCodeItem greencodeItem,
) async {
return delete(
greencodeItemRepository.removeCurrentUserFromGreenCodeItem,
(items, item) =>
items..removeWhere((itemToCheck) => itemToCheck.id == item.id),
greencodeItem.id,
greencodeItem,
);
}
}

final greencodeItemListProvider = StateNotifierProvider<
UserGreenCodeItemListNotifier, AsyncValue<List<GreenCodeItem>>>((ref) {
final greencodeItemRepository = ref.watch(greencodeItemRepositoryProvider);
final greencodeItemListNotifier = UserGreenCodeItemListNotifier(
greencodeItemRepository: greencodeItemRepository,
);
tokenExpireWrapperAuth(ref, () async {
await greencodeItemListNotifier.loadCurrentUserGreenCodeItemList();
});
return greencodeItemListNotifier;
});
109 changes: 109 additions & 0 deletions test/greencode/user_greencode_item_list_provider_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:mocktail/mocktail.dart';
import 'package:myecl/greencode/class/greencode_item.dart';
import 'package:myecl/greencode/providers/user_greencode_item_list_provider.dart';
import 'package:myecl/greencode/repositories/greencode_item_repository.dart';

class MockGreenCodeItemRepository extends Mock
implements GreenCodeItemRepository {}

final greencodeItems = [
GreenCodeItem.empty().copyWith(
id: '1',
qrCodeContent: 'QRCodeContent 1',
title: 'GreenCodeItem 1',
content: 'GreenCodeItem 1 content',
),
GreenCodeItem.empty().copyWith(
id: '2',
qrCodeContent: 'QRCodeContent 2',
title: 'GreenCodeItem 2',
content: 'GreenCodeItem 2 content',
),
GreenCodeItem.empty().copyWith(
id: '3',
qrCodeContent: 'QRCodeContent 3',
title: 'GreenCodeItem 3',
content: 'GreenCodeItem 3 content',
),
];
void main() {
group('GreenCodeItemListNotifier', () {
late UserGreenCodeItemListNotifier greencodeItemListNotifier;

setUp(() {
greencodeItemListNotifier = UserGreenCodeItemListNotifier(
greencodeItemRepository: MockGreenCodeItemRepository(),
);
});

test('loadGreenCodeItemList', () async {
when(
() => greencodeItemListNotifier.greencodeItemRepository
.getCurrentUserGreenCodeItems(),
).thenAnswer((_) async => greencodeItems);
await greencodeItemListNotifier.loadCurrentUserGreenCodeItemList();
expect(
greencodeItemListNotifier.state.when(
data: (greencodeItems) => greencodeItems,
loading: () => [],
error: (error, stackTrace) => [],
),
greencodeItems,
);
});

test('addCurrentUserToGreenCodeItem', () async {
final newGreenCodeItem = GreenCodeItem.empty().copyWith(
id: '4',
qrCodeContent: 'QRCodeContent 4',
title: 'GreenCodeItem 4',
content: 'GreenCodeItem 4 content',
);
when(
() => greencodeItemListNotifier.greencodeItemRepository
.addCurrentUserToGreenCodeItem(newGreenCodeItem.id),
).thenAnswer((_) async => true);
greencodeItemListNotifier.state =
AsyncValue.data(greencodeItems.sublist(0));
await greencodeItemListNotifier
.addCurrentUserToGreenCodeItem(newGreenCodeItem);

expect(
greencodeItemListNotifier.state.when(
data: (greencodeItems) => greencodeItems,
loading: () => [],
error: (error, stackTrace) => [],
),
greencodeItems + [newGreenCodeItem],
);
});

test('removeCurrentUserFromGreenCodeItem', () async {
final deletedGreenCodeItem = greencodeItems[1];
when(
() => greencodeItemListNotifier.greencodeItemRepository
.removeCurrentUserFromGreenCodeItem(deletedGreenCodeItem.id),
).thenAnswer((_) async => true);

greencodeItemListNotifier.state =
AsyncValue.data(greencodeItems.sublist(0));
await greencodeItemListNotifier
.removeCurrentUserFromGreenCodeItem(deletedGreenCodeItem);

expect(
greencodeItemListNotifier.state.when(
data: (greencodeItems) => greencodeItems,
loading: () => [],
error: (error, stackTrace) => [],
),
greencodeItems
.where(
(greenCodeItem) => greenCodeItem.id != deletedGreenCodeItem.id,
)
.toList(),
);
});
});
}

0 comments on commit 61d1da8

Please sign in to comment.