Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toast.promise does not work with Jest #435

Open
ashkan117 opened this issue Jun 6, 2024 · 2 comments
Open

toast.promise does not work with Jest #435

ashkan117 opened this issue Jun 6, 2024 · 2 comments

Comments

@ashkan117
Copy link

Describe the feature / bug 📝:

I'm currently using sonner like the following

toast.promise(apiPromiseCall, {
  loading: 'Loading...',
  success: (data) => {
    return `${data.name} toast has been added`;
  },
  error: 'Error',
});

When testing I want to mock out this api call but when used in tandem with sonner this fails with the following error.

  TypeError: n is not a function

    >  98 |     toast.promise(
          |           ^
       99 |       mockedApiCall(...),
      100 |       {
      101 |         loading: 'Loading message,

A workaround I am using is to manually handle the states with the apiCall.then().catch() methods

Steps to reproduce the bug 🔁:

  1. Mock a promise function using jest.spyOn
  2. Trigger the event that calls the toast

I can try to add more details so let me know what else might be useful.

@ARKN-SOL
Copy link

I've stumbled upon this error in a different context. It's because sonner does an outdated check if a promise is a Promise.

This is the responsible line of code: src/state.ts#L124
A promise doesn't need to be an instance of Promise to be a promise (yeah ... I know). It only needs to have a .then() method. This Stackoverflow answer has a good summary.
TL;DR: Sonner should use Promise.resolve(foo) instead of testing for type.

@michalhonc
Copy link

Stumbled on this in the context of server actions.

Explicit one-off solution to what @ARKN-SOL suggested above

- toast.promise(apiPromiseCall, {
+ toast.promise(Promise.resolve(apiPromiseCall), {
  loading: 'Loading...',
  success: (data) => {
    return `${data.name} toast has been added`;
  },
  error: 'Error',
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants