From 62df72384fd38c6190fc8955d5c5562560fc5aa4 Mon Sep 17 00:00:00 2001 From: simpletron Date: Tue, 13 Jul 2021 12:31:55 +0700 Subject: [PATCH] Pass all listeners to render function in CFlex, Stack --- packages/chakra-ui-core/src/CFlex/CFlex.js | 3 +- .../src/CFlex/tests/CFlex.test.js | 63 ++++-- .../tests/__snapshots__/CFlex.test.js.snap | 24 +++ packages/chakra-ui-core/src/CStack/CStack.js | 3 +- .../src/CStack/tests/CStack.test.js | 146 ++++++++----- .../tests/__snapshots__/CStack.test.js.snap | 199 ++++++++++++++++++ 6 files changed, 366 insertions(+), 72 deletions(-) diff --git a/packages/chakra-ui-core/src/CFlex/CFlex.js b/packages/chakra-ui-core/src/CFlex/CFlex.js index d2e577a3..da6a0ea0 100644 --- a/packages/chakra-ui-core/src/CFlex/CFlex.js +++ b/packages/chakra-ui-core/src/CFlex/CFlex.js @@ -38,7 +38,8 @@ const CFlex = { render (h) { return h(this.as, { class: this.className, - attrs: this.computedAttrs + attrs: this.computedAttrs, + on: this.computedListeners }, this.$slots.default) } } diff --git a/packages/chakra-ui-core/src/CFlex/tests/CFlex.test.js b/packages/chakra-ui-core/src/CFlex/tests/CFlex.test.js index e5abcb5b..b5303c63 100644 --- a/packages/chakra-ui-core/src/CFlex/tests/CFlex.test.js +++ b/packages/chakra-ui-core/src/CFlex/tests/CFlex.test.js @@ -1,5 +1,5 @@ import CFlex from '..' -import { render, screen } from '@/tests/test-utils' +import { render, screen, userEvent } from '@/tests/test-utils' const renderComponent = (props) => { const inlineAttrs = (props && props.inlineAttrs) || '' @@ -11,21 +11,56 @@ const renderComponent = (props) => { return render(base) } -it('should render correctly', () => { - const { asFragment } = renderComponent() +describe('CFlex', () => { + it('should render correctly', () => { + const { asFragment } = renderComponent() - expect(asFragment()).toMatchSnapshot() -}) + expect(asFragment()).toMatchSnapshot() + }) + + it('should change styles', () => { + const inlineAttrs = 'align="center" justify="center" direction="column"' + const { asFragment } = renderComponent({ inlineAttrs }) + + expect(asFragment()).toMatchSnapshot() + + const flex = screen.getByTestId('flex') + expect(flex).toHaveStyle('display: flex') + expect(flex).toHaveStyle('align-items: center') + expect(flex).toHaveStyle('justify-content: center') + expect(flex).toHaveStyle('flex-direction: column') + }) + + it('should preserve inherited event listeners', async () => { + const handleClick = jest.fn() + renderComponent({ + inlineAttrs: '@click="handleClick"', + methods: { + handleClick + } + }) -it('should change styles', () => { - const inlineAttrs = 'align="center" justify="center" direction="column"' - const { asFragment } = renderComponent({ inlineAttrs }) + await userEvent.click(screen.getByTestId('flex')) + expect(handleClick).toHaveBeenCalledTimes(1) + }) - expect(asFragment()).toMatchSnapshot() + it('should preserve inherited event listeners with custom `as` component', async () => { + const handleSubmit = jest.fn((event) => { + expect(event.target).toBe(screen.getByTestId('flex-as-form')) + }) + renderComponent({ + template: ` + + +