diff --git a/src/__tests__/alert.test.js b/src/__tests__/alert.test.js index 797ff86..c4355b9 100644 --- a/src/__tests__/alert.test.js +++ b/src/__tests__/alert.test.js @@ -55,6 +55,29 @@ describe('triggers callbacks', () => { expect(onOk).toHaveBeenCalled(); }); + it('works with async onOk function', async () => { + const asyncOnOk = jest.fn( + () => + new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 1000); + }) + ); + const promise = alert('Message', { + async onOk() { + await asyncOnOk(); + }, + }); + + const okButton = screen.getByRole('button', { name: '确定' }); + userEvent.click(okButton); + + expect(asyncOnOk).toHaveBeenCalled(); + + await act(() => promise); + }); + describe('waits for async onOk', () => { let promise; beforeEach(async () => { diff --git a/src/__tests__/confirm.test.js b/src/__tests__/confirm.test.js index 259814b..fc90e71 100644 --- a/src/__tests__/confirm.test.js +++ b/src/__tests__/confirm.test.js @@ -97,6 +97,29 @@ describe('triggers callbacks', () => { expect(onCancel).toHaveBeenCalled(); }); + it('works with async onOk function', async () => { + const asyncOnOk = jest.fn( + () => + new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 1000); + }) + ); + const promise = confirm('Message', { + async onOk() { + await asyncOnOk(); + }, + }); + + const okButton = screen.getByRole('button', { name: '确定' }); + userEvent.click(okButton); + + expect(asyncOnOk).toHaveBeenCalled(); + + await act(() => promise); + }); + describe('waits for async onOk', () => { it('shows loading on ok button', async () => { const asyncOnOk = jest.fn( diff --git a/src/__tests__/prompt.test.js b/src/__tests__/prompt.test.js index d7b3181..ed7a5f5 100644 --- a/src/__tests__/prompt.test.js +++ b/src/__tests__/prompt.test.js @@ -139,6 +139,29 @@ describe('triggers callbacks', () => { expect(onCancel).toHaveBeenCalled(); }); + it('works with async onOk function', async () => { + const asyncOnOk = jest.fn( + () => + new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, 1000); + }) + ); + const promise = prompt('Message', '', { + async onOk() { + await asyncOnOk(); + }, + }); + + const okButton = screen.getByRole('button', { name: '确定' }); + userEvent.click(okButton); + + expect(asyncOnOk).toHaveBeenCalled(); + + await act(() => promise); + }); + describe('waits for async onOk', () => { it('shows loading on ok button', async () => { const asyncOnOk = jest.fn( diff --git a/src/utils.js b/src/utils.js index 48ecfb3..a95f3a5 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,8 +1,3 @@ -/** - * https://stackoverflow.com/a/7356528/8106429 - */ -export function isFunction(functionToCheck) { - return ( - functionToCheck && {}.toString.call(functionToCheck) === '[object Function]' - ); +export function isFunction(value) { + return typeof value === 'function'; }