Blazored / Toast

A JavaScript free toast library for Blazor and Razor Component applications
https://blazored.github.io/Toast/
MIT License
663 stars 92 forks source link

ShowToast return instance #210

Open Cvijo opened 1 year ago

Cvijo commented 1 year ago

@chrissainty this is the first draft of this feature. I have fixed some bugs with custom toasts (they did now follow MaxToastCount rule and queue logic) In code I have put my comments for some changes and will remove them when we agree on them, they all start with //todo remove comment after review `

I am not sure about toastInstance.Dispose(), can you check if that is fine with you, if yes i will have to implement those on ClearAll methods too.

Here is screen shoot.

https://user-images.githubusercontent.com/506853/221378816-38b096cf-12d6-4c4c-9968-f76e5d021c8b.mp4

Cvijo commented 1 year ago

@chrissainty I am not sure how to adjust unit testing (I never wrote any) on these new events that now return an instance. Do you know anyone who could help with that? I have tried something but i don't think it is the correct way, created a mock Instance and returned it.



    internal class MockToastInstance : IToastInstance
    {
        public Guid Id => Guid.NewGuid();

        public void Close() {}
    }


    [Fact]
    public void OnShowInvoked_When_ShowWarningCalled()
    {
        // arrange
        var instance = new MockToastInstance(); 
        var onShowCalled = false;

        _sut.OnShow += (_, _, _) => { onShowCalled = true; return instance;  };
        // act
        _sut.ShowWarning("message");

        // assert
        Assert.True(onShowCalled);
    }

chrissainty commented 1 year ago

I wouldn't mock anything. I'd render a real toast instance verify its there then call close on the instance and verify it's now gone. That way the test will be testing real stuff and no mocks

Cvijo commented 1 year ago

As i said i never do any kind of unit testing, now it fails because OnShow and every other event returns instance instead of void and i don't know how to change them

_sut.OnShow += (_, _, _) => onShowCalled = true;

OnShow has to return instance and to my zero knowledge on unit testing i could only make it work with code i posted. If you can share one sample of this test how should i change it i could do the rest

    [Fact]
    public void OnShowInvoked_When_ShowSuccessCalled()

And btw. I wanted you to check if this is even an option for this feature to return ToastInstance

chrissainty commented 1 year ago

No worries. I might be able to get a bit of time this evening to get on the laptop. I'll do my best to write up an example.

chrissainty commented 1 year ago

@Cvijo Sorry I've not been able to help with this for so long. I think the mock instance will do for now. I can rework the tests once we merge things if there are any major issues.