-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic unit tests for DataGrid component
- Loading branch information
1 parent
f996129
commit 1051643
Showing
12 changed files
with
491 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace Maui.DataGrid.Sample.Tests; | ||
|
||
using System.Collections.ObjectModel; | ||
using Maui.DataGrid.Sample.Tests.TestUtils; | ||
using Xunit; | ||
|
||
public class ColumnsTest | ||
{ | ||
private readonly List<DataGridColumn> _columns = | ||
[ | ||
new() { Title = "Name", PropertyName = "Name" }, | ||
new() { Title = "Won", PropertyName = "Won" }, | ||
new() { Title = "Lost", PropertyName = "Lost" } | ||
]; | ||
|
||
[Fact] | ||
public async void TestColumnsBindingFromViewModel() | ||
{ | ||
var columns = new ObservableCollection<DataGridColumn>(_columns); | ||
|
||
var viewModel = new SingleVM<ObservableCollection<DataGridColumn>>(); | ||
var dataGrid = new DataGrid(); | ||
|
||
dataGrid.SetBinding(DataGrid.ColumnsProperty, new Binding("Item", source: viewModel)); | ||
Assert.Null(dataGrid.Columns); | ||
|
||
var propertyChangedEventTriggered = false; | ||
dataGrid.PropertyChanged += (s, e) => | ||
{ | ||
if (e.PropertyName == DataGrid.ColumnsProperty.PropertyName) | ||
{ | ||
propertyChangedEventTriggered = true; | ||
} | ||
}; | ||
|
||
viewModel.Item = columns; | ||
Assert.Equal(columns, await dataGrid.GetValueSafe(DataGrid.ColumnsProperty)); | ||
Assert.True(propertyChangedEventTriggered); | ||
|
||
columns.RemoveAt(2); | ||
|
||
var newColumns = await dataGrid.GetValueSafe(DataGrid.ColumnsProperty) as ObservableCollection<DataGridColumn>; | ||
Assert.NotNull(newColumns); | ||
Assert.Equal(2, dataGrid.Columns.Count); | ||
Assert.Equal("Name", dataGrid.Columns[0].Title); | ||
Assert.Equal("Won", dataGrid.Columns[1].Title); | ||
} | ||
|
||
|
||
[Fact] | ||
public async void SortOrderBindingOnlyWorksWhenLoaded() | ||
{ | ||
var dataGrid = new DataGrid | ||
{ | ||
Columns = new ObservableCollection<DataGridColumn>(_columns), | ||
}; | ||
|
||
var viewModel = new SingleVM<SortData?>(); | ||
|
||
dataGrid.SetBinding(DataGrid.SortedColumnIndexProperty, new Binding("Item", source: viewModel)); | ||
Assert.Null(dataGrid.SortedColumnIndex); | ||
|
||
var propertyChangedEventTriggered = false; | ||
dataGrid.PropertyChanged += (s, e) => | ||
{ | ||
if (e.PropertyName == DataGrid.SortedColumnIndexProperty.PropertyName) | ||
{ | ||
propertyChangedEventTriggered = true; | ||
} | ||
}; | ||
|
||
viewModel.Item = -1; | ||
if (!dataGrid.IsLoaded) | ||
{ | ||
Assert.Null(await dataGrid.GetValueSafe(DataGrid.SortedColumnIndexProperty)); | ||
Assert.False(propertyChangedEventTriggered); | ||
return; | ||
} | ||
else | ||
{ | ||
Assert.Equal(-1, await dataGrid.GetValueSafe(DataGrid.SortedColumnIndexProperty)); | ||
Assert.True(propertyChangedEventTriggered); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
namespace Maui.DataGrid.Sample.Tests; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using Maui.DataGrid.Sample.Models; | ||
using Maui.DataGrid.Sample.Tests.TestUtils; | ||
using Xunit; | ||
|
||
public class ItemsSourceTest | ||
{ | ||
private readonly List<Team> _teams = Utils.DummyDataProvider.GetTeams(); | ||
private readonly Team _dummyTeam = new() | ||
{ | ||
Name = "Not Exists", | ||
Conf = "", | ||
Div = "", | ||
Home = "", | ||
Last10 = "", | ||
Logo = "", | ||
Road = "", | ||
Streak = new Streak { NumStreak = 3, Result = Result.Lost } | ||
}; | ||
|
||
[Fact] | ||
public void BindsItemSource() | ||
{ | ||
var dataGrid = new DataGrid(); | ||
dataGrid.CheckPropertyBindingWorks(DataGrid.ItemsSourceProperty, _teams, null); | ||
} | ||
|
||
[Fact] | ||
public void BindsSelectedItem() | ||
{ | ||
var datagrid = new DataGrid { ItemsSource = _teams }; | ||
datagrid.CheckPropertyBindingWorks(DataGrid.SelectedItemProperty, _teams.ElementAt(2), _teams.ElementAt(3)); | ||
} | ||
|
||
|
||
[Fact] | ||
public async void SelectNonExistingItemNotPossible() | ||
{ | ||
var viewModel = new SingleVM<Team>(); | ||
var datagrid = new DataGrid { ItemsSource = _teams }; | ||
|
||
datagrid.SetBinding(DataGrid.SelectedItemProperty, new Binding("Item", source: viewModel)); | ||
|
||
viewModel.Item = _teams.First(); | ||
Assert.Equal(_teams.First(), await datagrid.GetValueSafe(DataGrid.SelectedItemProperty)); | ||
|
||
viewModel.Item = _dummyTeam; | ||
Assert.Null(await datagrid.GetValueSafe(DataGrid.SelectedItemProperty)); | ||
} | ||
|
||
[Fact] | ||
public async void RemovingItemInObservableCollectionUpdatesItemsSource() | ||
{ | ||
var viewModel = new SingleVM<ObservableCollection<Team>> { Item = new ObservableCollection<Team>(_teams) }; | ||
var datagrid = new DataGrid(); | ||
datagrid.SetBinding(DataGrid.ItemsSourceProperty, new Binding("Item", source: viewModel)); | ||
|
||
viewModel.Item.RemoveAt(2); | ||
var itemsSource = await datagrid.GetValueSafe(DataGrid.ItemsSourceProperty) as ObservableCollection<Team>; | ||
Assert.NotNull(itemsSource); | ||
Assert.Equal(_teams.Count - 1, itemsSource!.Count); | ||
Assert.DoesNotContain(_teams.ElementAt(2), itemsSource); | ||
} | ||
|
||
[Fact] | ||
public async void AddingItemInObservableCollectionUpdatesItemsSource() | ||
{ | ||
var viewModel = new SingleVM<ObservableCollection<Team>> { Item = new ObservableCollection<Team>(_teams) }; | ||
var datagrid = new DataGrid(); | ||
datagrid.SetBinding(DataGrid.ItemsSourceProperty, new Binding("Item", source: viewModel)); | ||
|
||
viewModel.Item.Add(_dummyTeam); | ||
var itemsSource = await datagrid.GetValueSafe(DataGrid.ItemsSourceProperty) as ObservableCollection<Team>; | ||
Assert.NotNull(itemsSource); | ||
Assert.Equal(_teams.Count + 1, itemsSource!.Count); | ||
Assert.Contains(_dummyTeam, itemsSource); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
namespace Maui.DataGrid.Sample.Tests; | ||
using System.Collections.Generic; | ||
using Maui.DataGrid.Sample.Models; | ||
using Maui.DataGrid.Sample.Tests.TestUtils; | ||
using Xunit; | ||
|
||
public class PaginationTest | ||
{ | ||
private readonly List<Team> _teams = Utils.DummyDataProvider.GetTeams(); | ||
|
||
[Fact] | ||
public void PageCountDoesNotChangesWithBinding() | ||
{ | ||
var dataGrid = new DataGrid { ItemsSource = _teams, PageSize = 10 }; | ||
|
||
Assert.Equal(2, dataGrid.PageCount); | ||
|
||
var countViewModel = new SingleVM<int>(); | ||
dataGrid.SetBinding(DataGrid.PageCountProperty, new Binding("Item", source: countViewModel)); | ||
|
||
countViewModel.Item = 1; | ||
Assert.Equal(2, dataGrid.PageCount); | ||
|
||
} | ||
|
||
[Fact] | ||
public void PageNumberDoesNotExceedsLimit() | ||
{ | ||
var dataGrid = new DataGrid { ItemsSource = _teams, PageSize = 10 }; | ||
|
||
Assert.Equal(1, dataGrid.PageNumber); | ||
|
||
|
||
dataGrid.PageNumber = 2; | ||
Assert.Equal(2, dataGrid.PageNumber); | ||
|
||
dataGrid.PageNumber = 3; | ||
Assert.Equal(2, dataGrid.PageNumber); | ||
|
||
} | ||
|
||
[Fact] | ||
public void PageSizeAllowsMorePageNumber() | ||
{ | ||
var dataGrid = new DataGrid { ItemsSource = _teams, PageSize = 10 }; | ||
|
||
Assert.Equal(1, dataGrid.PageNumber); | ||
|
||
dataGrid.PageSize = 5; | ||
|
||
dataGrid.PageNumber = 3; | ||
Assert.Equal(3, dataGrid.PageNumber); | ||
|
||
dataGrid.PageNumber = 30; | ||
Assert.Equal(3, dataGrid.PageNumber); | ||
} | ||
|
||
[Fact] | ||
public void PageNumberCannotBeNegative() | ||
{ | ||
var dataGrid = new DataGrid { ItemsSource = _teams, PageSize = 10 }; | ||
|
||
Assert.Equal(1, dataGrid.PageNumber); | ||
|
||
dataGrid.PageNumber = -1; | ||
Assert.Equal(1, dataGrid.PageNumber); | ||
} | ||
|
||
[Fact] | ||
public void PageSizeCannotBeNegative() | ||
{ | ||
var dataGrid = new DataGrid { ItemsSource = _teams, PageSize = 10 }; | ||
|
||
Assert.Equal(10, dataGrid.PageSize); | ||
dataGrid.PageSize = -1; | ||
Assert.Equal(10, dataGrid.PageSize); | ||
} | ||
|
||
[Fact] | ||
public void PageNumberResetsWhenPageSizeChanges() | ||
{ | ||
#pragma warning disable IDE0017 // Simplify object initialization | ||
var dataGrid = new DataGrid { ItemsSource = _teams, PageSize = 6 }; | ||
dataGrid.PageNumber = 3; | ||
#pragma warning restore IDE0017 // Simplify object initialization | ||
Assert.Equal(3, dataGrid.PageNumber); | ||
dataGrid.PageSize = 5; | ||
Assert.Equal(1, dataGrid.PageNumber); | ||
|
||
} | ||
|
||
[Fact] | ||
public void PageSizeListUpdatedWithUnknownNumber() | ||
{ | ||
var dataGrid = new DataGrid { ItemsSource = _teams, PageSize = 6 }; | ||
|
||
Assert.DoesNotContain(7, dataGrid.PageSizeList); | ||
dataGrid.PageSize = 7; | ||
Assert.Contains(7, dataGrid.PageSizeList); | ||
|
||
} | ||
} | ||
|
Oops, something went wrong.