Skip to content

Commit

Permalink
fix: fix async onOk function never gets called (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed Aug 9, 2022
1 parent 33bbe0a commit b17661d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
23 changes: 23 additions & 0 deletions src/__tests__/alert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/confirm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 2 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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';
}

0 comments on commit b17661d

Please sign in to comment.