-
Notifications
You must be signed in to change notification settings - Fork 25
feat: pass pluginProps to wrapper
#118
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| import React from 'react'; | ||
| import '@testing-library/jest-dom'; | ||
| import classNames from 'classnames'; | ||
| import { render, fireEvent } from '@testing-library/react'; | ||
| import { render, fireEvent, within } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { logError } from '@edx/frontend-platform/logging'; | ||
| import { IntlProvider } from '@edx/frontend-platform/i18n'; | ||
|
|
@@ -78,14 +78,15 @@ function DefaultContents({ className, onClick, ...rest }) { | |
| ); | ||
| } | ||
|
|
||
| function PluginSlotWrapper({ slotOptions, children }) { | ||
| function PluginSlotWrapper({ slotOptions, children, pluginProps }) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this file, please create a new set of tests for when there are no pluginProps specified.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done: 15bb3db |
||
| return ( | ||
| <IntlProvider locale="en"> | ||
| <PluginSlot | ||
| id="test-slot" | ||
| data-testid="test-slot-id" | ||
| as="div" | ||
| slotOptions={slotOptions} | ||
| pluginProps={pluginProps} | ||
| > | ||
| {children} | ||
| </PluginSlot> | ||
|
|
@@ -182,9 +183,39 @@ describe('PluginSlot', () => { | |
| { | ||
| op: PLUGIN_OPERATIONS.Wrap, | ||
| widgetId: 'default_contents', | ||
| wrapper: ({ component }) => ( | ||
| wrapper: ({ component, pluginProps }) => ( | ||
| <div data-testid="custom-wrapper"> | ||
| {component} | ||
| <div data-testid="custom-wrapper-props"> | ||
| {pluginProps?.prop1 && `This is a wrapper with ${pluginProps?.prop1}.`} | ||
| </div> | ||
| </div> | ||
| ), | ||
| }, | ||
| ], | ||
| keepDefault: true, | ||
| }); | ||
|
|
||
| const { getByTestId } = render(<TestPluginSlot pluginProps={{ prop1: 'prop1' }} />); | ||
| const customWrapper = getByTestId('custom-wrapper'); | ||
| const defaultContent = getByTestId('default_contents'); | ||
| expect(customWrapper).toContainElement(defaultContent); | ||
| const pluginProps = within(customWrapper).getByTestId('custom-wrapper-props'); | ||
| expect(pluginProps).toHaveTextContent('This is a wrapper with prop1.'); | ||
| }); | ||
|
|
||
| it('should wrap a Plugin when using the "wrap" operation without passing props', () => { | ||
| usePluginSlot.mockReturnValueOnce({ | ||
| plugins: [ | ||
| { | ||
| op: PLUGIN_OPERATIONS.Wrap, | ||
| widgetId: 'default_contents', | ||
| wrapper: ({ component, pluginProps }) => ( | ||
| <div data-testid="custom-wrapper"> | ||
| {component} | ||
| <div data-testid="custom-wrapper-no-props"> | ||
| {`This is a wrapper without props: ${JSON.stringify(pluginProps)}`} | ||
| </div> | ||
| </div> | ||
| ), | ||
| }, | ||
|
|
@@ -196,6 +227,8 @@ describe('PluginSlot', () => { | |
| const customWrapper = getByTestId('custom-wrapper'); | ||
| const defaultContent = getByTestId('default_contents'); | ||
| expect(customWrapper).toContainElement(defaultContent); | ||
| const pluginProps = within(customWrapper).getByTestId('custom-wrapper-no-props'); | ||
| expect(pluginProps).toHaveTextContent('This is a wrapper without props: {}'); | ||
| }); | ||
|
|
||
| it('should not render a widget if the Hide operation is applied to it', () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,10 +22,10 @@ const mockIsAdminWrapper = ({ widget }) => { | |
| return isAdmin ? widget : null; | ||
| }; | ||
|
|
||
| const makeMockElementWrapper = (testId = 0) => function MockElementWrapper({ component }) { | ||
| const makeMockElementWrapper = (testId = 0) => function MockElementWrapper({ component, pluginProps }) { | ||
| return ( | ||
| <div data-testid={`wrapper${testId}`}> | ||
| This is a wrapper. | ||
| {pluginProps?.prop1 && `This is a wrapper with ${pluginProps?.prop1}.`} | ||
| {component} | ||
| </div> | ||
| ); | ||
|
|
@@ -181,6 +181,18 @@ describe('organizePlugins', () => { | |
| describe('wrapComponent', () => { | ||
| describe('when provided with a single wrapper in an array', () => { | ||
| it('should wrap the provided component', () => { | ||
| const wrappedComponent = wrapComponent(mockRenderWidget, [makeMockElementWrapper()], { prop1: 'prop1' }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add a test where a component gets wrapped specifically without any pluginProps?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review @arbrandes! Done: 15bb3db |
||
|
|
||
| const { getByTestId } = render(wrappedComponent); | ||
|
|
||
| const wrapper = getByTestId('wrapper0'); | ||
| const widget = getByTestId('widget'); | ||
|
|
||
| expect(wrapper).toContainElement(widget); | ||
| expect(wrapper).toHaveTextContent('This is a wrapper with prop1.'); | ||
| }); | ||
|
|
||
| it('should wrap the provided component without passing props', () => { | ||
| const wrappedComponent = wrapComponent(mockRenderWidget, [makeMockElementWrapper()]); | ||
|
|
||
| const { getByTestId } = render(wrappedComponent); | ||
|
|
@@ -189,6 +201,7 @@ describe('wrapComponent', () => { | |
| const widget = getByTestId('widget'); | ||
|
|
||
| expect(wrapper).toContainElement(widget); | ||
| expect(wrapper).not.toHaveTextContent('This is a wrapper with prop1.'); | ||
| }); | ||
| }); | ||
| describe('when provided with multiple wrappers in an array', () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was wrong before the PR, but do you mind fixing it?
idxis not referenced at all by createElement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I got it..
The
idxis being used askeyhere:frontend-plugin-framework/src/plugins/data/utils.jsx
Line 91 in b81f409