elfisa-pharmacy/tests/Elfisa.Tests/CartServiceTests.cs
Exest 3d89fac6e0
Some checks failed
Desktop tests / test (push) Has been cancelled
тесты: автотесты десктопа (xUnit) + CI
- проект tests/Elfisa.Tests: 56 юнит-тестов на Elfisa.Core + Elfisa.Avalonia:
  версии апдейтера, срок JWT-сессии, ApiClient (URL/ошибки/ретраи/парсинг),
  DTO-контракты, черновики, корзина, дрилдаун прайса (МГ/базовое имя)
- тест-швы: internal-методы через InternalsVisibleTo, ApiClient(HttpMessageHandler)
- CI .github/workflows/desktop-tests.yml — dotnet test при пуше (GitHub/Gitea Actions)
- README с описанием покрытия

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-17 11:51:50 +03:00

70 lines
1.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Elfisa.Avalonia.ViewModels;
using Elfisa.Core;
using Xunit;
namespace Elfisa.Tests;
// Все тесты корзины в одном классе → xUnit гонит их последовательно (синглтон CartService).
public class CartServiceTests
{
private static PriceItem Item(string id, decimal price, string name = "X")
=> new() { SupplierPriceId = id, Price = price, DrugName = name, SupplierName = "S" };
[Fact]
public void Add_items_and_total()
{
var cart = CartService.Instance;
cart.Clear();
cart.Add(Item("1", 10m), 2);
cart.Add(Item("2", 5m), 3);
Assert.Equal(2, cart.Count);
Assert.Equal(35m, cart.Total); // 20 + 15
cart.Clear();
}
[Fact]
public void Add_same_id_merges_quantity()
{
var cart = CartService.Instance;
cart.Clear();
cart.Add(Item("1", 10m), 2);
cart.Add(Item("1", 10m), 3); // тот же supplier_price_id → qty += 3
Assert.Equal(1, cart.Count);
Assert.Equal(50m, cart.Total); // qty 5 * 10
cart.Clear();
}
[Fact]
public void Add_ignores_empty_id()
{
var cart = CartService.Instance;
cart.Clear();
cart.Add(Item("", 10m), 1);
Assert.Equal(0, cart.Count);
}
[Fact]
public void Remove_and_clear()
{
var cart = CartService.Instance;
cart.Clear();
cart.Add(Item("1", 10m), 1);
cart.Remove(cart.Items[0]);
Assert.Equal(0, cart.Count);
}
[Fact]
public void Changed_event_fires_on_add()
{
var cart = CartService.Instance;
cart.Clear();
int fired = 0;
Action handler = () => fired++;
cart.Changed += handler;
cart.Add(Item("1", 10m), 1);
cart.Changed -= handler;
cart.Clear();
Assert.True(fired >= 1);
}
}