Skip to content

Commit 90aa82d

Browse files
Update: Unit tests
1 parent fe8bab2 commit 90aa82d

19 files changed

+289
-213
lines changed

lib/data/models/order/order_details_model.dart

+7-6
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ class OrderDetailsModel extends OrderDetails {
5959

6060
factory OrderDetailsModel.fromEntity(OrderDetails entity) =>
6161
OrderDetailsModel(
62-
id: entity.id,
63-
orderItems: entity.orderItems
64-
.map((orderItem) => OrderItemModel.fromEntity(orderItem))
65-
.toList(),
66-
deliveryInfo: DeliveryInfoModel.fromEntity(entity.deliveryInfo),
67-
discount: entity.discount);
62+
id: entity.id,
63+
orderItems: entity.orderItems
64+
.map((orderItem) => OrderItemModel.fromEntity(orderItem))
65+
.toList(),
66+
deliveryInfo: DeliveryInfoModel.fromEntity(entity.deliveryInfo),
67+
discount: entity.discount,
68+
);
6869
}

test/data/models/order/order_details_model_test.dart

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ void main() {
1818

1919
group('fromJson', () {
2020
test(
21-
'''Should successfully deserialize a JSON map into a OrderDetailsMap
22-
object and ensure that the resulting
23-
object matches the expected tOrderDetails''',
21+
'Should successfully deserialize a JSON map into an OrderDetailsModel and match expected result',
2422
() async {
2523
/// Arrange
2624
final Map<String, dynamic> jsonMap =

test/data/models/product/product_model_test.dart

+27-10
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,36 @@ void main() {
6161

6262
/// Assert
6363
final expectedMap = {
64-
'_id': '1',
65-
'name': 'name',
66-
'description': 'description',
67-
'priceTags': [
68-
{'_id': '1', 'name': 'name', 'price': 100}
64+
"_id": "64eb722a41cb9b05eb4420b7",
65+
"name": "Asus Gaming Mouse",
66+
"description": "Text description",
67+
"priceTags": [
68+
{
69+
"_id": "64eb728341cb9b05eb4420ba",
70+
"name": "White",
71+
"price": 50.99
72+
}
6973
],
70-
'categories': [
71-
{'_id': '1', 'name': 'name', 'image': 'image'}
74+
"categories": [
75+
{
76+
"_id": "64cecb613357eaec7b1ab31b",
77+
"name": "Headphone",
78+
"image": "https://res.cloudinary.com/dhyttttax/image/upload/v1693148015/category/headphone_pdqwo2.jpg"
79+
}
7280
],
73-
'images': ['image'],
74-
'createdAt': '2000-01-01T00:00:00.000',
75-
'updatedAt': '2000-01-01T00:00:00.000'
81+
"images": [
82+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/vxyyemcdwcuoooyejehj.jpg",
83+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/vqiw6cswpnzhgryd3s1l.jpg",
84+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/tkanjwktt2t0qvybk5xf.jpg",
85+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/yjxkgevogpaim02wonks.jpg",
86+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/m2bb9pzzobynrpyo9ike.jpg",
87+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/xhojjofgfyfpbjwo2vox.jpg"
88+
],
89+
"createdAt": "2023-08-27T15:56:26.504Z",
90+
"updatedAt": "2023-08-27T16:19:16.683Z"
7691
};
92+
93+
/// Assert
7794
expect(result, expectedMap);
7895
},
7996
);

test/data/repositories/product_repository_impl_test.dart

-38
Original file line numberDiff line numberDiff line change
@@ -140,43 +140,5 @@ void main() {
140140
},
141141
);
142142
});
143-
144-
runTestsOffline(() {
145-
test(
146-
'should return last locally cached data when the cached data is present',
147-
() async {
148-
/// Arrange
149-
when(() => mockLocalDataSource.getLastProducts())
150-
.thenAnswer((_) async => tProductResponseModel);
151-
152-
/// Act
153-
final result =
154-
await repository.getRemoteProducts(const FilterProductParams());
155-
156-
/// Assert
157-
verifyZeroInteractions(mockRemoteDataSource);
158-
verify(() => mockLocalDataSource.getLastProducts());
159-
expect(result, equals(Right(tProductResponseModel)));
160-
},
161-
);
162-
163-
test(
164-
'should return CacheFailure when there is no cached data present',
165-
() async {
166-
/// Arrange
167-
when(() => mockLocalDataSource.getLastProducts())
168-
.thenThrow(CacheException());
169-
170-
/// Act
171-
final result =
172-
await repository.getRemoteProducts(const FilterProductParams());
173-
174-
/// Assert
175-
verifyZeroInteractions(mockRemoteDataSource);
176-
verify(() => mockLocalDataSource.getLastProducts());
177-
expect(result, equals(Left(CacheFailure())));
178-
},
179-
);
180-
});
181143
});
182144
}

