Skip to content

Commit

Permalink
Add test to replace items in StackLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Sep 1, 2023
1 parent ed15f1e commit 42b983f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 86 deletions.
23 changes: 12 additions & 11 deletions samples/ThirdPartyControlsSample/Pages/CommunityToolkitPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
@using CommunityToolkit.Maui.Core
@using ThirdPartyControlsSample.Extensions;

@inject Navigation Navigation;

<ContentPage Title="CommunityToolkit">
<ScrollView>
<VerticalStackLayout Margin="20" Spacing="8">
<Label>Avatar with photo url</Label>
<AvatarView Text="HW" ImageSource="urlSource" />
<AvatarView Text="HW" ImageSource="urlSource"/>

<Label>Avatar without photo</Label>
<AvatarView Text="HW" />
<AvatarView Text="HW"/>

<Label>Drawing view</Label>
<DrawingView IsMultiLineModeEnabled="true"
ShouldClearOnFinish="false"
LineColor="Colors.Black"
HeightRequest="100"
WidthRequest="200" />
WidthRequest="200"/>

<Label>Popup</Label>
<Button OnClick="ShowPopup">Show popup</Button>
Expand All @@ -27,14 +29,13 @@
</ContentPage>

@code {
@inject Navigation Navigation;
ImageSource urlSource = "https://static.wikia.nocookie.net/firefly/images/f/fa/Wash.jpg";

ImageSource urlSource = "https://static.wikia.nocookie.net/firefly/images/f/fa/Wash.jpg";
string popupResult;

string popupResult;
async Task ShowPopup()
{
popupResult = (string)await Navigation.ShowCommunityToolkitPopupAsync<CommunityToolkitPopup>();
}

async Task ShowPopup()
{
popupResult = (string)await Navigation.ShowCommunityToolkitPopupAsync<CommunityToolkitPopup>();
}
}
}
194 changes: 119 additions & 75 deletions src/BlazorBindings.UnitTests/Elements/StackLayoutTests.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
@inherits ElementTestBase

@code {

[Test]
public async Task AddLayoutChildren()
{
var layout = await Render<MC.Layout>(
@<VerticalStackLayout>
<Label>First</Label>
<Button>Second</Button>
@<VerticalStackLayout>
<Label>First</Label>
<Button>Second</Button>

<HorizontalStackLayout>
<Label>Third</Label>
</HorizontalStackLayout>
</VerticalStackLayout>
);
<HorizontalStackLayout>
<Label>Third</Label>
</HorizontalStackLayout>
</VerticalStackLayout>
);

var label = (MC.Label)layout.Children[0];
var button = (MC.Button)layout.Children[1];
Expand All @@ -32,33 +33,33 @@
var insertItems = false;

var layout = await Render<MC.Layout>(
@<VerticalStackLayout>
@if (insertItems)
{
<Label>1</Label>
}
<Label>2</Label>
@if (insertItems)
{
<Label>3</Label>
}
<Label>4</Label>

@if (insertItems)
{
<Label>5</Label>
}
else
{
<Label>6</Label>
}

@if (insertItems)
{
<Label>7</Label>
}
</VerticalStackLayout>
);
@<VerticalStackLayout>
@if (insertItems)
{
<Label>1</Label>
}
<Label>2</Label>
@if (insertItems)
{
<Label>3</Label>
}
<Label>4</Label>

@if (insertItems)
{
<Label>5</Label>
}
else
{
<Label>6</Label>
}

@if (insertItems)
{
<Label>7</Label>
}
</VerticalStackLayout>
);

var items = layout.Children.Cast<MC.Label>().Select(l => l.Text).ToArray();
Assert.That(items, Is.EqualTo(new[] { "2", "4", "6" }));
Expand All @@ -83,45 +84,45 @@
var insertItems = false;

var layout = await Render<MC.Layout>(
@<VerticalStackLayout>
<Label>0</Label>
@if (insertItems)
{
<Label>1</Label>
}

<WrapperWithCascadingValue>
@if (insertItems)
{
<Label>2</Label>
}
<Label>3</Label>
</WrapperWithCascadingValue>

<Label>4</Label>
@if (insertItems)
{
<Label>5</Label>
}

@if (insertItems)
{
<WrapperWithCascadingValue>
<Label>6</Label>
<Label>7</Label>
</WrapperWithCascadingValue>
}
else
{
<Label>8</Label>
}

@if (insertItems)
{
<Label>9</Label>
}
</VerticalStackLayout>
);
@<VerticalStackLayout>
<Label>0</Label>
@if (insertItems)
{
<Label>1</Label>
}

<WrapperWithCascadingValue>
@if (insertItems)
{
<Label>2</Label>
}
<Label>3</Label>
</WrapperWithCascadingValue>

<Label>4</Label>
@if (insertItems)
{
<Label>5</Label>
}

@if (insertItems)
{
<WrapperWithCascadingValue>
<Label>6</Label>
<Label>7</Label>
</WrapperWithCascadingValue>
}
else
{
<Label>8</Label>
}

@if (insertItems)
{
<Label>9</Label>
}
</VerticalStackLayout>
);

var items = layout.Children.Cast<MC.Label>().Select(l => l.Text).ToArray();
Assert.That(items, Is.EqualTo(new[] { "0", "3", "4", "8" }));
Expand All @@ -139,4 +140,47 @@
Assert.That(items, Is.EqualTo(new[] { "0", "3", "4", "8" }));
}

}

[Test]
public async Task ReplaceItems()
{
var replaceItems = true;

var layout = await Render<MC.Layout>(
@<VerticalStackLayout>
@if (replaceItems)
{
<Label>1</Label>

<WrapperWithCascadingValue>
<Label>2</Label>
</WrapperWithCascadingValue>

<Label>3</Label>
}
else
{
<Label>4</Label>
<Label>5</Label>
<Label>6</Label>
}
</VerticalStackLayout>
);

var items = layout.Children.Cast<MC.Label>().Select(l => l.Text).ToArray();
Assert.That(items, Is.EqualTo(new[] { "1", "2", "3" }));

replaceItems = false;
StateHasChanged();

items = layout.Children.Cast<MC.Label>().Select(l => l.Text).ToArray();
Assert.That(items, Is.EqualTo(new[] { "4", "5", "6" }));

replaceItems = true;
StateHasChanged();

items = layout.Children.Cast<MC.Label>().Select(l => l.Text).ToArray();
Assert.That(items, Is.EqualTo(new[] { "1", "2", "3" }));
}

}

0 comments on commit 42b983f

Please sign in to comment.