Skip to content

Commit e811af6

Browse files
committed
Add KeyboardEventDispatcher test for special keys
1 parent 3c49690 commit e811af6

3 files changed

Lines changed: 76 additions & 29 deletions

File tree

src/bunit.web/EventDispatchExtensions/Key.cs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ private Key(string value, string code, bool controlKey, bool shiftKey, bool altK
6060

6161
/// <summary>
6262
/// Gets the key value of the key represented. If the value has a printed
63-
/// representation, this attribute's value is the same as the char attribute.
63+
/// representation, this attribute's value is the same as the char attribute.
6464
/// </summary>
6565
public string Value { get; }
6666

6767
/// <summary>
6868
/// Gets the string that identifies the physical key being pressed. The value is not
69-
/// affected by the current keyboard layout or modifier state, so a particular key
70-
/// will always return the same value.
69+
/// affected by the current keyboard layout or modifier state, so a particular key
70+
/// will always return the same value.
7171
/// </summary>
7272
public string Code { get; }
7373

@@ -337,23 +337,23 @@ private Key(string value, string code, bool controlKey, bool shiftKey, bool altK
337337
/// <param name="value">The key value.</param>
338338
public static Key FromChar(char value) => new Key(value);
339339

340-
/// <summary>
341-
/// Gets the value indicating whether the current object is equal to another object of the same type.
342-
/// </summary>
343-
/// <param name="obj">The object to compare with this object.</param>
344-
/// <returns><c>True</c> if the current object is equal to the other parameter; otherwise, <c>false</c>.</returns>
345-
public override bool Equals(object? obj)
346-
{
347-
return obj is Key key && Equals(key);
348-
}
340+
/// <summary>
341+
/// Gets the value indicating whether the current object is equal to another object of the same type.
342+
/// </summary>
343+
/// <param name="obj">The object to compare with this object.</param>
344+
/// <returns><c>True</c> if the current object is equal to the other parameter; otherwise, <c>false</c>.</returns>
345+
public override bool Equals(object? obj)
346+
{
347+
return obj is Key key && Equals(key);
348+
}
349349

350350
/// <summary>
351351
/// Gets the value indicating whether the current object is equal to another object of the same type.
352352
/// </summary>
353353
/// <param name="other">A key to compare with this object.</param>
354354
/// <returns><c>True</c> if the current object is equal to the other parameter; otherwise, <c>false</c>.</returns>
355-
public bool Equals(Key? other)
356-
{
355+
public bool Equals(Key? other)
356+
{
357357
if (other is null)
358358
{
359359
return false;
@@ -365,25 +365,25 @@ public bool Equals(Key? other)
365365
ShiftKey == other.ShiftKey &&
366366
AltKey == other.AltKey &&
367367
CommandKey == other.CommandKey;
368-
}
368+
}
369369

370370
/// <summary>
371371
/// Gets hash code of this object.
372372
/// </summary>
373373
/// <returns>A hash code for the current object.</returns>
374-
public override int GetHashCode()
375-
{
376-
return HashCode.Combine(Value, Code, ControlKey, ShiftKey, AltKey, CommandKey);
377-
}
374+
public override int GetHashCode()
375+
{
376+
return HashCode.Combine(Value, Code, ControlKey, ShiftKey, AltKey, CommandKey);
377+
}
378378

379379
/// <summary>
380380
/// Gets a string that represents the current object.
381381
/// </summary>
382382
/// <returns>A string that represents the current object.</returns>
383-
public override string ToString()
384-
{
385-
return Value;
386-
}
383+
public override string ToString()
384+
{
385+
return Value;
386+
}
387387

388388
/// <summary>
389389
/// Gets the key with new value of Control key.
@@ -526,11 +526,14 @@ static bool IsSuitableForCombination(Key key)
526526
/// <returns>The Key instance with character value.</returns>
527527
[return: NotNullIfNotNull("key")]
528528
[SuppressMessage("Usage", "CA2225:Operator overloads have named alternates", Justification = "Key should have minimal relation to KeyboardEventArgs type")]
529-
public static implicit operator KeyboardEventArgs?(Key? key)
529+
public static implicit operator KeyboardEventArgs(Key key)
530530
{
531531
if (key is null)
532532
{
533-
return null;
533+
// In future the operator should support null as input.
534+
// However, NotNullIfNotNull does not work correctly with operators.
535+
// Therfore workaround is to make input non-nullable.
536+
return null!;
534537
}
535538

536539
return new KeyboardEventArgs
@@ -543,5 +546,5 @@ static bool IsSuitableForCombination(Key key)
543546
MetaKey = key.CommandKey
544547
};
545548
}
546-
}
549+
}
547550
}