test/domain/usecases/cart/clear_cart_usecase_test.dart renamed to test/domain/usecases/cart/delete_cart_usecase_test.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,31 @@ void main() {
2121
'Should get clea item from the repository when Cart Repository clear data successfully',
2222
() async {
2323
/// Arrange
24-
when(() => mockProductRepository.clearCart())
24+
when(() => mockProductRepository.deleteCart())
2525
.thenAnswer((_) async => const Right(true));
2626

2727
/// Act
2828
final result = await usecase(NoParams());
2929

3030
/// Assert
3131
expect(result, const Right(true));
32-
verify(() => mockProductRepository.clearCart());
32+
verify(() => mockProductRepository.deleteCart());
3333
verifyNoMoreInteractions(mockProductRepository);
3434
},
3535
);
3636

3737
test('should return a Failure from the repository', () async {
3838
/// Arrange
3939
final failure = NetworkFailure();
40-
when(() => mockProductRepository.clearCart())
40+
when(() => mockProductRepository.deleteCart())
4141
.thenAnswer((_) async => Left(failure));
4242

4343
/// Act
4444
final result = await usecase(NoParams());
4545

4646
/// Assert
4747
expect(result, Left(failure));
48-
verify(() => mockProductRepository.clearCart());
48+
verify(() => mockProductRepository.deleteCart());
4949
verifyNoMoreInteractions(mockProductRepository);
5050
});
5151
}

test/fixtures/cart/cart_item.json

+32-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
{
22
"_id": "1",
33
"product": {
4-
"_id": "1",
5-
"name": "name",
6-
"description": "description",
7-
"priceTags": [{"_id": "1", "name": "name", "price": 100}],
8-
"categories": [{"_id": "1", "name": "name", "image": "image"}],
9-
"images": ["image"],
10-
"createdAt": "2000-01-01T00:00:00.000",
11-
"updatedAt": "2000-01-01T00:00:00.000"
4+
"_id": "64eb722a41cb9b05eb4420b7",
5+
"name": "Asus Gaming Mouse",
6+
"description": "Text description",
7+
"priceTags": [
8+
{
9+
"_id": "64eb728341cb9b05eb4420ba",
10+
"name": "White",
11+
"price": 50.99
12+
}
13+
],
14+
"categories": [
15+
{
16+
"_id": "64cecb613357eaec7b1ab31b",
17+
"name": "Headphone",
18+
"image": "https://res.cloudinary.com/dhyttttax/image/upload/v1693148015/category/headphone_pdqwo2.jpg"
19+
}
20+
],
21+
"images": [
22+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/vxyyemcdwcuoooyejehj.jpg",
23+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/vqiw6cswpnzhgryd3s1l.jpg",
24+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/tkanjwktt2t0qvybk5xf.jpg",
25+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/yjxkgevogpaim02wonks.jpg",
26+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/m2bb9pzzobynrpyo9ike.jpg",
27+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/xhojjofgfyfpbjwo2vox.jpg"
28+
],
29+
"createdAt": "2023-08-27T15:56:26.504Z",
30+
"updatedAt": "2023-08-27T16:19:16.683Z"
1231
},
13-
"priceTag": {"_id": "1", "name": "name", "price": 100}
32+
"priceTag": {
33+
"_id": "64eb728341cb9b05eb4420ba",
34+
"name": "White",
35+
"price": 50.99
36+
}
1437
}

