Skip to content

Commit

Permalink
Use replace instead of remove+add on ObservableCollectionAdaptor.DoUp…
Browse files Browse the repository at this point in the history
…date (#381)

* Use replace instead of remove+add

* Add unit test

Co-authored-by: Marius Aiordachioaei <marius.aiordachioaei@itiviti.com>
  • Loading branch information
wilversings and Marius Aiordachioaei authored Jul 24, 2020
1 parent 5880de9 commit 6912429
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Reactive.Linq;
using DynamicData.Binding;
using DynamicData.Tests.Domain;
using FluentAssertions;
Expand Down Expand Up @@ -49,6 +51,25 @@ public void UpdateToSourceUpdatesTheDestination()
_collection.First().Should().Be(personUpdated, "Should be updated person");
}

[Fact]
public void UpdateToSourceSendsReplaceOnDestination()
{
var person = new Person("Adult1", 50);
var anotherPerson = new Person("Adult1", 51);
NotifyCollectionChangedAction action = default;
_source.AddOrUpdate(person);

using (_collection
.ObserveCollectionChanges()
.Select(x => x.EventArgs.Action)
.Subscribe(updateType => action = updateType))
{
_source.AddOrUpdate(anotherPerson);
}

action.Should().Be(NotifyCollectionChangedAction.Replace, "The notification type should be Replace");
}

[Fact]
public void RemoveSourceRemovesFromTheDestination()
{
Expand Down
3 changes: 1 addition & 2 deletions src/DynamicData/Binding/ObservableCollectionAdaptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ private void DoUpdate(IChangeSet<TObject, TKey> updates, IObservableCollection<T
list.Remove(update.Current);
break;
case ChangeReason.Update:
list.Remove(update.Previous.Value);
list.Add(update.Current);
list.Replace(update.Previous.Value, update.Current);
break;
}
}
Expand Down

0 comments on commit 6912429

Please sign in to comment.