Skip to content

Commit c11e27d

Browse files
committed
refactor: Remove IRefreshableCollection and behavior
1 parent 5bda93c commit c11e27d

File tree

8 files changed

+54
-172
lines changed

8 files changed

+54
-172
lines changed

MIGRATION.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,26 @@ The `TestServiceProvider` class has been renamed to `BunitTestServiceProvider`.
8282

8383
## `DisposeComponents` is now asynchronous and called `DisposeComponentsAsync`
8484
`DisposeComponentsAsync` allows to await `DisposeAsync` of components under test. If you used `DisposeComponents`, you should replace it with `DisposeComponentsAsync`.
85+
86+
## `IRefreshableElementCollection` was removed
87+
88+
The `IRefreshableElementCollection` interface has been removed. With this the `FindAll` method does not accept a `bool enableRefresh` parameter anymore. Code like this:
89+
90+
```csharp
91+
var items = cut.FindAll("li", enableRefresh: true);
92+
93+
cut.Find("button").Click(); // Some action that causes items to change
94+
95+
Assert.Equal(3, items.Count);
96+
```
97+
98+
Should be changed to this:
99+
100+
```csharp
101+
var items = cut.FindAll("li");
102+
103+
cut.Find("button").Click(); // Some action that causes items to change
104+
105+
items = cut.FindAll("li"); // Re-query the items
106+
Assert.Equal(3, items.Count);
107+
```

src/bunit/Extensions/IRefreshableElementCollection.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/bunit/Extensions/Internal/RefreshableElementCollection.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/bunit/Extensions/RenderedComponentExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public static IElement Find<TComponent>(this IRenderedComponent<TComponent> rend
3636
/// </summary>
3737
/// <param name="renderedComponent">The rendered fragment to search.</param>
3838
/// <param name="cssSelector">The group of selectors to use.</param>
39-
/// <param name="enableAutoRefresh">If true, the returned <see cref="IRefreshableElementCollection{IElement}"/> will automatically refresh its <see cref="IElement"/>s whenever the <paramref name="renderedComponent"/> changes.</param>
40-
/// <returns>An <see cref="IRefreshableElementCollection{IElement}"/>, that can be refreshed to execute the search again.</returns>
41-
public static IRefreshableElementCollection<IElement> FindAll<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, bool enableAutoRefresh = false)
39+
/// <returns>An <see cref="IReadOnlyList{IElement}"/>, that can be refreshed to execute the search again.</returns>
40+
public static IReadOnlyList<IElement> FindAll<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector)
4241
where TComponent : IComponent
4342
{
4443
ArgumentNullException.ThrowIfNull(renderedComponent);
45-
return new RefreshableElementCollection((IRenderedComponent<IComponent>)renderedComponent, cssSelector) { EnableAutoRefresh = enableAutoRefresh };
44+
45+
return renderedComponent.Nodes.QuerySelectorAll(cssSelector).ToArray();
4646
}
4747

4848
/// <summary>

src/bunit/Extensions/WaitForHelpers/RenderedComponentWaitForHelperExtensions.WaitForElement.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public static IElement WaitForElement<TComponent>(this IRenderedComponent<TCompo
4040
/// <param name="renderedComponent">The render fragment or component find the matching element in.</param>
4141
/// <param name="cssSelector">The CSS selector to use to search for elements.</param>
4242
/// <exception cref="WaitForFailedException">Thrown if no elements is found matching the <paramref name="cssSelector"/> within the default timeout.</exception>
43-
/// <returns>The <see cref="IRefreshableElementCollection{IElement}"/>.</returns>
44-
public static IRefreshableElementCollection<IElement> WaitForElements<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector)
43+
/// <returns>The <see cref="IReadOnlyList{IElement}"/>.</returns>
44+
public static IReadOnlyList<IElement> WaitForElements<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector)
4545
where TComponent : IComponent => WaitForElementsCore(renderedComponent, cssSelector, matchElementCount: null, timeout: null);
4646

