Skip to content

Commit 980ffdf

Browse files
committed
Added Test
1 parent e6956c2 commit 980ffdf

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

src/LinkDotNet.Blog.Web/Features/Components/MarkdownTextArea.razor

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@
7070
var options = await UploadDialog.ShowAsync(file.Name);
7171
if (options is null)
7272
{
73+
await file.Cancel();
7374
continue;
7475
}
7576

76-
var url = await BlobUploadService.UploadFileAsync(options.Name, memoryStream, new());
77+
var url = await BlobUploadService.UploadFileAsync(options.Name, memoryStream, new UploadOptions
78+
{
79+
SetCacheControlHeader = options.CacheMedia,
80+
});
7781
file.UploadUrl = url;
7882
ToastService.ShowSuccess($"Successfully uploaded {file.Name}");
7983
}

src/LinkDotNet.Blog.Web/Features/Components/UploadFileModalDialog.razor

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@
33
<div class="modal-content">
44
<div class="modal-header">
55
<h5 class="modal-title">Configuration</h5>
6-
<button type="button" class="btn-close" @onclick="OnAbort"></button>
6+
<button type="button" class="btn-close" @onclick="Abort"></button>
77
</div>
88
<div class="modal-body">
9-
<EditForm Model="@model" OnValidSubmit="OnOk">
9+
<EditForm Model="@model" OnValidSubmit="Ok">
1010
<DataAnnotationsValidator />
1111
<div class="form-floating mb-3">
12-
<InputText type="text" class="form-control" id="name" placeholder="Filename"
13-
@bind-Value="model.Name" />
14-
<label for="name">Filename</label>
15-
<ValidationMessage For="() => model.Name"></ValidationMessage>
12+
<InputText type="text" class="form-control" id="name" placeholder="Filename"
13+
@bind-Value="model.Name"/>
14+
<label for="name">The filename of the media file. On services like Azure, this can include the directory path.</label>
15+
<ValidationMessage For="() => model.Name"></ValidationMessage>
1616
</div>
1717
<div class="form-check form-switch mb-3">
1818
<InputCheckbox class="form-check-input" id="cache" @bind-Value="model.CacheMedia" />
1919
<label class="form-check-label" for="cache">Enable Media Caching</label><br />
20-
<small class="form-text text-body-secondary">If enabled, the browser will cache the media file</small>
20+
<small class="form-text text-body-secondary">
21+
If enabled, the "Cache-Control" header will be set automatically.<br/>
22+
The value is set to one week. If disabled, no header will be set.
23+
</small>
2124
</div>
2225
</EditForm>
2326
</div>
2427
<div class="modal-footer">
25-
<button type="button" class="btn btn-secondary" @onclick="OnAbort">Abort</button>
26-
<button type="button" class="btn btn-primary" @onclick="OnOk">OK</button>
28+
<button id="abort" type="button" class="btn btn-secondary" @onclick="Abort">Abort</button>
29+
<button type="button" class="btn btn-primary" @onclick="Ok">OK</button>
2730
</div>
2831
</div>
2932
</div>
@@ -52,19 +55,19 @@
5255
return await result.Task;
5356
}
5457

55-
private void OnAbort()
58+
private void Abort()
5659
{
5760
modalDisplay = "none";
58-
modalClass = "";
61+
modalClass = string.Empty;
5962
showBackdrop = false;
6063
result.SetResult(null);
6164
StateHasChanged();
6265
}
6366

64-
private void OnOk()
67+
private void Ok()
6568
{
6669
modalDisplay = "none";
67-
modalClass = "";
70+
modalClass = string.Empty;
6871
showBackdrop = false;
6972
result.SetResult(model);
7073
StateHasChanged();

src/LinkDotNet.Blog.Web/Features/Services/FileUpload/AzureBlobStorageService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task<string> UploadFileAsync(string fileName, Stream fileStream, Up
3030
{
3131
blobOptions.HttpHeaders = new BlobHttpHeaders
3232
{
33-
CacheControl = "public, max-age=31536000"
33+
CacheControl = "public, max-age=604800"
3434
};
3535
}
3636

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Threading.Tasks;
2+
using LinkDotNet.Blog.Web.Features.Components;
3+
4+
namespace LinkDotNet.Blog.UnitTests.Web.Features.Components;
5+
6+
public class UploadFileModalDialogTests : BunitContext
7+
{
8+
[Fact]
9+
public async Task ShouldReturnFilenameAndSettingWhenSubmitting()
10+
{
11+
var cut = Render<UploadFileModalDialog>();
12+
var task = cut.InvokeAsync(() => cut.Instance.ShowAsync("Filename.png"));
13+
cut.Find("#cache").Change(false);
14+
15+
await cut.Find("form").SubmitAsync();
16+
17+
var result = await task;
18+
result.ShouldNotBeNull();
19+
result.Name.ShouldBe("Filename.png");
20+
result.CacheMedia.ShouldBeFalse();
21+
}
22+
23+
[Fact]
24+
public async Task ShouldReturnNullWhenAborted()
25+
{
26+
var cut = Render<UploadFileModalDialog>();
27+
var task = cut.InvokeAsync(() => cut.Instance.ShowAsync("Filename.png"));
28+
29+
cut.Find("#abort").Click();
30+
31+
var result = await task;
32+
result.ShouldBeNull();
33+
}
34+
}

0 commit comments

Comments
 (0)