mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-03 12:37:40 +08:00
fix(module: select): OnDataSourceChange called when expected (#1419)
* fix(module:select): OnDataSourceChange called when expected * test(module:select): add missing scenari
This commit is contained in:
parent
555fdb9a43
commit
883b7c6e11
@ -228,10 +228,12 @@ namespace AntDesign
|
||||
SelectedOptionItems.Clear();
|
||||
|
||||
Value = default;
|
||||
var sameObject = object.ReferenceEquals(_datasource, value);
|
||||
|
||||
_datasource = value;
|
||||
|
||||
OnDataSourceChanged?.Invoke();
|
||||
if (!sameObject)
|
||||
OnDataSourceChanged?.Invoke();
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -0,0 +1,102 @@
|
||||
@inherits AntDesignTestBase
|
||||
@code {
|
||||
record Person(int Id, string Name);
|
||||
List<Person> _persons = new List<Person>
|
||||
{
|
||||
new Person(1, "John"),
|
||||
new Person(2, "Lucy"),
|
||||
new Person(3, "Jack"),
|
||||
new Person(4, "Emily"),
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public async Task Action_fired_when_datasource_changed()
|
||||
{
|
||||
//Arrange
|
||||
JSInterop.Setup<AntDesign.JsInterop.DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
|
||||
.SetResult(new AntDesign.JsInterop.DomRect());
|
||||
bool handlerCalled = false;
|
||||
Action OnDataSourceChangedHandler = () => handlerCalled = true;
|
||||
|
||||
var cut = Render<AntDesign.Select<int, Person>>(
|
||||
@<AntDesign.Select DataSource="@_persons"
|
||||
LabelName="@nameof(Person.Name)"
|
||||
ValueName="@nameof(Person.Id)"
|
||||
Value="0"
|
||||
OnDataSourceChanged="OnDataSourceChangedHandler">
|
||||
</AntDesign.Select>);
|
||||
//Act
|
||||
cut.SetParametersAndRender(parameters => parameters.Add(p => p.DataSource, new List<Person>()));
|
||||
//Assert
|
||||
handlerCalled.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Action_not_fired_when_datasource_item_added()
|
||||
{
|
||||
//Arrange
|
||||
JSInterop.Setup<AntDesign.JsInterop.DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
|
||||
.SetResult(new AntDesign.JsInterop.DomRect());
|
||||
bool handlerCalled = false;
|
||||
Action OnDataSourceChangedHandler = () => handlerCalled = true;
|
||||
|
||||
var cut = Render<AntDesign.Select<int, Person>>(
|
||||
@<AntDesign.Select DataSource="@_persons"
|
||||
LabelName="@nameof(Person.Name)"
|
||||
ValueName="@nameof(Person.Id)"
|
||||
Value="0"
|
||||
OnDataSourceChanged="OnDataSourceChangedHandler">
|
||||
</AntDesign.Select>);
|
||||
//Act
|
||||
_persons.Add(new Person(5, "Fank"));
|
||||
cut.SetParametersAndRender(parameters => parameters.Add(p => p.DataSource, _persons));
|
||||
//Assert
|
||||
handlerCalled.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Action_not_fired_when_datasource_item_removed()
|
||||
{
|
||||
//Arrange
|
||||
JSInterop.Setup<AntDesign.JsInterop.DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
|
||||
.SetResult(new AntDesign.JsInterop.DomRect());
|
||||
bool handlerCalled = false;
|
||||
Action OnDataSourceChangedHandler = () => handlerCalled = true;
|
||||
_persons = new List<Person> { _persons[0] };
|
||||
var cut = Render<AntDesign.Select<int, Person>>(
|
||||
@<AntDesign.Select DataSource="@_persons"
|
||||
LabelName="@nameof(Person.Name)"
|
||||
ValueName="@nameof(Person.Id)"
|
||||
Value="0"
|
||||
OnDataSourceChanged="OnDataSourceChangedHandler">
|
||||
</AntDesign.Select>);
|
||||
//Act
|
||||
_persons.RemoveAt(0);
|
||||
cut.SetParametersAndRender(parameters => parameters.Add(p => p.DataSource, _persons));
|
||||
//Assert
|
||||
handlerCalled.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Action_not_fired_when_datasource_cleared()
|
||||
{
|
||||
//Arrange
|
||||
JSInterop.Setup<AntDesign.JsInterop.DomRect>(JSInteropConstants.GetBoundingClientRect, _ => true)
|
||||
.SetResult(new AntDesign.JsInterop.DomRect());
|
||||
bool handlerCalled = false;
|
||||
Action OnDataSourceChangedHandler = () => handlerCalled = true;
|
||||
|
||||
var cut = Render<AntDesign.Select<int, Person>>(
|
||||
@<AntDesign.Select DataSource="@_persons"
|
||||
LabelName="@nameof(Person.Name)"
|
||||
ValueName="@nameof(Person.Id)"
|
||||
Value="0"
|
||||
OnDataSourceChanged="OnDataSourceChangedHandler">
|
||||
</AntDesign.Select>);
|
||||
//Act
|
||||
_persons.Clear();
|
||||
cut.SetParametersAndRender(parameters => parameters.Add(p => p.DataSource, _persons));
|
||||
//Assert
|
||||
handlerCalled.Should().BeFalse();
|
||||
}
|
||||
}
|
@ -8,4 +8,5 @@
|
||||
@using Xunit
|
||||
@using Moq
|
||||
@using AntDesign
|
||||
@using AntDesign.JsInterop
|
||||
@using FluentAssertions
|
Loading…
Reference in New Issue
Block a user