Skip to content

Commit a917d54

Browse files
committed
KeyboardEvent extensions accepting Key
1 parent 674f63f commit a917d54

File tree

4 files changed

+109
-10
lines changed

4 files changed

+109
-10
lines changed

src/bunit.web/EventDispatchExtensions/Key.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ static bool IsSuitableForCombination(Key key)
536536
return true;
537537
}
538538

539-
return !(x is null) && x.Equals(y);
539+
return x is not null && x.Equals(y);
540540
}
541541

542542
/// <summary>
@@ -559,11 +559,11 @@ static bool IsSuitableForCombination(Key key)
559559
[SuppressMessage("Usage", "CA2225:Operator overloads have named alternates", Justification = "Alternative method is named " + nameof(Combine))]
560560
public static Key operator +(Key x, Key? y)
561561
{
562-
if (!(x is null))
562+
if (x is not null)
563563
{
564564
return x.Combine(y);
565565
}
566-
else if (!(y is null))
566+
else if (y is not null)
567567
{
568568
return y.Combine(x);
569569
}

src/bunit.web/EventDispatchExtensions/KeyboardEventDispatchExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public static void KeyDown(this IElement element, string key, string? code = def
4343
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
4444
public static void KeyDown(this IElement element, KeyboardEventArgs eventArgs) => _ = KeyDownAsync(element, eventArgs);
4545

46+
/// <summary>
47+
/// Raises the <c>@onkeydown</c> event on <paramref name="element"/>, passing the provided <paramref name="key"/>
48+
/// to the event handler.
49+
/// </summary>
50+
/// <param name="element">The element to raise the event on.</param>
51+
/// <param name="key">The keyboard key to pass to the event handler.</param>
52+
public static void KeyDown(this IElement element, Key key) => _ = KeyDownAsync(element, key);
53+
4654
/// <summary>
4755
/// Raises the <c>@onkeydown</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
4856
/// to the event handler.
@@ -86,6 +94,14 @@ public static void KeyUp(this IElement element, string key, string? code = defau
8694
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
8795
public static void KeyUp(this IElement element, KeyboardEventArgs eventArgs) => _ = KeyUpAsync(element, eventArgs);
8896

97+
/// <summary>
98+
/// Raises the <c>@onkeyup</c> event on <paramref name="element"/>, passing the provided <paramref name="key"/>
99+
/// to the event handler.
100+
/// </summary>
101+
/// <param name="element">The element to raise the event on.</param>
102+
/// <param name="key">The keyboard key to pass to the event handler.</param>
103+
public static void KeyUp(this IElement element, Key key) => _ = KeyUpAsync(element, key);
104+
89105
/// <summary>
90106
/// Raises the <c>@onkeyup</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
91107
/// to the event handler.
@@ -129,6 +145,14 @@ public static void KeyPress(this IElement element, string key, string? code = de
129145
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
130146
public static void KeyPress(this IElement element, KeyboardEventArgs eventArgs) => _ = KeyPressAsync(element, eventArgs);
131147

148+
/// <summary>
149+
/// Raises the <c>@onkeypress</c> event on <paramref name="element"/>, passing the provided <paramref name="key"/>
150+
/// to the event handler.
151+
/// </summary>
152+
/// <param name="element">The element to raise the event on.</param>
153+
/// <param name="key">The keyboard key to pass to the event handler.</param>
154+
public static void KeyPress(this IElement element, Key key) => _ = KeyPressAsync(element, key);
155+
132156
/// <summary>
133157
/// Raises the <c>@onkeypress</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
134158
/// to the event handler.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ public void NullsAreEqual()
181181
[MemberData(nameof(NonEqualsTestData))]
182182
public void EqualsShouldBeFalseForDifferentKeys(Key? key1, Key? key2)
183183
{
184-
if (!(key1 is null))
184+
if (key1 is not null)
185185
{
186186
key1.Equals(key2).ShouldBeFalse();
187187
key1.Equals((object?)key2).ShouldBeFalse();
188188
}
189189

190-
if (!(key2 is null))
190+
if (key2 is not null)
191191
{
192192
key2.Equals(key1).ShouldBeFalse();
193193
key2.Equals((object?)key1).ShouldBeFalse();

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

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
13
using System.Reflection;
24
using Microsoft.AspNetCore.Components.Web;
35
using Shouldly;
@@ -7,10 +9,12 @@ namespace Bunit
79
{
810
public class KeyboardEventDispatchExtensionsTest : EventDispatchExtensionsTest<KeyboardEventArgs>
911
{
12+
public static IEnumerable<MethodInfo[]> Helpers { get; } = GetEventHelperMethods(typeof(KeyboardEventDispatchExtensions), x => x.GetParameters().All(p => p.ParameterType != typeof(Key)));
13+
1014
protected override string ElementName => "input";
1115

1216
[Theory(DisplayName = "Keyboard events are raised correctly through helpers")]
13-
[MemberData(nameof(GetEventHelperMethods), typeof(KeyboardEventDispatchExtensions))]
17+
[MemberData(nameof(Helpers))]
1418
public void CanRaiseEvents(MethodInfo helper)
1519
{
1620
var expected = new KeyboardEventArgs()
@@ -47,13 +51,13 @@ public void CanRaiseKeyDownWithCtrlEnter()
4751
spy.RaisedEvent.ShouldBeEquivalentTo(expected);
4852
}
4953

50-
[Fact(DisplayName = "KeyDown event is raised correctly through helper using character keys")]
51-
public void CanRaiseKeyUpWithAKey()
54+
[Fact(DisplayName = "KeyDown event is raised correctly through helper using character key")]
55+
public void CanRaiseKeyDownWithAKey()
5256
{
53-
var spy = CreateTriggerSpy(ElementName, "onkeyup");
57+
var spy = CreateTriggerSpy(ElementName, "onkeydown");
5458
spy.Trigger(element =>
5559
{
56-
element.KeyUp((Key)'A');
60+
element.KeyDown('A');
5761
});
5862

5963
var expected = new KeyboardEventArgs
@@ -63,5 +67,76 @@ public void CanRaiseKeyUpWithAKey()
6367
};
6468
spy.RaisedEvent.ShouldBeEquivalentTo(expected);
6569
}
70+
71+
[Fact(DisplayName = "KeyUp event is raised correctly through helper using special key")]
72+
public void CanRaiseKeyUpWithShiftSpace()
73+
{
74+
var spy = CreateTriggerSpy(ElementName, "onkeyup");
75+
spy.Trigger(element =>
76+
{
77+
element.KeyUp(Key.Space + Key.Shift + Key.Alt);
78+
});
79+
80+
var expected = new KeyboardEventArgs
81+
{
82+
Key = " ",
83+
Code = "Space",
84+
ShiftKey = true,
85+
AltKey = true
86+
};
87+
spy.RaisedEvent.ShouldBeEquivalentTo(expected);
88+
}
89+
90+
[Fact(DisplayName = "KeyUp event is raised correctly through helper using character key")]
91+
public void CanRaiseKeyUpWithBKey()
92+
{
93+
var spy = CreateTriggerSpy(ElementName, "onkeyup");
94+
spy.Trigger(element =>
95+
{
96+
element.KeyUp(Key.Alt + 'B');
97+
});
98+
99+
var expected = new KeyboardEventArgs
100+
{
101+
Key = "B",
102+
Code = "B",
103+
AltKey = true
104+
};
105+
spy.RaisedEvent.ShouldBeEquivalentTo(expected);
106+
}
107+
108+
[Fact(DisplayName = "KeyPress event is raised correctly through helper using special key")]
109+
public void CanRaiseKeyPressWithNum8Key()
110+
{
111+
var spy = CreateTriggerSpy(ElementName, "onkeypress");
112+
spy.Trigger(element =>
113+
{
114+
element.KeyPress(Key.NumberPad8);
115+
});
116+
117+
var expected = new KeyboardEventArgs
118+
{
119+
Key = "8",
120+
Code = "Numpad8"
121+
};
122+
spy.RaisedEvent.ShouldBeEquivalentTo(expected);
123+
}
124+
125+
[Fact(DisplayName = "KeyPress event is raised correctly through helper using character key")]
126+
public void CanRaiseKeyPressWith8Key()
127+
{
128+
var spy = CreateTriggerSpy(ElementName, "onkeypress");
129+
spy.Trigger(element =>
130+
{
131+
element.KeyPress('8');
132+
});
133+
134+
var expected = new KeyboardEventArgs
135+
{
136+
Key = "8",
137+
Code = "8"
138+
};
139+
spy.RaisedEvent.ShouldBeEquivalentTo(expected);
140+
}
66141
}
67142
}

0 commit comments

Comments
 (0)