From 6905d4e2a014fc819bc698652c9059779d5cb57e Mon Sep 17 00:00:00 2001 From: Daniel Chalmers Date: Mon, 1 Jul 2024 16:08:09 -0500 Subject: [PATCH] Edit Category Tests --- JournalApp.Tests/EditCategoryTests.razor | 54 -------- JournalApp.Tests/ManageCategoriesTests.razor | 120 +++++++++++++++++- .../Components/EditCategoryDialog.razor | 8 +- JournalApp/Pages/ManageCategoriesPage.razor | 2 +- 4 files changed, 118 insertions(+), 66 deletions(-) delete mode 100644 JournalApp.Tests/EditCategoryTests.razor diff --git a/JournalApp.Tests/EditCategoryTests.razor b/JournalApp.Tests/EditCategoryTests.razor deleted file mode 100644 index 30336bb..0000000 --- a/JournalApp.Tests/EditCategoryTests.razor +++ /dev/null @@ -1,54 +0,0 @@ -@namespace JournalApp.Tests -@inherits JaTestContext - -@code { - public override async Task InitializeAsync() - { - await base.InitializeAsync(); - - AddDbContext(); - Services.GetService().SeedCategories(); - } - - [Fact(Skip = "Stub")] - public async Task EditNewCategory() { } - - [Fact(Skip = "Stub")] - public async Task EditNewMedication() { } - - [Fact(Skip = "Stub")] - public async Task EditExistingCategory() - { - var category = new DataPointCategory - { - Guid = Guid.NewGuid(), - Type = PointType.Note, - Name = "My note", - - }; - - var dialogService = Services.GetService(); - var cut = RenderComponent(); - - //var parameters = new DialogParameters { { x => x.Group, category.Group }, { x => x.Category, category } }; - //var result = await DialogService.Show(parameters).Result; - - IDialogReference dialogReference = null; - await cut.InvokeAsync(() => dialogReference = dialogService?.Show()); - } - - [Fact(Skip = "Stub")] - public async Task EditExistingMedication() { } - - [Fact(Skip = "Stub")] - public async Task DeleteCategoryFromDatabase() { } - - [Fact(Skip = "Stub")] - public async Task SaveButtonSaves() { } - - [Fact(Skip = "Stub")] - public async Task CancelButtonCancels() { } - - [Fact(Skip = "Stub")] - public async Task DeviceBackButtonSaves() { } -} diff --git a/JournalApp.Tests/ManageCategoriesTests.razor b/JournalApp.Tests/ManageCategoriesTests.razor index 3e07477..61f649d 100644 --- a/JournalApp.Tests/ManageCategoriesTests.razor +++ b/JournalApp.Tests/ManageCategoriesTests.razor @@ -10,14 +10,120 @@ Services.GetService().SeedCategories(); } - [Fact(Skip = "Stub")] - public async Task ClickCategoryOpensEditor() { } + [Fact] + public void EditDialog_Open() + { + var dbf = Services.GetService>(); + var guid = "0fb54aff-9ecc-4c17-bab5-b908b794cea9"; // Anxiety. - [Fact(Skip = "Stub")] - public async Task ClickCategoryOpensEditorAndUpdatesList() { } + var layout = Render( + @ + + + + + ); + + DataPointCategory Category() + { + using var db = dbf.CreateDbContext(); + return db.Categories.Single(c => c.Guid == new Guid(guid)); + } + + layout.Find($"#manage-category-{guid} > .manage-category-edit-button").Click(); + var editDialog = layout.FindComponent(); + + // Assert. + Category().Name.Should().Be("Anxiety"); + editDialog.FindAll(".mud-input-text input")[0].GetAttribute("value").Should().Be("Anxiety"); + + layout.Find(".submit-button").Click(); + } + + [Fact] + public void EditDialog_UpdatesList() + { + var dbf = Services.GetService>(); + var guid = "0fb54aff-9ecc-4c17-bab5-b908b794cea9"; // Anxiety. + + var layout = Render( + @ + + + + + ); + + DataPointCategory Category() + { + using var db = dbf.CreateDbContext(); + return db.Categories.Single(c => c.Guid == new Guid(guid)); + } + + layout.Find($"#manage-category-{guid} > .manage-category-edit-button").Click(); + + Category().Name.Should().Be("Anxiety"); + layout.FindAll(".category-dialog .mud-input-text input")[0].Input("New name"); + + // Submit changes and confirm they have changed on the main list. + layout.Find(".category-dialog .submit-button").Click(); + layout.Find($"#manage-category-{guid} > .manage-category-edit-button").TextContent.Should().Be("New name"); + } [Fact(Skip = "Stub")] - public async Task ClickPlusOpensEditorAndAddsNewCategoryToList() { } + public void EditDialog_Delete() { } + + [Fact] + public void AddNewCategory() + { + var layout = Render( + @ + + + + + ); + + // Initial state. + layout.FindAll(".manage-category-edit-button").Count.Should().Be(13); + + // Open the dialog to create a new category. + layout.Find(".add-category-button").Click(); + layout.Markup.Should().Contain("New category"); + + // Set up the new category. + layout.FindAll(".category-dialog .mud-input-text input")[0].Input("New name"); + + // Submit changes and confirm there is a new category in the list. + layout.Find(".category-dialog .submit-button").Click(); + layout.FindAll(".manage-category-edit-button").Count.Should().Be(14); + } + + [Fact] + public void AddNewMedication() + { + var layout = Render( + @ + + + + + ); + + // Initial state. + layout.FindAll(".manage-category-edit-button").Count.Should().Be(6); + + // Open the dialog to create a new category. + layout.Find(".add-category-button").Click(); + layout.Markup.Should().Contain("New medication"); + + // Set up the new category. + layout.FindAll(".category-dialog .mud-input-text input")[0].Input("New name"); + + // Submit changes and confirm there is a new category in the list. + layout.Find(".category-dialog .submit-button").Click(); + layout.FindAll(".manage-category-edit-button").Count.Should().Be(7); + } [Fact] public void ReadOnlyCannotBeEdited() @@ -73,7 +179,7 @@ cut.Find($"#manage-category-{guid} > .manage-category-up-button").HasAttribute("disabled").Should().BeTrue(); } - [Fact(Skip = "Doesn't like the null group being passed")] + [Fact(Skip = "TBD")] [Description("Checks if the number of categories in the database per group match the number rendered")] public void ShowsCorrectCategoriesInGroup() { @@ -86,7 +192,7 @@ { var cut = Render(@); - cut.FindAll(".manage-category").Count.Should().Be(x.Count); + cut.FindAll(".manage-category-edit-button").Count.Should().Be(x.Count); } } } diff --git a/JournalApp/Components/EditCategoryDialog.razor b/JournalApp/Components/EditCategoryDialog.razor index d0fb8af..428e5f2 100644 --- a/JournalApp/Components/EditCategoryDialog.razor +++ b/JournalApp/Components/EditCategoryDialog.razor @@ -3,10 +3,10 @@ @inject IDialogService DialogService @inject KeyEventService KeyEventService - +
- + @(Category == null ? "New" : "Edit") @(Group == "Medications" ? "medication" : "category") @@ -14,10 +14,10 @@ @if (Category != null) { - + } - +
diff --git a/JournalApp/Pages/ManageCategoriesPage.razor b/JournalApp/Pages/ManageCategoriesPage.razor index 980a3a8..36062f9 100644 --- a/JournalApp/Pages/ManageCategoriesPage.razor +++ b/JournalApp/Pages/ManageCategoriesPage.razor @@ -13,7 +13,7 @@ - +