Skip to content

Commit 486f500

Browse files
Copilotjaviercn
andcommitted
Fix GenerateMarkerKey to handle string keys and add comprehensive tests
Co-authored-by: javiercn <[email protected]>
1 parent 3c62c5d commit 486f500

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

src/Components/Endpoints/src/Rendering/SSRRenderModeBoundary.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,12 @@ private ComponentMarkerKey GenerateMarkerKey(int sequence, object? componentKey)
212212
var sequenceString = sequence.ToString(CultureInfo.InvariantCulture);
213213

214214
var locationHash = $"{componentTypeNameHash}:{sequenceString}";
215-
var formattedComponentKey = (componentKey as IFormattable)?.ToString(null, CultureInfo.InvariantCulture) ?? string.Empty;
215+
var formattedComponentKey = componentKey switch
216+
{
217+
string str => str,
218+
IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture),
219+
_ => string.Empty
220+
};
216221

217222
return new()
218223
{

src/Components/Endpoints/test/SSRRenderModeBoundaryTest.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,117 @@ public Task SetParametersAsync(ParameterView parameters)
9696
=> throw new NotImplementedException();
9797
}
9898

99+
[Fact]
100+
public void GetComponentMarkerKey_WorksWithStringKey()
101+
{
102+
// Arrange
103+
var httpContext = new DefaultHttpContext();
104+
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode());
105+
var stringKey = "test-string-key";
106+
107+
// Act
108+
var markerKey = boundary.GetComponentMarkerKey(1, stringKey);
109+
110+
// Assert
111+
Assert.Equal(stringKey, markerKey.FormattedComponentKey);
112+
Assert.NotEmpty(markerKey.LocationHash);
113+
}
114+
115+
[Fact]
116+
public void GetComponentMarkerKey_WorksWithIntKey()
117+
{
118+
// Arrange
119+
var httpContext = new DefaultHttpContext();
120+
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode());
121+
var intKey = 42;
122+
123+
// Act
124+
var markerKey = boundary.GetComponentMarkerKey(1, intKey);
125+
126+
// Assert
127+
Assert.Equal("42", markerKey.FormattedComponentKey);
128+
Assert.NotEmpty(markerKey.LocationHash);
129+
}
130+
131+
[Fact]
132+
public void GetComponentMarkerKey_WorksWithGuidKey()
133+
{
134+
// Arrange
135+
var httpContext = new DefaultHttpContext();
136+
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode());
137+
var guidKey = Guid.Parse("12345678-1234-1234-1234-123456789012");
138+
139+
// Act
140+
var markerKey = boundary.GetComponentMarkerKey(1, guidKey);
141+
142+
// Assert
143+
Assert.Equal("12345678-1234-1234-1234-123456789012", markerKey.FormattedComponentKey);
144+
Assert.NotEmpty(markerKey.LocationHash);
145+
}
146+
147+
[Fact]
148+
public void GetComponentMarkerKey_WorksWithDoubleKey()
149+
{
150+
// Arrange
151+
var httpContext = new DefaultHttpContext();
152+
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode());
153+
var doubleKey = 123.45;
154+
155+
// Act
156+
var markerKey = boundary.GetComponentMarkerKey(1, doubleKey);
157+
158+
// Assert
159+
Assert.Equal("123.45", markerKey.FormattedComponentKey);
160+
Assert.NotEmpty(markerKey.LocationHash);
161+
}
162+
163+
[Fact]
164+
public void GetComponentMarkerKey_WorksWithDateTimeKey()
165+
{
166+
// Arrange
167+
var httpContext = new DefaultHttpContext();
168+
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode());
169+
var dateTimeKey = new DateTime(2023, 12, 25, 10, 30, 0, DateTimeKind.Utc);
170+
171+
// Act
172+
var markerKey = boundary.GetComponentMarkerKey(1, dateTimeKey);
173+
174+
// Assert
175+
Assert.Equal("12/25/2023 10:30:00", markerKey.FormattedComponentKey);
176+
Assert.NotEmpty(markerKey.LocationHash);
177+
}
178+
179+
[Fact]
180+
public void GetComponentMarkerKey_WorksWithNullKey()
181+
{
182+
// Arrange
183+
var httpContext = new DefaultHttpContext();
184+
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode());
185+
186+
// Act
187+
var markerKey = boundary.GetComponentMarkerKey(1, null);
188+
189+
// Assert
190+
Assert.Equal(string.Empty, markerKey.FormattedComponentKey);
191+
Assert.NotEmpty(markerKey.LocationHash);
192+
}
193+
194+
[Fact]
195+
public void GetComponentMarkerKey_WorksWithNonFormattableKey()
196+
{
197+
// Arrange
198+
var httpContext = new DefaultHttpContext();
199+
var boundary = new SSRRenderModeBoundary(httpContext, typeof(TestComponent), new InteractiveServerRenderMode());
200+
var nonFormattableKey = new object();
201+
202+
// Act
203+
var markerKey = boundary.GetComponentMarkerKey(1, nonFormattableKey);
204+
205+
// Assert
206+
Assert.Equal(string.Empty, markerKey.FormattedComponentKey);
207+
Assert.NotEmpty(markerKey.LocationHash);
208+
}
209+
99210
class ServerRenderModeSubclass : InteractiveServerRenderMode { }
100211
class WebAssemblyRenderModeSubclass : InteractiveWebAssemblyRenderMode { }
101212
class AutoRenderModeSubclass : InteractiveAutoRenderMode { }

0 commit comments

Comments
 (0)