1+ using MockQueryable ;
2+ using Moq ;
3+ using ShoeStore . Application . Interface . CartItemInterface ;
4+ using ShoeStore . Application . Interface . Common ;
5+ using ShoeStore . Application . Interface . ProductInterface ;
6+ using ShoeStore . Application . Interface . UserInterface ;
7+ using ShoeStore . Application . Services ;
8+ using ShoeStore . Domain . Entities ;
9+
10+ namespace ShoeStore . Tests . Unit . Services . CartItemServiceTests ;
11+
12+ public class GetCartItemTests
13+ {
14+ private readonly Mock < ICartItemRepository > _cartItemRepository = new ( ) ;
15+ private readonly CartItemService _cartItemService ;
16+ private readonly Mock < IUnitOfWork > _mockUow = new ( ) ;
17+ private readonly Mock < IUserRepository > _userRepository = new ( ) ;
18+ private readonly Mock < IProductVariantRepository > _variantRepository = new ( ) ;
19+
20+ public GetCartItemTests ( )
21+ {
22+ _cartItemService = new CartItemService ( _cartItemRepository . Object , _mockUow . Object , _variantRepository . Object ,
23+ _userRepository . Object ) ;
24+ }
25+
26+ [ Fact ]
27+ public async Task GetCartItemsByUserIdAsync_WhenUserNotFound_ReturnsNotFound ( )
28+ {
29+ // Arrange
30+ var userPublicId = Guid . NewGuid ( ) ;
31+ _userRepository . Setup ( x => x . CheckUserExistsAsync ( userPublicId , It . IsAny < CancellationToken > ( ) ) )
32+ . ReturnsAsync ( false ) ;
33+
34+ // Act
35+ var result = await _cartItemService . GetCartItemsByUserIdAsync ( userPublicId , CancellationToken . None ) ;
36+
37+ // Assert
38+ Assert . True ( result . IsError ) ;
39+ Assert . Equal ( "User.NotFound" , result . FirstError . Code ) ;
40+ _cartItemRepository . Verify ( x => x . GetCartItemsByUserId ( It . IsAny < Guid > ( ) ) , Times . Never ) ;
41+ }
42+
43+ [ Fact ]
44+ public async Task GetCartItemsByUserIdAsync_WhenCartItemsExist_ReturnsMappedResponse ( )
45+ {
46+ // Arrange
47+ var userPublicId = Guid . NewGuid ( ) ;
48+ var cartItemPublicId = Guid . NewGuid ( ) ;
49+ var variantPublicId = Guid . NewGuid ( ) ;
50+
51+ var fakeCartItems = new List < CartItem >
52+ {
53+ new ( )
54+ {
55+ PublicId = cartItemPublicId ,
56+ UserId = 1 ,
57+ Quantity = 2 ,
58+ ProductVariantId = 1 ,
59+ ProductVariant = CreateProductVariant ( variantPublicId )
60+ }
61+ } . BuildMock ( ) . AsQueryable ( ) ;
62+
63+ _userRepository . Setup ( x => x . CheckUserExistsAsync ( userPublicId , It . IsAny < CancellationToken > ( ) ) )
64+ . ReturnsAsync ( true ) ;
65+ _cartItemRepository . Setup ( x => x . GetCartItemsByUserId ( userPublicId ) )
66+ . Returns ( fakeCartItems ) ;
67+
68+ // Act
69+ var result = await _cartItemService . GetCartItemsByUserIdAsync ( userPublicId , CancellationToken . None ) ;
70+
71+ // Assert
72+ Assert . False ( result . IsError ) ;
73+ var cartItems = result . Value ;
74+ Assert . Single ( cartItems ) ;
75+
76+ var cartItem = cartItems [ 0 ] ;
77+ Assert . Equal ( cartItemPublicId , cartItem . CartItemId ) ;
78+ Assert . Equal ( 2 , cartItem . Quantity ) ;
79+ Assert . Equal ( variantPublicId , cartItem . ProductVariantId ) ;
80+ Assert . Equal ( 1 , cartItem . ColorId ) ;
81+ Assert . Equal ( "Black" , cartItem . ColorName ) ;
82+ Assert . Equal ( 42 , cartItem . Size ) ;
83+ Assert . Equal ( 100 , cartItem . Price ) ;
84+ Assert . Equal ( "Jordan 1" , cartItem . ProductName ) ;
85+ Assert . Equal ( "Nike" , cartItem . Brand ) ;
86+ Assert . Equal ( "https://image.test/shoe.jpg" , cartItem . ImageUrl ) ;
87+ Assert . Equal ( 5 , cartItem . Stock ) ;
88+ Assert . True ( cartItem . IsSelling ) ;
89+ }
90+
91+ private static ProductVariant CreateProductVariant ( Guid publicId )
92+ {
93+ return new ProductVariant
94+ {
95+ Id = 1 ,
96+ PublicId = publicId ,
97+ SizeId = 1 ,
98+ ProductId = 1 ,
99+ ColorId = 1 ,
100+ Stock = 5 ,
101+ IsSelling = true ,
102+ ImageUrl = "https://image.test/shoe.jpg" ,
103+ Price = 100 ,
104+ Product = new Product
105+ {
106+ Id = 1 ,
107+ ProductName = "Jordan 1" ,
108+ Brand = "Nike"
109+ } ,
110+ Color = new Color
111+ {
112+ Id = 1 ,
113+ ColorName = "Black"
114+ } ,
115+ Size = new ProductSize
116+ {
117+ Id = 1 ,
118+ Size = 42
119+ }
120+ } ;
121+ }
122+ }
0 commit comments