Proper unit tests for masked Textfields #3960
-
My most recent PR has shown that writing unit tests for masked text fields is somewhat tricky. Calling
The workaround I have found is to fetch the mask component itself: var maskComp = maskTextFieldComp.FindComponent<MudMask>();
var mask = maskComp.Instance; And then use keyboard events to provide input: await comp.InvokeAsync(() => mask.HandleKeyDown(new KeyboardEventArgs() { Key = "m" }));
await comp.InvokeAsync(() => mask.HandleKeyDown(new KeyboardEventArgs() { Key = "u" }));
await comp.InvokeAsync(() => mask.HandleKeyDown(new KeyboardEventArgs() { Key = "d" })); Is this the correct procedure or is there a better way of testing masked textfields? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You are correct. Masked text fields have to be tested differently. I suggest you move the Reset test to the other TF masking tests and look how I did it there (in MaskTests.cs). For instance: [Test]
public async Task MaskTest_MultipleTFsLinkedViaTwoWayBinding()
{
var comp = Context.RenderComponent<MaskedTextFieldTwoWayBindingTest>();
var tfs=comp.FindComponents<MudTextField<string>>().Select(x=>x.Instance).ToArray();
var masks = comp.FindComponents<MudMask>().Select(x=>x.Instance).ToArray();
await comp.InvokeAsync(() => masks[0].OnPaste("123456"));
masks[0].Mask.ToString().Should().Be("123-456|");
comp.WaitForAssertion(()=>masks[1].Mask.ToString().Should().Be("12/34/56|"));
tfs[0].Text.Should().Be("123-456");
tfs[1].Text.Should().Be("12/34/56");
await comp.InvokeAsync(() => masks[1].HandleKeyDown(new KeyboardEventArgs() { Key = "Backspace" }));
masks[1].Mask.ToString().Should().Be("12/34/5|");
comp.WaitForAssertion(() => masks[0].Mask.ToString().Should().Be("123-45|"));
tfs[0].Text.Should().Be("123-45");
tfs[1].Text.Should().Be("12/34/5");
} |
Beta Was this translation helpful? Give feedback.
You are correct. Masked text fields have to be tested differently. I suggest you move the Reset test to the other TF masking tests and look how I did it there (in MaskTests.cs). For instance: