IvanJosipovic / BlazorTable

Blazor Table Component with Sorting, Paging and Filtering
https://BlazorTable.netlify.app
MIT License
533 stars 110 forks source link

Unit test needs Logger #273

Open KevinBurton opened 3 years ago

KevinBurton commented 3 years ago

Describe the bug I am writing a unit test for a component but I get an exception' image I realize that I am using bUnit to render my components but since the exception is because BlazorTable cannot be constructed because of a missing Logger dependency I was wondering if you might have a workaround?

To Reproduce Please create a standalone Page which reproduces the issue and paste the code here in a code block. More Examples

@using BlazorTable
<h2>Projects</h2>
<Table TableItem="BreakpointProjectSummary" DataLoader="_loader" Items="data" PageSize="5" SelectionType="selectionType" RowClickAction="RowClick" SelectedItems="selectedItems" ShowSearchBar="false">
    <Column TableItem="BreakpointProjectSummary" Title="Project Id" Field="@(x => x.ProjectId)" Sortable="true" Width="10%" DefaultSortColumn="true" />
    <Column TableItem="BreakpointProjectSummary" Title="Project Name" Field="@(x => x.ProjectName)" Sortable="true" Width="10%" DefaultSortColumn="true" />
    <Column TableItem="BreakpointProjectSummary" Title="Company Name" Field="@(x => x.CompanyName)" Sortable="true" Width="10%" DefaultSortColumn="true" />
    <Column TableItem="BreakpointProjectSummary" Title="Clent Type" Field="@(x => x.ClientType)" Sortable="true" Width="10%" DefaultSortColumn="true" />
    <Pager ShowPageNumber="true" ShowTotalCount="true" />
</Table>

    public partial class ProjectPicker
    {
        [Inject]
        private IBreakpointManagementDataService dataService { get; set; }

        private IDataLoader<BreakpointProjectSummary> _loader;

        private IEnumerable<BreakpointProjectSummary> data;

        private BreakpointProjectSummary selected;

        private SelectionType selectionType = SelectionType.Single;

        private List<BreakpointProjectSummary> selectedItems = new List<BreakpointProjectSummary>();

        protected override async Task OnInitializedAsync()
        {
            _loader = new ProjectDataLoader(dataService);
            data = (await _loader.LoadDataAsync(null)).Records;
        }
        public void RowClick(BreakpointProjectSummary data)
        {
            selected = data;
            StateHasChanged();
        }
    }

    public class ProjectDataLoader : IDataLoader<BreakpointProjectSummary>
    {
        private readonly IBreakpointManagementDataService _dataService;
        public ProjectDataLoader(IBreakpointManagementDataService dataService)
        {
            _dataService = dataService;
        }
        public async Task<PaginationResult<BreakpointProjectSummary>> LoadDataAsync(FilterData parameters)
        {
            if (parameters == null) return new PaginationResult<BreakpointProjectSummary>();
            IList<BreakpointProjectSummary> results;
            if (parameters == null)
            {
                results = await _dataService.GetBreakpointProjects();
            }
            else if (parameters.Top == null)
            {
                results = await _dataService.GetBreakpointProjects();
            }
            else if (string.IsNullOrWhiteSpace(parameters.OrderBy))
            {
                results = await _dataService.GetBreakpointProjects(parameters.Top.Value, parameters.Skip.Value);
            }
            else
            {
                var order = parameters.OrderBy.Split(" ");
                if (order.Length >= 2)
                {
                    results = await _dataService.GetBreakpointProjects(parameters.Top.Value, parameters.Skip.Value, order[0]);
                }
                else
                {
                    results = await _dataService.GetBreakpointProjects(parameters.Top.Value, parameters.Skip.Value);
                }
            }
            var count = await _dataService.GetBreakpointProjectCount();
            return new PaginationResult<BreakpointProjectSummary>
            {
                Records = results,
                Skip = parameters?.Skip ?? 0,
                Total = int.Parse(count),
                Top = parameters?.Top ?? 0
            };
        }
    }

Expected behavior I would expect the unit test to pass. But at a bare minimum not through an exception when bUnit renders my component.

IvanJosipovic commented 3 years ago

Hey,

Can you give this a try? https://bunit.egilhansen.com/api/Bunit.Extensions.LoggerHelperExtensions.html If you get stuck, let me know where the code is and I can have a look.

Thank you!

KevinBurton commented 3 years ago

So this details a CreateLogger method but I am not sure how to tie this into my component which uses BlazorTable?

On Tue, Mar 9, 2021 at 11:09 AM Ivan Josipovic notifications@github.com wrote:

Hey,

Can you give this a try? https://bunit.egilhansen.com/api/Bunit.Extensions.LoggerHelperExtensions.html If you get stuck, let me know where the code is and I can have a look.

Thank you!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/IvanJosipovic/BlazorTable/issues/273#issuecomment-794175692, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAUFM3SGELS2UZXIGUZM3ATTCZB4LANCNFSM4Y4AU3RA .