210 lines
7.0 KiB
C#
210 lines
7.0 KiB
C#
using Avalonia;
|
||
using Avalonia.Headless;
|
||
using Avalonia.Controls;
|
||
using TovarV2;
|
||
|
||
namespace TestTovarV2
|
||
{
|
||
[TestFixture]
|
||
public class KorzinaTests
|
||
{
|
||
private Korzina _korzina;
|
||
|
||
[OneTimeSetUp]
|
||
public void OneTimeSetup()
|
||
{
|
||
AppBuilder.Configure<App>()
|
||
.UseHeadless(new AvaloniaHeadlessPlatformOptions())
|
||
.SetupWithoutStarting();
|
||
}
|
||
|
||
[SetUp]
|
||
public void Setup()
|
||
{
|
||
ListPr.productSelects.Clear();
|
||
ListPr.productSelects.AddRange(new List<ProductSelect>
|
||
{
|
||
new ProductSelect { Id = 0, nameProdKorz = "Test 1", priceProdKorz = 100, quantityProdKorz = 1, quantitySelect = 1 },
|
||
new ProductSelect { Id = 1, nameProdKorz = "Test 2", priceProdKorz = 200, quantityProdKorz = 2, quantitySelect = 1 },
|
||
new ProductSelect { Id = 2, nameProdKorz = "Test 3", priceProdKorz = 300, quantityProdKorz = 3, quantitySelect = 1 }
|
||
});
|
||
|
||
_korzina = new Korzina();
|
||
_korzina.Show();
|
||
}
|
||
|
||
[TearDown]
|
||
public void TearDown()
|
||
{
|
||
_korzina.Close();
|
||
ListPr.productSelects.Clear();
|
||
}
|
||
|
||
[Test]
|
||
public void DelBtnOnClick_ShouldRemoveProduct_WhenFirstProductIsSelected()
|
||
{
|
||
int initialCount = ListPr.productSelects.Count;
|
||
|
||
var delButton = new Button { Tag = 0 };
|
||
_korzina.DelBtn_Click(delButton, null);
|
||
|
||
Assert.That(initialCount - 1 == ListPr.productSelects.Count);
|
||
}
|
||
|
||
[Test]
|
||
public void DelBtnOnClick_ShouldRemoveProduct_WhenSecondProductIsSelected()
|
||
{
|
||
int initialCount = ListPr.productSelects.Count;
|
||
|
||
var delButton = new Button { Tag = 1 };
|
||
_korzina.DelBtn_Click(delButton, null);
|
||
|
||
Assert.That(initialCount - 1 == ListPr.productSelects.Count);
|
||
}
|
||
|
||
[Test]
|
||
public void CheckQuantity_ShouldCapAtMax_WhenQuantityExceedsStock()
|
||
{
|
||
var result = _korzina.CheckQuantity(10, 5);
|
||
|
||
Assert.That(5 == result);
|
||
}
|
||
|
||
[Test]
|
||
public void CheckQuantity_ShouldRaiseToMin_WhenQuantityBelowOne()
|
||
{
|
||
var result = _korzina.CheckQuantity(0, 5);
|
||
|
||
Assert.That(1 == result);
|
||
}
|
||
|
||
[Test]
|
||
public void CheckQuantity_ShouldReturnSame_WhenWithinRange()
|
||
{
|
||
var result = _korzina.CheckQuantity(3, 5);
|
||
|
||
Assert.That(3 == result);
|
||
}
|
||
|
||
[Test]
|
||
public void NextPageOnClick_ShouldReturnLastTwoProducts_WhenThreeProductsInList()
|
||
{
|
||
_korzina.PagesConfig();
|
||
_korzina.NextPage_OnClick(null, null);
|
||
|
||
Assert.That("Test 3" == _korzina.currentProdSel[0].nameProdKorz);
|
||
}
|
||
|
||
[Test]
|
||
public void PrevPageOnClick_ShouldReturnLastTwoProducts_WhenThreeProductsInList()
|
||
{
|
||
_korzina.PagesConfig();
|
||
_korzina.NextPage_OnClick(null, null);
|
||
_korzina.PrevPage_OnClick(null, null);
|
||
|
||
Assert.That("Test 1" == _korzina.currentProdSel[0].nameProdKorz);
|
||
}
|
||
|
||
[Test]
|
||
public void ReturnProdEditClick_ShouldOpenProductEditWindow_WhenItPressed()
|
||
{
|
||
_korzina.ReturnProdEdit_Click(null, null);
|
||
Assert.That(ProductEdit.IsOpen);
|
||
}
|
||
|
||
[Test]
|
||
public void PodschetOrderBtnClick_ShouldCalculateTotalPriceCorrectly_WhenDataIsCorrect()
|
||
{
|
||
var korzina = new Korzina();
|
||
korzina.GetPodschetstoimosti = new TextBlock();
|
||
ListPr.productSelects = new List<ProductSelect>()
|
||
{
|
||
new ProductSelect { priceProdKorz = 100, quantitySelect = 2 },
|
||
new ProductSelect { priceProdKorz = 150, quantitySelect = 1 }
|
||
};
|
||
|
||
korzina.PodschetOrderBtn_Click(null, null);
|
||
|
||
Assert.That("Общая стоимость составляет: 350 руб." == korzina.GetPodschetstoimosti.Text);
|
||
}
|
||
|
||
[Test]
|
||
public void PodschetOrderBtnClick_ShouldCalculateTotalPriceCorrectly_WhenDataIsNotCorrect()
|
||
{
|
||
var korzina = new Korzina();
|
||
korzina.GetPodschetstoimosti = new TextBlock();
|
||
ListPr.productSelects = new List<ProductSelect>()
|
||
{
|
||
new ProductSelect { priceProdKorz = 100, quantitySelect = 0 },
|
||
new ProductSelect { priceProdKorz = -150, quantitySelect = 1 }
|
||
};
|
||
|
||
korzina.PodschetOrderBtn_Click(null, null);
|
||
|
||
Assert.That("Общая стоимость составляет: -150 руб." == korzina.GetPodschetstoimosti.Text);
|
||
}
|
||
|
||
[Test]
|
||
public void ExitClick_ShouldOpenMainWindow_WhenItPressed()
|
||
{
|
||
_korzina.Exit_Click(null, null);
|
||
Assert.That(MainWindow.IsOpen);
|
||
}
|
||
|
||
[Test]
|
||
public void UvelBtnOnClick_ShouldIncreaseQuantity_WhenItPressedOnce()
|
||
{
|
||
var testProduct = ListPr.productSelects[0];
|
||
testProduct.quantitySelect = 2;
|
||
testProduct.quantityProdKorz = 10;
|
||
|
||
var button = new Button { Tag = testProduct.Id };
|
||
|
||
_korzina.UvelBtn_OnClick(button, null);
|
||
|
||
Assert.That(testProduct.quantitySelect, Is.EqualTo(3));
|
||
}
|
||
|
||
[Test]
|
||
public void UvelBtnOnClick_ShouldNotIncreaseAboveMaxQuantity_WhenItPressedOnce()
|
||
{
|
||
var testProduct = ListPr.productSelects[0];
|
||
testProduct.quantitySelect = 10;
|
||
testProduct.quantityProdKorz = 10;
|
||
|
||
var button = new Button { Tag = testProduct.Id };
|
||
|
||
_korzina.UvelBtn_OnClick(button, null);
|
||
|
||
Assert.That(testProduct.quantitySelect, Is.EqualTo(10));
|
||
Assert.That(_korzina.GetPodschetstoimosti.Text, Does.Contain("Ошибка"));
|
||
}
|
||
|
||
[Test]
|
||
public void UmenBtnOnClick_ShouldDecreaseQuantity_WhenItPressedOnce()
|
||
{
|
||
var testProduct = ListPr.productSelects[0];
|
||
testProduct.quantitySelect = 2;
|
||
|
||
var button = new Button { Tag = testProduct.Id };
|
||
|
||
_korzina.UmenBtn_OnClick(button, null);
|
||
|
||
Assert.That(testProduct.quantitySelect, Is.EqualTo(1));
|
||
}
|
||
|
||
[Test]
|
||
public void UmenBtnOnClick_ShouldNotDecreaseBelowOne_WhenItPressedOnce()
|
||
{
|
||
var testProduct = ListPr.productSelects[0];
|
||
testProduct.quantitySelect = 1;
|
||
|
||
var button = new Button { Tag = testProduct.Id };
|
||
|
||
_korzina.UmenBtn_OnClick(button, null);
|
||
|
||
Assert.That(testProduct.quantitySelect, Is.EqualTo(1));
|
||
}
|
||
}
|
||
}
|