Skip to content

Commit

Permalink
refactor(BarcodeInput): remove business logic from behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
AudBrou committed Nov 15, 2024
1 parent 6a9d681 commit d523e25
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,6 @@ public void ValidGencodeFilled()
Command.Verify(_ => _.Execute("2970812075764"), Times.Once);

Check failure on line 37 in Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs

View workflow job for this annotation

GitHub Actions / Unit Tests Results

Smartway.UiComponent.UnitTests.Inputs.Barcode.OnTextChanged ► ValidGencodeFilled

Failed test found in: Smartway.UiComponent.UnitTests/TestResults/test-results-unittests.trx Error: Moq.MockException : Expected invocation on the mock once, but was 0 times: _ => _.Execute("2970812075764") Performed invocations: Mock<ICommand:1> (_): No invocations performed.
Raw output
Moq.MockException : 
Expected invocation on the mock once, but was 0 times: _ => _.Execute("2970812075764")

Performed invocations:

   Mock<ICommand:1> (_):
   No invocations performed.

   at Moq.Mock.Verify(Mock mock, LambdaExpression expression, Times times, String failMessage) in C:\projects\moq4\src\Moq\Mock.cs:line 337
   at Moq.Mock`1.Verify(Expression`1 expression, Times times) in C:\projects\moq4\src\Moq\Mock`1.cs:line 738
   at Moq.Mock`1.Verify(Expression`1 expression, Func`1 times) in C:\projects\moq4\src\Moq\Mock`1.cs:line 752
   at Smartway.UiComponent.UnitTests.Inputs.Barcode.OnTextChanged.ValidGencodeFilled() in /runner_work/Smartway.UiComponent/Smartway.UiComponent/Smartway.UiComponent.UnitTests/Inputs/Barcode/BarcodeInputBehaviorTest.cs:line 37
}

[Fact]
public void UnvalidGencodeFilled()
{
Entry.Text = "297081207576";
Entry.Text = "2970812075765";

Check.That(Entry.Text).IsEqualTo("297081207576");
Command.Verify(_ => _.Execute(It.IsAny<string>()), Times.Never);
}

[Fact]
public void GencodeNotTotallyFilled()
{
Entry.Text = "297081207";

Check.That(Entry.Text).IsEqualTo("297081207");
Command.Verify(_ => _.Execute(It.IsAny<string>()), Times.Never);
}

[Fact]
public void GencodeFilledWithNotDigitChar()
{
Expand All @@ -64,25 +45,5 @@ public void GencodeFilledWithNotDigitChar()
Check.That(Entry.Text).IsEqualTo(null);
Command.Verify(_ => _.Execute(It.IsAny<string>()), Times.Never);
}

[Fact]
public void GencodeFilledBigLength()
{
Entry.Text = "29708120757644";

Check.That(Entry.Text).IsEqualTo(null);
Command.Verify(_ => _.Execute(It.IsAny<string>()), Times.Never);
}

[Fact]
public void CanExecuteIsFalse()
{
Command.Setup(_ => _.CanExecute(It.IsAny<object>())).Returns(false);

Entry.Text = "2970812075764";

Check.That(Entry.Text).IsEqualTo("2970812075764");
Command.Verify(_ => _.Execute(It.IsAny<object>()), Times.Never);
}
}
}
27 changes: 10 additions & 17 deletions Smartway.UiComponent/Inputs/Barcode/BarcodeInputBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Linq;
using System;
using System.Linq;
using System.Windows.Input;
using Smartway.Barcode.Ean13;
using Smartway.UiComponent.Behaviors;
using Xamarin.Forms;

Expand All @@ -21,6 +21,7 @@ protected override void OnAttachedTo(BindableObject bindable)
base.OnAttachedTo(bindable);

AssociatedObject.Focused += OnAssociatedObjectOnFocused;
AssociatedObject.Completed += Completed;
AssociatedObject.TextChanged += OnTextChanged;
}

Expand All @@ -29,6 +30,7 @@ protected override void OnDetachingFrom(BindableObject bindable)
base.OnDetachingFrom(bindable);

AssociatedObject.Focused -= OnAssociatedObjectOnFocused;
AssociatedObject.Completed -= Completed;
AssociatedObject.TextChanged -= OnTextChanged;
}

Expand All @@ -45,27 +47,18 @@ private void OnTextChanged(object sender, TextChangedEventArgs e)
if (InputIsInvalid(e.NewTextValue))
{
AssociatedObject.Text = e.OldTextValue;
return;
}

if (e.NewTextValue.Length != Ean13.CheckedLength)
return;

if (Command == null || !Command.CanExecute(null))
return;

Command.Execute(e.NewTextValue);
}

private bool InputIsInvalid(string input)
{
if (input.Length > Ean13.CheckedLength)
return true;

if (input.Length == Ean13.CheckedLength && !Ean13.Check(input))
return true;

return input.ToCharArray().Any(_ => !char.IsDigit(_));
}

private void Completed(object sender, EventArgs e)
{
var entry = (Entry)sender;
Command.Execute(entry.Text);
}
}
}

0 comments on commit d523e25

Please sign in to comment.