4747
/// <summary>
@@ -52,8 +52,8 @@ public static IRefreshableElementCollection<IElement> WaitForElements<TComponent
5252
/// <param name="cssSelector">The CSS selector to use to search for elements.</param>
5353
/// <param name="matchElementCount">The exact number of elements to that the <paramref name="cssSelector"/> should match.</param>
5454
/// <exception cref="WaitForFailedException">Thrown if no elements is found matching the <paramref name="cssSelector"/> within the default timeout.</exception>
55-
/// <returns>The <see cref="IRefreshableElementCollection{IElement}"/>.</returns>
56-
public static IRefreshableElementCollection<IElement> WaitForElements<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, int matchElementCount)
55+
/// <returns>The <see cref="IReadOnlyList{IElement}"/>.</returns>
56+
public static IReadOnlyList<IElement> WaitForElements<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, int matchElementCount)
5757
where TComponent : IComponent => WaitForElementsCore(renderedComponent, cssSelector, matchElementCount: matchElementCount, timeout: null);
5858

5959
/// <summary>
@@ -64,8 +64,8 @@ public static IRefreshableElementCollection<IElement> WaitForElements<TComponent
6464
/// <param name="cssSelector">The CSS selector to use to search for elements.</param>
6565
/// <param name="timeout">The maximum time to wait for elements to appear.</param>
6666
/// <exception cref="WaitForFailedException">Thrown if no elements is found matching the <paramref name="cssSelector"/> within the default timeout.</exception>
67-
/// <returns>The <see cref="IRefreshableElementCollection{IElement}"/>.</returns>
68-
public static IRefreshableElementCollection<IElement> WaitForElements<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, TimeSpan timeout)
67+
/// <returns>The <see cref="IReadOnlyList{IElement}"/>.</returns>
68+
public static IReadOnlyList<IElement> WaitForElements<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, TimeSpan timeout)
6969
where TComponent : IComponent
7070
=> WaitForElementsCore(renderedComponent, cssSelector, matchElementCount: null, timeout: timeout);
7171

@@ -78,8 +78,8 @@ public static IRefreshableElementCollection<IElement> WaitForElements<TComponent
7878
/// <param name="matchElementCount">The exact number of elements to that the <paramref name="cssSelector"/> should match.</param>
7979
/// <param name="timeout">The maximum time to wait for elements to appear.</param>
8080
/// <exception cref="WaitForFailedException">Thrown if no elements is found matching the <paramref name="cssSelector"/> within the default timeout.</exception>
81-
/// <returns>The <see cref="IRefreshableElementCollection{IElement}"/>.</returns>
82-
public static IRefreshableElementCollection<IElement> WaitForElements<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, int matchElementCount, TimeSpan timeout)
81+
/// <returns>The <see cref="IReadOnlyList{IElement}"/>.</returns>
82+
public static IReadOnlyList<IElement> WaitForElements<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, int matchElementCount, TimeSpan timeout)
8383
where TComponent : IComponent
8484
=> WaitForElementsCore(renderedComponent, cssSelector, matchElementCount: matchElementCount, timeout: timeout);
8585

@@ -115,8 +115,8 @@ internal static Task<IElement> WaitForElementAsync<TComponent>(this IRenderedCom
115115
/// <param name="cssSelector">The CSS selector to use to search for elements.</param>
116116
/// <param name="matchElementCount">The exact number of elements to that the <paramref name="cssSelector"/> should match.</param>
117117
/// <exception cref="WaitForFailedException">Thrown if no elements is found matching the <paramref name="cssSelector"/> within the default timeout.</exception>
118-
/// <returns>The <see cref="IRefreshableElementCollection{IElement}"/>.</returns>
119-
internal static Task<IRefreshableElementCollection<IElement>> WaitForElementsAsync<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, int matchElementCount)
118+
/// <returns>The <see cref="IReadOnlyList{IElement}"/>.</returns>
119+
internal static Task<IReadOnlyList<IElement>> WaitForElementsAsync<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, int matchElementCount)
120120
where TComponent : IComponent
121121
=> WaitForElementsCoreAsync(renderedComponent, cssSelector, matchElementCount: matchElementCount, timeout: null);
122122