test/fixtures/cart/cart_item_list.json

+32-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,38 @@
22
{
33
"_id": "1",
44
"product": {
5-
"_id": "1",
6-
"name": "name",
7-
"description": "description",
8-
"priceTags": [{"_id": "1", "name": "name", "price": 100}],
9-
"categories": [{"_id": "1", "name": "name", "image": "image"}],
10-
"images": ["image"],
11-
"createdAt": "2000-01-01T00:00:00.000",
12-
"updatedAt": "2000-01-01T00:00:00.000"
5+
"_id": "64eb722a41cb9b05eb4420b7",
6+
"name": "Asus Gaming Mouse",
7+
"description": "Text description",
8+
"priceTags": [
9+
{
10+
"_id": "64eb728341cb9b05eb4420ba",
11+
"name": "White",
12+
"price": 50.99
13+
}
14+
],
15+
"categories": [
16+
{
17+
"_id": "64cecb613357eaec7b1ab31b",
18+
"name": "Headphone",
19+
"image": "https://res.cloudinary.com/dhyttttax/image/upload/v1693148015/category/headphone_pdqwo2.jpg"
20+
}
21+
],
22+
"images": [
23+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/vxyyemcdwcuoooyejehj.jpg",
24+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/vqiw6cswpnzhgryd3s1l.jpg",
25+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/tkanjwktt2t0qvybk5xf.jpg",
26+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/yjxkgevogpaim02wonks.jpg",
27+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/m2bb9pzzobynrpyo9ike.jpg",
28+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/xhojjofgfyfpbjwo2vox.jpg"
29+
],
30+
"createdAt": "2023-08-27T15:56:26.504Z",
31+
"updatedAt": "2023-08-27T16:19:16.683Z"
1332
},
14-
"priceTag": {"_id": "1", "name": "name", "price": 100}
33+
"priceTag": {
34+
"_id": "64eb728341cb9b05eb4420ba",
35+
"name": "White",
36+
"price": 50.99
37+
}
1538
}
1639
]

test/fixtures/category/category.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"_id":"1",
3-
"name":"name",
4-
"image":"image"
2+
"_id": "64cecb613357eaec7b1ab31b",
3+
"name": "Headphone",
4+
"image": "https://res.cloudinary.com/dhyttttax/image/upload/v1693148015/category/headphone_pdqwo2.jpg"
55
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"data": [
3-
{
4-
"_id":"1",
5-
"name":"name",
6-
"image":"image"
7-
}
3+
{
4+
"_id": "64cecb613357eaec7b1ab31b",
5+
"name": "Headphone",
6+
"image": "https://res.cloudinary.com/dhyttttax/image/upload/v1693148015/category/headphone_pdqwo2.jpg"
7+
}
88
]
99
}
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
3-
"_id":"1",
4-
"name":"name",
5-
"image":"image"
3+
"_id": "64cecb613357eaec7b1ab31b",
4+
"name": "Headphone",
5+
"image": "https://res.cloudinary.com/dhyttttax/image/upload/v1693148015/category/headphone_pdqwo2.jpg"
66
}
77
]

test/fixtures/constant_objects.dart

+37-22
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ import 'package:eshop/domain/usecases/user/sign_up_usecase.dart';
1515