tests/bunit.web.tests/EventDispatchExtensions/KeyTest.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class KeyTest
1111
{
1212
public static IEnumerable<object[]> CharTestData { get; } = new[] { ' ', 'x', 'A', '2', '0', '&', (char)0 }
1313
.Select(c => new object[] { c }).ToList();
14-
14+
1515
public static IEnumerable<Key[]> EqualsTestData { get; } = GetEqualsTestData().Select(i => new[] { i.Item1, i.Item2 });
1616

1717
public static IEnumerable<Key?[]> NonEqualsTestData { get; } = GetNonEqualsTestData().Select(i => new[] { i.Item1, i.Item2 });
@@ -308,7 +308,7 @@ public void Combine2MainKeys(Key key1, Key key2)
308308
[MemberData(nameof(KeyboardEventArgsTestData))]
309309
public void ToKeyboardEventArgs(Key key, string value, string code, bool ctrlKey, bool shiftKey, bool altKey, bool metaKey)
310310
{
311-
KeyboardEventArgs result = key!;
311+
KeyboardEventArgs result = key;
312312
result.Key.ShouldBe(value);
313313
result.Code.ShouldBe(code);
314314
result.CtrlKey.ShouldBe(ctrlKey);
@@ -319,6 +319,14 @@ public void ToKeyboardEventArgs(Key key, string value, string code, bool ctrlKey
319319
result.Type.ShouldBe(null);
320320
}
321321

322+
[Fact(DisplayName = "Can convert null to KeyboardEventArgs")]
323+
public void NullToKeyboardEventArgs()
324+
{
325+
Key key = null!;
326+
KeyboardEventArgs result = key;
327+
result.ShouldBeNull();
328+
}
329+
322330
private static IEnumerable<ValueTuple<Key, Key>> GetEqualsTestData()
323331
{
324332
var xKey = Key.FromChar('x');

tests/bunit.web.tests/EventDispatchExtensions/KeyboardEventDispatchExtensionsTest.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Reflection;
22
using Microsoft.AspNetCore.Components.Web;
3+
using Shouldly;
34
using Xunit;
45

56
namespace Bunit
67
{
7-
public class KeyboardEventDispatchExtensionsTest : EventDispatchExtensionsTest<KeyboardEventArgs>
8+
public class KeyboardEventDispatchExtensionsTest : EventDispatchExtensionsTest<KeyboardEventArgs>
89
{
910
protected override string ElementName => "input";
1011

@@ -27,5 +28,40 @@ public void CanRaiseEvents(MethodInfo helper)
2728

2829
VerifyEventRaisesCorrectly(helper, expected);
2930
}
31+
32+
[Fact(DisplayName = "KeyDown event is raised correctly through helper using special key")]
33+
public void CanRaiseKeyDownWithCtrlEnter()
34+
{
35+
var spy = CreateTriggerSpy(ElementName, "onkeydown");
36+
spy.Trigger(element =>
37+
{
38+
element.KeyDown(Key.Enter + Key.Control);
39+
});
40+
41+
var expected = new KeyboardEventArgs
42+
{
43+
Key = "Enter",
44+
Code = "Enter",
45+
CtrlKey = true
46+
};
47+
spy.RaisedEvent.ShouldBeEquivalentTo(expected);
48+
}
49+
50+
[Fact(DisplayName = "KeyDown event is raised correctly through helper using character keys")]
51+
public void CanRaiseKeyUpWithAKey()
52+
{
53+
var spy = CreateTriggerSpy(ElementName, "onkeyup");
54+
spy.Trigger(element =>
55+
{
56+
element.KeyUp((Key)'A');
57+
});
58+
59+
var expected = new KeyboardEventArgs
60+
{
61+
Key = "A",
62+
Code = "A"
63+
};
64+
spy.RaisedEvent.ShouldBeEquivalentTo(expected);
65+
}
3066
}
3167
}

0 commit comments

Comments
 (0)