@@ -128,8 +128,8 @@ internal static Task<IRefreshableElementCollection<IElement>> WaitForElementsAsy
128128
/// <param name="cssSelector">The CSS selector to use to search for elements.</param>
129129
/// <param name="timeout">The maximum time to wait for elements to appear.</param>
130130
/// <exception cref="WaitForFailedException">Thrown if no elements is found matching the <paramref name="cssSelector"/> within the default timeout.</exception>
131-
/// <returns>The <see cref="IRefreshableElementCollection{IElement}"/>.</returns>
132-
internal static Task<IRefreshableElementCollection<IElement>> WaitForElementsAsync<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, TimeSpan timeout)
131+
/// <returns>The <see cref="IReadOnlyList{IElement}"/>.</returns>
132+
internal static Task<IReadOnlyList<IElement>> WaitForElementsAsync<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, TimeSpan timeout)
133133
where TComponent : IComponent
134134
=> WaitForElementsCoreAsync(renderedComponent, cssSelector, matchElementCount: null, timeout: timeout);
135135

@@ -142,8 +142,8 @@ internal static Task<IRefreshableElementCollection<IElement>> WaitForElementsAsy
142142
/// <param name="matchElementCount">The exact number of elements to that the <paramref name="cssSelector"/> should match.</param>
143143
/// <param name="timeout">The maximum time to wait for elements to appear.</param>
144144
/// <exception cref="WaitForFailedException">Thrown if no elements is found matching the <paramref name="cssSelector"/> within the default timeout.</exception>
145-
/// <returns>The <see cref="IRefreshableElementCollection{IElement}"/>.</returns>
146-
internal static Task<IRefreshableElementCollection<IElement>> WaitForElementsAsync<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, int matchElementCount, TimeSpan timeout)
145+
/// <returns>The <see cref="IReadOnlyList{IElement}"/>.</returns>
146+
internal static Task<IReadOnlyList<IElement>> WaitForElementsAsync<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector, int matchElementCount, TimeSpan timeout)
147147
where TComponent : IComponent
148148
=> WaitForElementsCoreAsync(renderedComponent, cssSelector, matchElementCount: matchElementCount, timeout: timeout);
149149

@@ -154,8 +154,8 @@ internal static Task<IRefreshableElementCollection<IElement>> WaitForElementsAsy
154154
/// <param name="renderedComponent">The render fragment or component find the matching element in.</param>
155155
/// <param name="cssSelector">The CSS selector to use to search for elements.</param>
156156
/// <exception cref="WaitForFailedException">Thrown if no elements is found matching the <paramref name="cssSelector"/> within the default timeout.</exception>
157-
/// <returns>The <see cref="IRefreshableElementCollection{IElement}"/>.</returns>
158-
internal static Task<IRefreshableElementCollection<IElement>> WaitForElementsAsync<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector)
157+
/// <returns>The <see cref="IReadOnlyList{IElement}"/>.</returns>
158+
internal static Task<IReadOnlyList<IElement>> WaitForElementsAsync<TComponent>(this IRenderedComponent<TComponent> renderedComponent, string cssSelector)
159159
where TComponent : IComponent
160160
=> WaitForElementsCoreAsync<TComponent>(renderedComponent, cssSelector, matchElementCount: null, timeout: null);
161161

@@ -185,7 +185,7 @@ private static async Task<IElement> WaitForElementCoreAsync<TComponent>(this IRe
185185
return await waiter.WaitTask;
186186
}
187187

188-
private static IRefreshableElementCollection<IElement> WaitForElementsCore<TComponent>(
188+
private static IReadOnlyList<IElement> WaitForElementsCore<TComponent>(
189189
this IRenderedComponent<TComponent> renderedComponent,
190190
string cssSelector,
191191
int? matchElementCount,
@@ -207,7 +207,7 @@ private static IRefreshableElementCollection<IElement> WaitForElementsCore<TComp
207207
}
208208
}
209209

