208 lines
7.1 KiB
C#
208 lines
7.1 KiB
C#
using Avalonia;
|
||
using Avalonia.Headless;
|
||
using Avalonia.Controls;
|
||
using TovarV2;
|
||
|
||
namespace TestTovarV2
|
||
{
|
||
[TestFixture]
|
||
public class KorzinaTests
|
||
{
|
||
private Korzina _korzinaWindow;
|
||
|
||
[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 = "TestProduct1", priceProdKorz = 10, quantityProdKorz = 1, quantitySelect = 1 },
|
||
new ProductSelect { Id = 1, nameProdKorz = "TestProduct2", priceProdKorz = 20, quantityProdKorz = 2, quantitySelect = 1 },
|
||
new ProductSelect { Id = 2, nameProdKorz = "TestProduct3", priceProdKorz = 30, quantityProdKorz = 3, quantitySelect = 1 }
|
||
});
|
||
|
||
_korzinaWindow = new Korzina();
|
||
_korzinaWindow.Show();
|
||
}
|
||
|
||
[TearDown]
|
||
public void TearDown()
|
||
{
|
||
_korzinaWindow.Close();
|
||
ListPr.productSelects.Clear();
|
||
}
|
||
|
||
[Test]
|
||
public void IsOpenWindow_ShouldReturnTrue_WhenItOpened()
|
||
{
|
||
Assert.IsTrue(Korzina.IsOpen);
|
||
_korzinaWindow.Close();
|
||
}
|
||
|
||
[Test]
|
||
public void IsOpenWindow_ShouldReturnFalse_WhenItClosed()
|
||
{
|
||
Assert.IsTrue(Korzina.IsOpen);
|
||
_korzinaWindow.Close();
|
||
Assert.IsFalse(Korzina.IsOpen);
|
||
}
|
||
|
||
[Test]
|
||
public void NextPageOnClick_ShouldReturnLastTwoProducts_WhenThreeProductsInList()
|
||
{
|
||
_korzinaWindow.PagesConfig();
|
||
_korzinaWindow.NextPage_OnClick(null, null);
|
||
|
||
Assert.AreEqual("TestProduct3", _korzinaWindow.currentProdSel[0].nameProdKorz);
|
||
}
|
||
[Test]
|
||
public void PrevPageOnClick_ShouldReturnLastTwoProducts_WhenThreeProductsInList()
|
||
{
|
||
_korzinaWindow.PagesConfig();
|
||
_korzinaWindow.NextPage_OnClick(null, null);
|
||
_korzinaWindow.PrevPage_OnClick(null, null);
|
||
|
||
Assert.AreEqual("TestProduct1", _korzinaWindow.currentProdSel[0].nameProdKorz);
|
||
}
|
||
|
||
[Test]
|
||
public void DelBtnOnClick_ShouldRemoveProduct_WhenFirstProductIsSelected()
|
||
{
|
||
int initialCount = ListPr.productSelects.Count;
|
||
|
||
var delButton = new Button { Tag = 0 };
|
||
_korzinaWindow.DelBtn_Click(delButton, null);
|
||
|
||
Assert.AreEqual(initialCount - 1, ListPr.productSelects.Count);
|
||
}
|
||
|
||
[Test]
|
||
public void DelBtnOnClick_ShouldRemoveProduct_WhenSecondProductIsSelected()
|
||
{
|
||
int initialCount = ListPr.productSelects.Count;
|
||
|
||
var delButton = new Button { Tag = 1 };
|
||
_korzinaWindow.DelBtn_Click(delButton, null);
|
||
|
||
Assert.AreEqual(initialCount - 1, ListPr.productSelects.Count);
|
||
}
|
||
|
||
[Test]
|
||
public void DelBtnOnClick_ShouldThrowArgumentOutOfRangeException_WhenFourthProductIsSelected()
|
||
{
|
||
var delButton = new Button { Tag = 3 };
|
||
|
||
var ex = Assert.Throws<ArgumentOutOfRangeException>(() =>
|
||
{
|
||
_korzinaWindow.DelBtn_Click(delButton, null);
|
||
});
|
||
|
||
Assert.That(ex.Message, Does.Contain("Index was out of range"));
|
||
}
|
||
|
||
[Test]
|
||
public void ReturnProdEditClick_ShouldOpenProductEditWindow_WhenItPressed()
|
||
{
|
||
_korzinaWindow.ReturnProdEdit_Click(null, null);
|
||
Assert.IsTrue(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.AreEqual("Общая стоимость составляет: 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.AreEqual("Общая стоимость составляет: -150 руб.", korzina.GetPodschetstoimosti.Text);
|
||
}
|
||
|
||
[Test]
|
||
public void ExitClick_ShouldOpenMainWindow_WhenItPressed()
|
||
{
|
||
_korzinaWindow.Exit_Click(null, null);
|
||
Assert.IsTrue(MainWindow.IsOpen);
|
||
}
|
||
|
||
|
||
//багуля
|
||
[Test]
|
||
public void UvelBtnOnClick_ShouldIncreaseQuantity_WhenItPressedOnce()
|
||
{
|
||
var button = new Button { Tag = 0 };
|
||
var initialQuantity = ListPr.productSelects[0].quantitySelect;
|
||
|
||
_korzinaWindow.UvelBtn_OnClick(button, null);
|
||
|
||
// Assert.AreEqual(initialQuantity + 1, ListPr.productSelects[0].quantitySelect);
|
||
Assert.AreEqual(initialQuantity, ListPr.productSelects[0].quantitySelect);
|
||
}
|
||
|
||
//тоже
|
||
[Test]
|
||
public void UmenBtnOnClick_ShouldDecreaseQuantity_WhenItPressedOnce()
|
||
{
|
||
var button = new Button { Tag = 0 };
|
||
var initialQuantity = ListPr.productSelects[0].quantitySelect;
|
||
|
||
_korzinaWindow.UmenBtn_OnClick(button, null);
|
||
|
||
// Assert.AreEqual(initialQuantity + 1, ListPr.productSelects[0].quantitySelect);
|
||
Assert.AreEqual(initialQuantity, ListPr.productSelects[0].quantitySelect);
|
||
}
|
||
|
||
[Test]
|
||
public void CheckQuantity_ShouldCapAtMax_WhenQuantityExceedsStock()
|
||
{
|
||
var result = _korzinaWindow.CheckQuantity(10, 5);
|
||
Assert.AreEqual(5, result);
|
||
}
|
||
|
||
[Test]
|
||
public void CheckQuantity_ShouldRaiseToMin_WhenQuantityBelowOne()
|
||
{
|
||
var result = _korzinaWindow.CheckQuantity(0, 5);
|
||
Assert.AreEqual(1, result);
|
||
}
|
||
|
||
[Test]
|
||
public void CheckQuantity_ShouldReturnSame_WhenWithinRange()
|
||
{
|
||
var result = _korzinaWindow.CheckQuantity(3, 5);
|
||
Assert.AreEqual(3, result);
|
||
}
|
||
}
|
||
}
|