Skip to content

Commit

Permalink
fix(overview): use static methods of mock generating classes
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannLafore committed Apr 8, 2024
1 parent eab7b0f commit 0fca270
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
18 changes: 6 additions & 12 deletions test/viewmodels/home_view_model_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ void main() {
late MockGeoLocationService geoLocationService;
late FakeFirebaseFirestore fakeFireStore;

late MockPostFirestore mockPostFirestore;
late MockUserFirestore mockUserFirestore;
late UserRepositoryService userRepo;
late PostRepositoryService postRepo;

Expand Down Expand Up @@ -52,9 +50,6 @@ void main() {
],
);

mockPostFirestore = MockPostFirestore();
mockUserFirestore = MockUserFirestore();

when(geoLocationService.getCurrentPosition()).thenAnswer(
(_) async => userPosition,
);
Expand All @@ -67,7 +62,7 @@ void main() {
});

test("No posts are returned when they are far way from the user", () async {
final postData = mockPostFirestore.generatePostData(1)[0];
final postData = MockPostFirestore.generatePostData(1)[0];

await postRepo.addPost(
postData,
Expand All @@ -81,11 +76,11 @@ void main() {

test("Single near post returned correctly", () async {
// Add the post owner to the database
final owner = mockUserFirestore.generateUserFirestore(1)[0];
final owner = MockUserFirestore.generateUserFirestore(1)[0];
await userRepo.setUser(owner.uid, owner.data);

// Add the post to the database
final postData = mockPostFirestore.generatePostData(1).map((postData) {
final postData = MockPostFirestore.generatePostData(1).map((postData) {
return PostData(
ownerId: owner.uid, // Map to the owner
title: postData.title,
Expand Down Expand Up @@ -121,7 +116,7 @@ void main() {

test("Throws an exception when the owner of a post is not found", () async {
// Add the post to the database
final postData = mockPostFirestore.generatePostData(1).first;
final postData = MockPostFirestore.generatePostData(1).first;

await postRepo.addPost(
postData,
Expand All @@ -140,14 +135,13 @@ void main() {
const nbPosts = 10;

// Add the post owners to the database
final owners = mockUserFirestore.generateUserFirestore(nbOwners);
final owners = MockUserFirestore.generateUserFirestore(nbOwners);
for (final owner in owners) {
await userRepo.setUser(owner.uid, owner.data);
}

// Add the posts to the database
final postDatas = mockPostFirestore
.generatePostData(nbPosts)
final postDatas = MockPostFirestore.generatePostData(nbPosts)
.mapIndexed(
(index, element) => PostData(
ownerId: owners[index % nbOwners].uid, // Map to an owner
Expand Down
41 changes: 17 additions & 24 deletions test/viewmodels/home_view_model_unit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ void main() {
late UserRepositoryService userRepository;

late ProviderContainer container;
late MockPostFirestore mockPostFirestore;
late MockUserFirestore mockUserFirestore;

const point = GeoPoint(0, 0);

Expand All @@ -46,9 +44,6 @@ void main() {
),
],
);

mockPostFirestore = MockPostFirestore();
mockUserFirestore = MockUserFirestore();
});

test("No posts are returned when no posts returned by the repository",
Expand All @@ -69,9 +64,8 @@ void main() {
test(
"Post is returned correctly when single post is returned by the repository",
() async {
final owner = mockUserFirestore.generateUserFirestore(1)[0];
final postData = mockPostFirestore
.generatePostData(1)
final owner = MockUserFirestore.generateUserFirestore(1)[0];
final postData = MockPostFirestore.generatePostData(1)
.map(
(postData) => PostData(
ownerId: owner.uid,
Expand All @@ -82,7 +76,7 @@ void main() {
),
)
.toList()[0];
final post = mockPostFirestore.createPostAt(postData, point);
final post = MockPostFirestore.createPostAt(postData, point);

when(userRepository.getUser(post.data.ownerId)).thenAnswer(
(_) async => owner,
Expand Down Expand Up @@ -118,9 +112,8 @@ void main() {
"Posts are returned correctly when multiple posts are returned by the repository with all posts corresponding to the same owner",
() async {
// Generate the data for the test
final owner = mockUserFirestore.generateUserFirestore(1)[0];
final postsData = mockPostFirestore
.generatePostData(10)
final owner = MockUserFirestore.generateUserFirestore(1)[0];
final postsData = MockPostFirestore.generatePostData(10)
.map(
(postData) => PostData(
ownerId: owner.uid,
Expand All @@ -133,7 +126,7 @@ void main() {
.toList();

final posts = postsData.map((data) {
return mockPostFirestore.createPostAt(data, point);
return MockPostFirestore.createPostAt(data, point);
}).toList();

final expectedPosts = postsData.map((data) {
Expand Down Expand Up @@ -172,20 +165,20 @@ void main() {
const numberOfPosts = 10;

// Generate the data for the test
final owners = mockUserFirestore.generateUserFirestore(numberOfPosts);
final owners = MockUserFirestore.generateUserFirestore(numberOfPosts);
final postsData =
mockPostFirestore.generatePostData(numberOfPosts).mapIndexed(
(index, element) => PostData(
ownerId: owners[index].uid,
title: element.title,
description: element.description,
publicationTime: element.publicationTime,
voteScore: element.voteScore,
),
);
MockPostFirestore.generatePostData(numberOfPosts).mapIndexed(
(index, element) => PostData(
ownerId: owners[index].uid,
title: element.title,
description: element.description,
publicationTime: element.publicationTime,
voteScore: element.voteScore,
),
);

final posts = postsData.map((data) {
return mockPostFirestore.createPostAt(data, point);
return MockPostFirestore.createPostAt(data, point);
}).toList();

final expectedPosts = postsData.mapIndexed(
Expand Down

0 comments on commit 0fca270

Please sign in to comment.