Skip to content

Commit

Permalink
Implement basic unit tests for DataGrid component
Browse files Browse the repository at this point in the history
  • Loading branch information
akgulebubekir committed Feb 15, 2024
1 parent f996129 commit 1051643
Show file tree
Hide file tree
Showing 12 changed files with 491 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Maui.DataGrid.Sample.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Test|Any CPU = Test|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1A19A0CA-B02B-4DDB-B8F6-39FAAC6263E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand All @@ -28,10 +29,15 @@ Global
{1A19A0CA-B02B-4DDB-B8F6-39FAAC6263E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A19A0CA-B02B-4DDB-B8F6-39FAAC6263E0}.Release|Any CPU.Build.0 = Release|Any CPU
{1A19A0CA-B02B-4DDB-B8F6-39FAAC6263E0}.Release|Any CPU.Deploy.0 = Release|Any CPU
{1A19A0CA-B02B-4DDB-B8F6-39FAAC6263E0}.Test|Any CPU.ActiveCfg = Test|Any CPU
{1A19A0CA-B02B-4DDB-B8F6-39FAAC6263E0}.Test|Any CPU.Build.0 = Test|Any CPU
{1A19A0CA-B02B-4DDB-B8F6-39FAAC6263E0}.Test|Any CPU.Deploy.0 = Test|Any CPU
{3CAFEEB9-BB8E-40FE-B85B-F0ACD6A66429}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3CAFEEB9-BB8E-40FE-B85B-F0ACD6A66429}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3CAFEEB9-BB8E-40FE-B85B-F0ACD6A66429}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3CAFEEB9-BB8E-40FE-B85B-F0ACD6A66429}.Release|Any CPU.Build.0 = Release|Any CPU
{3CAFEEB9-BB8E-40FE-B85B-F0ACD6A66429}.Test|Any CPU.ActiveCfg = Test|Any CPU
{3CAFEEB9-BB8E-40FE-B85B-F0ACD6A66429}.Test|Any CPU.Build.0 = Test|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 3 additions & 1 deletion Maui.DataGrid.Sample/Maui.DataGrid.Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
Expand Down Expand Up @@ -32,6 +32,7 @@
<Title>Maui DataGrid Example</Title>
<AssemblyName></AssemblyName>
<Authors>Ebubekir Akgul</Authors>
<Configurations>Debug;Release;Test</Configurations>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -87,6 +88,7 @@
<PackageReference Include="CommunityToolkit.Maui" Version="7.0.1" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Shiny.Xunit.Runners.Maui" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 19 additions & 1 deletion Maui.DataGrid.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
namespace Maui.DataGrid.Sample;

#if TEST
using Xunit.Runners.Maui;
#else
using CommunityToolkit.Maui;
#endif

public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
#if TEST
builder.ConfigureTests(new TestOptions
{
Assemblies =
{
typeof(MauiProgram).Assembly
}
})
.UseVisualRunner();
#else


_ = builder
.UseMauiApp<App>()
#if DEBUG
Expand All @@ -19,11 +35,13 @@ public static MauiApp CreateMauiApp()
options.SetShouldSuppressExceptionsInConverters(true);
})
#endif
.ConfigureFonts(fonts =>
.ConfigureFonts(fonts =>
{
_ = fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
_ = fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#endif


return builder.Build();
}
Expand Down
85 changes: 85 additions & 0 deletions Maui.DataGrid.Sample/Tests/ColumnsTest.cs
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);
}
}
}
80 changes: 80 additions & 0 deletions Maui.DataGrid.Sample/Tests/ItemsSourceTest.cs
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);
}
}
103 changes: 103 additions & 0 deletions Maui.DataGrid.Sample/Tests/PaginationTest.cs
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);

}
}

Loading

0 comments on commit 1051643

Please sign in to comment.