From c2e4d76134a1b1d1f14c3f0b692f4d1306f609c7 Mon Sep 17 00:00:00 2001 From: Matthew Francis Brunetti Date: Tue, 20 Apr 2021 05:26:43 -0400 Subject: [PATCH] fix: passing react elements via .mixin (#153) * test: Demonstrate issue #152 * pass mixinParams to _main Co-authored-by: Limon Monte --- src/index.js | 6 ++-- test/integration.test.js | 75 +++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/index.js b/src/index.js index c9dcaea..cfe4825 100644 --- a/src/index.js +++ b/src/index.js @@ -21,8 +21,8 @@ export default function withReactContent (ParentSwal) { } } - _main (params) { - params = Object.assign({}, params) + _main (params, mixinParams) { + params = Object.assign({}, mixinParams, params) mounts.forEach(({ key, getter }) => { if (React.isValidElement(params[key])) { @@ -50,7 +50,7 @@ export default function withReactContent (ParentSwal) { } }) - return super._main(params) + return super._main(params, mixinParams) } update () { diff --git a/test/integration.test.js b/test/integration.test.js index 0e1af1f..fb9ac86 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -15,39 +15,50 @@ describe('integration', () => { // jest doesn't implement `window.scrollTo` so we need to mock it window.scrollTo = () => {} // eslint-disable-line @typescript-eslint/no-empty-function }) - it('renders React elements for each supported option', async () => { - await cleanSwalState() - const MySwal = withReactContent(SwalWithoutAnimation) - await MySwal.fire({ - title: title, - html: html, - icon: 'success', - iconHtml: @, - confirmButtonText: confirmButtonText, - denyButtonText: denyButtonText, - cancelButtonText: cancelButtonText, - closeButtonHtml: closeButtonHtml, - footer: footer, - didOpen: () => { - expect(MySwal.getTitle().innerHTML).toEqual('title') - expect(MySwal.getHtmlContainer().innerHTML).toEqual('html') - expect(MySwal.getConfirmButton().innerHTML).toEqual( - 'confirmButtonText', - ) - expect(MySwal.getDenyButton().innerHTML).toEqual( - 'denyButtonText', - ) - expect(MySwal.getCancelButton().innerHTML).toEqual( - 'cancelButtonText', - ) - expect(MySwal.getIcon().innerHTML).toEqual( - '
@
', - ) - expect(MySwal.getFooter().innerHTML).toEqual('footer') - expect(MySwal.getCloseButton().innerHTML).toEqual('closeButtonHtml') - MySwal.clickConfirm() - }, + describe('rendering React elements for each supported option', () => { + beforeEach(async () => cleanSwalState()) + it('works via .fire', async () => { + const MySwal = withReactContent(SwalWithoutAnimation) + await MySwal.fire({ + ...getReactOptions(), + didOpen: () => { + checkReactOptions(MySwal) + MySwal.clickConfirm() + }, + }) + }) + it('works via .mixin', async () => { + const MySwal = withReactContent(SwalWithoutAnimation) + const MyConfiguredSwal = MySwal.mixin({ ...getReactOptions() }) + await MyConfiguredSwal.fire({ + didOpen: () => { + checkReactOptions(MySwal) + MySwal.clickConfirm() + }, + }) }) + function getReactOptions () { + return { + title: title, + html: html, + iconHtml: @, + confirmButtonText: confirmButtonText, + denyButtonText: denyButtonText, + cancelButtonText: cancelButtonText, + closeButtonHtml: closeButtonHtml, + footer: footer, + } + } + function checkReactOptions (MySwal) { + expect(MySwal.getTitle().innerHTML).toEqual('title') + expect(MySwal.getHtmlContainer().innerHTML).toEqual('html') + expect(MySwal.getConfirmButton().innerHTML).toEqual('confirmButtonText') + expect(MySwal.getDenyButton().innerHTML).toEqual('denyButtonText') + expect(MySwal.getCancelButton().innerHTML).toEqual('cancelButtonText') + expect(MySwal.getIcon().innerHTML).toEqual('
@
') + expect(MySwal.getFooter().innerHTML).toEqual('footer') + expect(MySwal.getCloseButton().innerHTML).toEqual('closeButtonHtml') + } }) it('can mix React and non-React params', async () => { await cleanSwalState()