Skip to content

Commit 7f59da1

Browse files
committed
Use one element instead of two for ProfileInformationEntry
1 parent 177f0ad commit 7f59da1

File tree

9 files changed

+59
-95
lines changed

9 files changed

+59
-95
lines changed

LinkDotNet.Blog.IntegrationTests/Infrastructure/Persistence/Sql/ProfileRepositoryTests.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,22 @@ public class ProfileRepositoryTests : SqlDatabaseTestBase
1010
[Fact]
1111
public async Task ShouldSaveAndRetrieveAllEntries()
1212
{
13-
var item1 = new ProfileInformationEntryBuilder().WithKey("key1").WithValue("value1").Build();
14-
var item2 = new ProfileInformationEntryBuilder().WithKey("key2").WithValue("value2").Build();
13+
var item1 = new ProfileInformationEntryBuilder().WithContent("key1").Build();
14+
var item2 = new ProfileInformationEntryBuilder().WithContent("key2").Build();
1515
await ProfileRepository.AddAsync(item1);
1616
await ProfileRepository.AddAsync(item2);
1717

1818
var items = await ProfileRepository.GetAllAsync();
1919

20-
items[0].Key.Should().Be("key1");
21-
items[0].Value.Should().Be("value1");
22-
items[1].Key.Should().Be("key2");
23-
items[1].Value.Should().Be("value2");
20+
items[0].Content.Should().Be("key1");
21+
items[1].Content.Should().Be("key2");
2422
}
2523