1616
//products
1717
final tProductModel = ProductModel(
18-
id: "1",
19-
name: "name",
20-
description: "description",
21-
priceTags: [PriceTagModel(id: "1", name: "name", price: 100)],
22-
categories: const [CategoryModel(id: "1", name: "name", image: "image")],
23-
images: const ["image"],
24-
createdAt: DateTime(2000),
25-
updatedAt: DateTime(2000),
18+
id: "64eb722a41cb9b05eb4420b7",
19+
name: "Asus Gaming Mouse",
20+
description: "Text description",
21+
priceTags: [tPriceTagModel],
22+
categories: const [tCategoryModel],
23+
images: const [
24+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/vxyyemcdwcuoooyejehj.jpg",
25+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/vqiw6cswpnzhgryd3s1l.jpg",
26+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/tkanjwktt2t0qvybk5xf.jpg",
27+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/yjxkgevogpaim02wonks.jpg",
28+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/m2bb9pzzobynrpyo9ike.jpg",
29+
"https://res.cloudinary.com/dhyttttax/image/upload/v1693151785/product/xhojjofgfyfpbjwo2vox.jpg"
30+
],
31+
createdAt: DateTime.parse("2023-08-27T15:56:26.504Z"),
32+
updatedAt: DateTime.parse("2023-08-27T16:19:16.683Z"),
2633
);
2734

2835
final tProductModelList = [tProductModel, tProductModel];
@@ -45,7 +52,11 @@ final tProductResponseModel = ProductResponseModel(
4552
);
4653

4754
//price tag
48-
final tPriceTagModel = PriceTagModel(id: "1", name: "name", price: 100);
55+
final tPriceTagModel = PriceTagModel(
56+
id: "64eb728341cb9b05eb4420ba",
57+
name: "White",
58+
price: 50.99,
59+
);
4960

5061
//cart
5162
final tCartItemModel = CartItemModel(
@@ -56,21 +67,21 @@ final tCartItemModel = CartItemModel(
5667

5768
//category
5869
const tCategoryModel = CategoryModel(
59-
id: "1",
60-
name: "name",
61-
image: "image",
70+
id: "64cecb613357eaec7b1ab31b",
71+
name: "Headphone",
72+
image: "https://res.cloudinary.com/dhyttttax/image/upload/v1693148015/category/headphone_pdqwo2.jpg",
6273
);
6374

6475
// delivery info
6576
const tDeliveryInfoModel = DeliveryInfoModel(
6677
id: '1',
67-
firstName: 'firstName',
68-
lastName: 'lastName',
69-
addressLineOne: 'addressLineOne',
70-
addressLineTwo: 'addressLineTwo',
71-
city: 'city',
72-
zipCode: 'zipCode',
73-
contactNumber: 'contactNumber',
78+
firstName: 'Jon',
79+
lastName: 'Perera',
80+
addressLineOne: '23/1 Main Road',
81+
addressLineTwo: 'Navinna',
82+
city: 'Mahragama',
83+
zipCode: '10800',
84+
contactNumber: '0779125803',
7485
);
7586

7687
// order details
@@ -83,10 +94,10 @@ final tOrderDetailsModel = OrderDetailsModel(
8394

8495
// order item
8596
final tOrderItemModel = OrderItemModel(
86-
id: '1',
97+
id: '651301997eae44a63472d728',
8798
product: tProductModel,
8899
priceTag: tPriceTagModel,
89-
price: 100,
100+
price: 50.99,
90101
quantity: 1,
91102
);
92103

@@ -103,4 +114,8 @@ const tAuthenticationResponseModel =
103114
AuthenticationResponseModel(token: 'token', user: tUserModel);
104115
//params
105116
const tSignInParams = SignInParams(username: 'username', password: 'password');
106-
const tSignUpParams = SignUpParams(firstName: 'firstName', lastName: 'lastName', email: 'email', password: 'password');
117+
const tSignUpParams = SignUpParams(
118+
firstName: 'firstName',
119+
lastName: 'lastName',
120+
email: 'email',
121+
password: 'password');

0 commit comments

Comments
 (0)