Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

2.7 KB
Raw Blame
using Grand.Business.Catalog.Events.Handlers;
using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Data;
using Grand.Data.Tests.MongoDb;
using Grand.Domain.Catalog;
using Grand.Domain.Customers;
using Grand.Domain.Seo;
using Grand.Infrastructure.Events;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Grand.Business.Catalog.Tests.Events.Handlers;

[TestClass]
public class ProductDeletedEventHandlerTests
{
    private IRepository<CustomerGroupProduct> _customerGroupProductRepository;
    private IRepository<Customer> _customerRepository;
    private IRepository<EntityUrl> _entityUrlRepository;
    private ProductDeletedEventHandler _handler;
    private IRepository<ProductDeleted> _productDeletedRepository;
    private IRepository<ProductReview> _productReviewRepository;
    private IRepository<ProductTag> _productTagRepository;
    private Mock<IProductTagService> _productTagService;
    private IRepository<Product> _repository;

    [TestInitialize]
    public void Init()
    {
        _repository = new MongoDBRepositoryTest<Product>();
        _customerGroupProductRepository = new MongoDBRepositoryTest<CustomerGroupProduct>();
        _customerRepository = new MongoDBRepositoryTest<Customer>();
        _entityUrlRepository = new MongoDBRepositoryTest<EntityUrl>();
        _productTagRepository = new MongoDBRepositoryTest<ProductTag>();
        _productReviewRepository = new MongoDBRepositoryTest<ProductReview>();
        _productDeletedRepository = new MongoDBRepositoryTest<ProductDeleted>();
        _productTagService = new Mock<IProductTagService>();
        _handler = new ProductDeletedEventHandler(_repository, _customerGroupProductRepository,
            _customerRepository, _entityUrlRepository, _productTagRepository, _productReviewRepository,
            _productDeletedRepository);
    }


    [TestMethod]
    public async Task HandleTest()
    {
        //Arrange
        var product = new Product();
        product.RecommendedProduct.Add("1");
        product.RelatedProducts.Add(new RelatedProduct { ProductId2 = "1" });
        product.CrossSellProduct.Add("1");
        product.SimilarProducts.Add(new SimilarProduct { ProductId2 = "1" });
        await _repository.InsertAsync(product);
        //Act
        await _handler.Handle(new EntityDeleted<Product>(new Product { Id = "1" }), CancellationToken.None);

        //Assert
        var result = _repository.Table.FirstOrDefault(x => x.Id == product.Id);
        Assert.IsNotNull(result);
        Assert.IsEmpty(result.RelatedProducts);
        Assert.IsEmpty(result.RecommendedProduct);
        Assert.IsEmpty(result.CrossSellProduct);
        Assert.IsEmpty(result.SimilarProducts);
    }
}