Skip to content

Commit

Permalink
Added Test
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Nov 6, 2024
1 parent e6956c2 commit 980ffdf
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@
var options = await UploadDialog.ShowAsync(file.Name);
if (options is null)
{
await file.Cancel();
continue;
}

var url = await BlobUploadService.UploadFileAsync(options.Name, memoryStream, new());
var url = await BlobUploadService.UploadFileAsync(options.Name, memoryStream, new UploadOptions
{
SetCacheControlHeader = options.CacheMedia,
});
file.UploadUrl = url;
ToastService.ShowSuccess($"Successfully uploaded {file.Name}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Configuration</h5>
<button type="button" class="btn-close" @onclick="OnAbort"></button>
<button type="button" class="btn-close" @onclick="Abort"></button>
</div>
<div class="modal-body">
<EditForm Model="@model" OnValidSubmit="OnOk">
<EditForm Model="@model" OnValidSubmit="Ok">
<DataAnnotationsValidator />
<div class="form-floating mb-3">
<InputText type="text" class="form-control" id="name" placeholder="Filename"
@bind-Value="model.Name" />
<label for="name">Filename</label>
<ValidationMessage For="() => model.Name"></ValidationMessage>
<InputText type="text" class="form-control" id="name" placeholder="Filename"
@bind-Value="model.Name"/>
<label for="name">The filename of the media file. On services like Azure, this can include the directory path.</label>
<ValidationMessage For="() => model.Name"></ValidationMessage>
</div>
<div class="form-check form-switch mb-3">
<InputCheckbox class="form-check-input" id="cache" @bind-Value="model.CacheMedia" />
<label class="form-check-label" for="cache">Enable Media Caching</label><br />
<small class="form-text text-body-secondary">If enabled, the browser will cache the media file</small>
<small class="form-text text-body-secondary">
If enabled, the "Cache-Control" header will be set automatically.<br/>
The value is set to one week. If disabled, no header will be set.
</small>
</div>
</EditForm>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" @onclick="OnAbort">Abort</button>
<button type="button" class="btn btn-primary" @onclick="OnOk">OK</button>
<button id="abort" type="button" class="btn btn-secondary" @onclick="Abort">Abort</button>
<button type="button" class="btn btn-primary" @onclick="Ok">OK</button>
</div>
</div>
</div>
Expand Down Expand Up @@ -52,19 +55,19 @@
return await result.Task;
}

private void OnAbort()
private void Abort()
{
modalDisplay = "none";
modalClass = "";
modalClass = string.Empty;
showBackdrop = false;
result.SetResult(null);
StateHasChanged();
}

private void OnOk()
private void Ok()
{
modalDisplay = "none";
modalClass = "";
modalClass = string.Empty;
showBackdrop = false;
result.SetResult(model);
StateHasChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<string> UploadFileAsync(string fileName, Stream fileStream, Up
{
blobOptions.HttpHeaders = new BlobHttpHeaders
{
CacheControl = "public, max-age=31536000"
CacheControl = "public, max-age=604800"
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Threading.Tasks;
using LinkDotNet.Blog.Web.Features.Components;

namespace LinkDotNet.Blog.UnitTests.Web.Features.Components;

public class UploadFileModalDialogTests : BunitContext
{
[Fact]
public async Task ShouldReturnFilenameAndSettingWhenSubmitting()
{
var cut = Render<UploadFileModalDialog>();
var task = cut.InvokeAsync(() => cut.Instance.ShowAsync("Filename.png"));
cut.Find("#cache").Change(false);

await cut.Find("form").SubmitAsync();

var result = await task;
result.ShouldNotBeNull();
result.Name.ShouldBe("Filename.png");
result.CacheMedia.ShouldBeFalse();
}

[Fact]
public async Task ShouldReturnNullWhenAborted()
{
var cut = Render<UploadFileModalDialog>();
var task = cut.InvokeAsync(() => cut.Instance.ShowAsync("Filename.png"));

cut.Find("#abort").Click();

var result = await task;
result.ShouldBeNull();
}
}

0 comments on commit 980ffdf

Please sign in to comment.