210-
private static async Task<IRefreshableElementCollection<IElement>> WaitForElementsCoreAsync<TComponent>(
210+
private static async Task<IReadOnlyList<IElement>> WaitForElementsCoreAsync<TComponent>(
211211
this IRenderedComponent<TComponent> renderedComponent,
212212
string cssSelector,
213213
int? matchElementCount,

src/bunit/Extensions/WaitForHelpers/WaitForElementsHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Bunit.Extensions.WaitForHelpers;
77
/// <summary>
88
/// Represents an async wait helper, that will wait for a specified time for element(s) to become available in the DOM.
99
/// </summary>
10-
internal class WaitForElementsHelper<TComponent> : WaitForHelper<IRefreshableElementCollection<IElement>, TComponent>
10+
internal class WaitForElementsHelper<TComponent> : WaitForHelper<IReadOnlyList<IElement>, TComponent>
1111
where TComponent : IComponent
1212
{
1313
internal const string TimeoutBeforeFoundMessage = "The CSS selector did not result in any matching element(s) before the timeout period passed.";

tests/bunit.tests/BlazorE2E/ComponentRenderingTest.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,17 @@ public void CanTriggerKeyPressEvents()
106106
// List is initially empty
107107
var cut = Render<KeyPressEventComponent>();
108108
var inputElement = cut.Find("input");
109-
var liElements = cut.FindAll("li", enableAutoRefresh: true);
109+
var liElements = cut.FindAll("li");
110110
liElements.ShouldBeEmpty();
111111

112112
// Typing adds element
113113
inputElement.KeyPress("a");
114+
liElements = cut.FindAll("li");
114115
liElements.ShouldAllBe(li => Assert.Equal("a", li.TextContent));
115116

116117
// Typing again adds another element
117118
inputElement.KeyPress("b");
119+
liElements = cut.FindAll("li");
118120
liElements.ShouldAllBe(
119121
li => Assert.Equal("a", li.TextContent),
120122
li => Assert.Equal("b", li.TextContent));
@@ -285,7 +287,7 @@ public void CanRenderFragmentsWhilePreservingSurroundingElements()
285287
var cut = Render<RenderFragmentToggler>();
286288
var originalButton = cut.Find("button");
287289

288-
var fragmentElements = cut.FindAll("p[name=fragment-element]", enableAutoRefresh: true);
290+
var fragmentElements = cut.FindAll("p[name=fragment-element]");
289291
Assert.Empty(fragmentElements);
290292

291293
// The JS-side DOM builder handles regions correctly, placing elements
@@ -294,10 +296,12 @@ public void CanRenderFragmentsWhilePreservingSurroundingElements()
294296

295297
// When we click the button, the region is shown
296298
originalButton.Click();
299+
fragmentElements = cut.FindAll("p[name=fragment-element]");
297300
fragmentElements.Single().ShouldNotBeNull();
298301

299302
// The button itself was preserved, so we can click it again and see the effect
300303
originalButton.Click();
304+
fragmentElements = cut.FindAll("p[name=fragment-element]");
301305
Assert.Empty(fragmentElements);
302306
}
303307

@@ -516,11 +520,12 @@ public void CanRenderMultipleChildContent()
516520
e => Assert.Equal("Col2", e.TextContent),
517521
e => Assert.Equal("Col3", e.TextContent));
518522

519-
var tfootElements = cut.FindAll("table tfoot td", enableAutoRefresh: true);
523+
var tfootElements = cut.FindAll("table tfoot td");
520524
Assert.Empty(tfootElements);
521525
var toggle = cut.Find("#toggle");
522526
toggle.Change(true);
523527

528+
tfootElements = cut.FindAll("table tfoot td");
524529
Assert.Collection(
525530
tfootElements,
526531
e => Assert.Equal("The", e.TextContent),

tests/bunit.tests/Extensions/RefreshableQueryCollectionTest.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)