2624
[Fact]
2725
public async Task ShouldDelete()
2826
{
29-
var item1 = new ProfileInformationEntryBuilder().WithKey("key1").WithValue("value1").Build();
30-
var item2 = new ProfileInformationEntryBuilder().WithKey("key2").WithValue("value2").Build();
27+
var item1 = new ProfileInformationEntryBuilder().WithContent("key1").Build();
28+
var item2 = new ProfileInformationEntryBuilder().WithContent("key2").Build();
3129
await ProfileRepository.AddAsync(item1);
3230
await ProfileRepository.AddAsync(item2);
3331

@@ -41,7 +39,7 @@ public async Task ShouldDelete()
4139
[Fact]
4240
public async Task NoopOnDeleteWhenEntryNotFound()
4341
{
44-
var item = new ProfileInformationEntryBuilder().WithKey("key1").WithValue("value1").Build();
42+
var item = new ProfileInformationEntryBuilder().WithContent("key1").Build();
4543
await ProfileRepository.AddAsync(item);
4644

4745
await ProfileRepository.DeleteAsync("SomeIdWhichHopefullyDoesNotExist");

LinkDotNet.Blog.IntegrationTests/Web/Shared/ProfileTests.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class ProfileTests : TestContext
1818
[Fact]
1919
public void ShouldRenderAllItemsSortedByCreated()
2020
{
21-
var entry1 = new ProfileInformationEntryBuilder().WithKey("key 1").WithCreatedDate(new DateTime(1)).Build();
22-
var entry2 = new ProfileInformationEntryBuilder().WithKey("key 2").WithCreatedDate(new DateTime(2)).Build();
21+
var entry1 = new ProfileInformationEntryBuilder().WithContent("key 1").WithCreatedDate(new DateTime(1)).Build();
22+
var entry2 = new ProfileInformationEntryBuilder().WithContent("key 2").WithCreatedDate(new DateTime(2)).Build();
2323
var repoMock = RegisterServices();
2424
repoMock.Setup(r => r.GetAllAsync())
2525
.ReturnsAsync(new List<ProfileInformationEntry> { entry1, entry2 });
@@ -56,20 +56,18 @@ public void ShouldAddEntry()
5656
.Callback<ProfileInformationEntry>(p => entryToDb = p);
5757
var cut = RenderComponent<Profile>(p => p.Add(s => s.IsAuthenticated, true));
5858
var addShortItemComponent = cut.FindComponent<AddProfileShortItem>();
59-
addShortItemComponent.FindAll("input")[0].Change("key");
60-
addShortItemComponent.FindAll("input")[1].Change("value");
59+
addShortItemComponent.Find("input").Change("key");
6160

6261
addShortItemComponent.Find("button").Click();
6362

6463
entryToDb.Should().NotBeNull();
65-
entryToDb.Key.Should().Be("key");
66-
entryToDb.Value.Should().Be("value");
64+
entryToDb.Content.Should().Be("key");
6765
}
6866

6967
[Fact]
7068
public void ShouldDeleteEntryWhenConfirmed()
7169
{
72-
var entryToDelete = new ProfileInformationEntryBuilder().WithKey("key 2").WithCreatedDate(new DateTime(2)).Build();
70+
var entryToDelete = new ProfileInformationEntryBuilder().WithContent("key 2").WithCreatedDate(new DateTime(2)).Build();
7371
entryToDelete.Id = "SomeId";
7472
var repoMock = RegisterServices();
7573
repoMock.Setup(r => r.GetAllAsync()).ReturnsAsync(new[] { entryToDelete });
@@ -84,7 +82,7 @@ public void ShouldDeleteEntryWhenConfirmed()
8482
[Fact]
8583
public void ShouldNotDeleteEntryWhenNotConfirmed()
8684
{
87-
var entryToDelete = new ProfileInformationEntryBuilder().WithKey("key 2").WithCreatedDate(new DateTime(2)).Build();
85+
var entryToDelete = new ProfileInformationEntryBuilder().WithContent("key 2").WithCreatedDate(new DateTime(2)).Build();
8886
entryToDelete.Id = "SomeId";
8987
var repoMock = RegisterServices();
9088
repoMock.Setup(r => r.GetAllAsync()).ReturnsAsync(new[] { entryToDelete });

LinkDotNet.Blog.TestUtilities/ProfileInformationEntryBuilder.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,12 @@ namespace LinkDotNet.Blog.TestUtilities
55
{
66
public class ProfileInformationEntryBuilder
77
{
8-
private string key = "Key";
9-
private string value = "Value";
8+
private string content = "Content";
109
private DateTime? createdDate;
1110

12-
public ProfileInformationEntryBuilder WithKey(string key)
11+
public ProfileInformationEntryBuilder WithContent(string key)
1312
{
14-
this.key = key;
15-
return this;
16-
}
17-
18-
public ProfileInformationEntryBuilder WithValue(string value)
19-
{
20-
this.value = value;
13+
this.content = key;
2114
return this;
2215
}
2316

@@ -29,7 +22,7 @@ public ProfileInformationEntryBuilder WithCreatedDate(DateTime createdDate)
2922

3023
public ProfileInformationEntry Build()
3124
{
32-
return ProfileInformationEntry.Create(key, value, createdDate);
25+
return ProfileInformationEntry.Create(content, createdDate);
3326
}
3427
}
3528
}

LinkDotNet.Blog.UnitTests/Domain/ProfileInformationEntryTests.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,28 @@ public class ProfileInformationEntryTests
1010
[Fact]
1111
public void ShouldCreateObject()
1212
{
13-
var result = ProfileInformationEntry.Create("key", "value");
13+
var result = ProfileInformationEntry.Create("key");
1414

15-
result.Key.Should().Be("key");
16-
result.Value.Should().Be("value");
15+
result.Content.Should().Be("key");
1716
}
1817

1918
[Theory]
20-
[InlineData("", "V")]
21-
[InlineData(" ", "V")]
22-
[InlineData(null, "V")]
23-
[InlineData("K", "")]
24-
[InlineData("K", " ")]
25-
[InlineData("K", null)]
26-
public void ShouldThrowExceptionWhenEmptyKeyOrValue(string key, string value)
19+
[InlineData("")]
20+
[InlineData(" ")]
21+
[InlineData(null)]
22+
public void ShouldThrowExceptionWhenEmptyKeyOrValue(string content)
2723
{
28-
Action act = () => ProfileInformationEntry.Create(key, value);
24+
Action act = () => ProfileInformationEntry.Create(content);
2925

3026
act.Should().Throw<ArgumentOutOfRangeException>();
3127
}
3228

3329
[Fact]
3430
public void ShouldTrim()
3531
{
36-
var result = ProfileInformationEntry.Create(" key ", " value ");
32+
var result = ProfileInformationEntry.Create(" key ");
3733

38-
result.Key.Should().Be("key");
39-
result.Value.Should().Be("value");
34+
result.Content.Should().Be("key");
4035
}
4136
}
4237
}

LinkDotNet.Blog.UnitTests/Infrastructure/Persistence/InMemory/ProfileRepositoryTests.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,22 @@ public ProfileRepositoryTests()
1818
[Fact]
1919
public async Task ShouldSaveAndRetrieveAllEntries()
2020
{
21-
var item1 = new ProfileInformationEntryBuilder().WithKey("key1").WithValue("value1").Build();
22-
var item2 = new ProfileInformationEntryBuilder().WithKey("key2").WithValue("value2").Build();
21+
var item1 = new ProfileInformationEntryBuilder().WithContent("key1").Build();
22+
var item2 = new ProfileInformationEntryBuilder().WithContent("key2").Build();
2323
await profileRepository.AddAsync(item1);
2424
await profileRepository.AddAsync(item2);
2525

2626
var items = await profileRepository.GetAllAsync();
2727

28-
items[0].Key.Should().Be("key1");
29-
items[0].Value.Should().Be("value1");
30-
items[1].Key.Should().Be("key2");
31-
items[1].Value.Should().Be("value2");
28+
items[0].Content.Should().Be("key1");
29+
items[1].Content.Should().Be("key2");
3230
}
3331

3432
[Fact]
3533
public async Task ShouldDelete()
3634
{
37-
var item1 = new ProfileInformationEntryBuilder().WithKey("key1").WithValue("value1").Build();
38-
var item2 = new ProfileInformationEntryBuilder().WithKey("key2").WithValue("value2").Build();
35+
var item1 = new ProfileInformationEntryBuilder().WithContent("key1").Build();
36+
var item2 = new ProfileInformationEntryBuilder().WithContent("key2").Build();
3937
await profileRepository.AddAsync(item1);
4038
await profileRepository.AddAsync(item2);
4139

@@ -49,7 +47,7 @@ public async Task ShouldDelete()
4947
[Fact]
5048
public async Task NoopOnDeleteWhenEntryNotFound()
5149
{
52-
var item = new ProfileInformationEntryBuilder().WithKey("key1").WithValue("value1").Build();
50+
var item = new ProfileInformationEntryBuilder().WithContent("key1").Build();
5351
await profileRepository.AddAsync(item);
5452

5553
await profileRepository.DeleteAsync("SomeIdWhichHopefullyDoesNotExist");
Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Collections.Generic;
2-
using Bunit;
1+
using Bunit;
32
using FluentAssertions;
43
using LinkDotNet.Blog.Web.Shared;
54
using Xunit;
@@ -11,32 +10,26 @@ public class AddProfileShortItemTests : TestContext
1110
[Fact]
1211
public void ShouldAddShortItem()
1312
{
14-
KeyValuePair<string, string> addedItem = default;
13+
string addedItem = null;
1514
var cut = RenderComponent<AddProfileShortItem>(
16-
p => p.Add(s => s.ValueAdded, pair => addedItem = pair));
17-
cut.FindAll("input")[0].Change("Key");
18-
cut.FindAll("input")[1].Change("Value");
15+
p => p.Add(s => s.ValueAdded, c => addedItem = c));
16+
cut.Find("input").Change("Key");
1917

2018
cut.Find("button").Click();
2119

22-
addedItem.Key.Should().Be("Key");
23-
addedItem.Value.Should().Be("Value");
20+
addedItem.Should().Be("Key");
2421
}
2522

2623
[Theory]
27-
[InlineData("", "v")]
28-
[InlineData(" ", "v")]
29-
[InlineData(null, "v")]
30-
[InlineData("k", "")]
31-
[InlineData("k", " ")]
32-
[InlineData("k", null)]
33-
public void ShouldNotAddItemWhenKeyOrValueIsEmpty(string key, string value)
24+
[InlineData("")]
25+
[InlineData(" ")]
26+
[InlineData(null)]
27+
public void ShouldNotAddItemWhenKeyOrValueIsEmpty(string content)
3428
{
3529
var wasInvoked = false;
3630
var cut = RenderComponent<AddProfileShortItem>(
3731
p => p.Add(s => s.ValueAdded, _ => wasInvoked = true));
38-
cut.FindAll("input")[0].Change(key);
39-
cut.FindAll("input")[1].Change(value);
32+
cut.Find("input").Change(content);
4033

4134
cut.Find("button").Click();
4235

@@ -47,13 +40,11 @@ public void ShouldNotAddItemWhenKeyOrValueIsEmpty(string key, string value)
4740
public void ShouldEmptyModelAfterTextEntered()
4841
{
4942
var cut = RenderComponent<AddProfileShortItem>();
50-
cut.FindAll("input")[0].Change("Key");
51-
cut.FindAll("input")[1].Change("Value");
43+
cut.Find("input").Change("Key");
5244

5345
cut.Find("button").Click();
5446

55-
cut.FindAll("input")[0].TextContent.Should().BeEmpty();
56-
cut.FindAll("input")[1].TextContent.Should().BeEmpty();
47+
cut.Find("input").TextContent.Should().BeEmpty();
5748
}
5849
}
5950
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
<li><input type="text" @bind-value="@key" placeholder="supports markdown"/>:<input type="text" @bind-value="@value" placeholder="supports markdown"/>
1+
<li><input type="text" @bind-value="@content" placeholder="supports markdown"/>
22
<button type="button" class="btn btn-default" aria-label="Add Item" @onclick="@AddItemAsync">
33
<i class="far fa-plus-square" aria-hidden="true"></i>
44
</button>
55
</li>
66

77
@code {
88
[Parameter]
9-
public EventCallback<KeyValuePair<string, string>> ValueAdded { get; set; }
9+
public EventCallback<string> ValueAdded { get; set; }
1010

11-
private string key = string.Empty;
12-
private string value = string.Empty;
11+
private string content = string.Empty;
1312

1413
private async Task AddItemAsync()
1514
{
16-
if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(value))
15+
if (string.IsNullOrWhiteSpace(content))
1716
{
1817
return;
1918
}
2019

21-
await ValueAdded.InvokeAsync(new KeyValuePair<string, string>(key, value));
22-
key = value = string.Empty;
20+
await ValueAdded.InvokeAsync(content);
21+
content = string.Empty;
2322
}
2423
}

LinkDotNet.Blog.Web/Shared/Profile.razor

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
@if (IsAuthenticated)
2121
{
2222
<button type="button" class="btn btn-default" aria-label="Delete Item" @onclick="() =>
23-
ShowDeleteDialog(entry.Key)">
23+
ShowDeleteDialog(entry.Content)">
2424
<i class="fas fa-trash-alt" aria-hidden="true"></i>
2525
</button>
2626
}
27-
<bold>@RenderMarkupString(entry.Key)</bold>: @RenderMarkupString(entry.Value)
27+
@RenderMarkupString(entry.Content)
2828
</li>
2929
}
3030
@if (IsAuthenticated)
@@ -58,14 +58,14 @@ OnYesPressed="DeleteItem"></ConfirmDialog>
5858

5959
private async Task DeleteItem()
6060
{
61-
var entryToDelete = profileInformationEntries.Single(p => p.Key == currentDeleteKey);
61+
var entryToDelete = profileInformationEntries.Single(p => p.Content == currentDeleteKey);
6262
profileInformationEntries.Remove(entryToDelete);
6363
await repository.DeleteAsync(entryToDelete.Id);
6464
}
6565

66-
private async Task AddValue(KeyValuePair<string, string> toAdd)
66+
private async Task AddValue(string toAdd)
6767
{
68-
var newEntry = ProfileInformationEntry.Create(toAdd.Key, toAdd.Value);
68+
var newEntry = ProfileInformationEntry.Create(toAdd);
6969

7070
profileInformationEntries.Add(newEntry);
7171
await repository.AddAsync(newEntry);

LinkDotNet.Domain/ProfileInformationEntry.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,20 @@ private ProfileInformationEntry()
1010

1111
public string Id { get; set; }
1212

13-
public string Key { get; private set; }
14-
15-
public string Value { get; private set; }
13+
public string Content { get; private set; }
1614

1715
public DateTime CreatedDate { get; private set; }
1816

19-
public static ProfileInformationEntry Create(string key, string value, DateTime? createdDate = null)
17+
public static ProfileInformationEntry Create(string key, DateTime? createdDate = null)
2018
{
2119
if (string.IsNullOrWhiteSpace(key))
2220
{
2321
throw new ArgumentOutOfRangeException(nameof(key));
2422
}
2523

26-
if (string.IsNullOrWhiteSpace(value))
27-
{
28-
throw new ArgumentOutOfRangeException(nameof(key));
29-
}
30-
3124
return new ProfileInformationEntry
3225
{
33-
Key = key.Trim(),
34-
Value = value.Trim(),
26+
Content = key.Trim(),
3527
CreatedDate = createdDate ?? DateTime.Now,
3628
};
3729
}

0 commit comments

Comments
 (0)