diff --git a/superset-frontend/src/dashboard/components/Header/HeaderActionsDropdown/HeaderActionsDropdown.test.tsx b/superset-frontend/src/dashboard/components/Header/HeaderActionsDropdown/HeaderActionsDropdown.test.tsx index d1f87ec999..57fe7a1333 100644 --- a/superset-frontend/src/dashboard/components/Header/HeaderActionsDropdown/HeaderActionsDropdown.test.tsx +++ b/superset-frontend/src/dashboard/components/Header/HeaderActionsDropdown/HeaderActionsDropdown.test.tsx @@ -17,6 +17,8 @@ * under the License. */ import React from 'react'; +import { shallow } from 'enzyme'; +import sinon from 'sinon'; import { render, screen } from 'spec/helpers/testing-library'; import userEvent from '@testing-library/user-event'; import fetchMock from 'fetch-mock'; @@ -202,3 +204,29 @@ test('should show the properties modal', async () => { userEvent.click(screen.getByText('Edit dashboard properties')); expect(editModeOnProps.showPropertiesModal).toHaveBeenCalledTimes(1); }); + +describe('UNSAFE_componentWillReceiveProps', () => { + let wrapper: any; + const mockedProps = createProps(); + const props = { ...mockedProps, customCss: '' }; + + beforeEach(() => { + wrapper = shallow(); + wrapper.setState({ css: props.customCss }); + sinon.spy(wrapper.instance(), 'setState'); + }); + + afterEach(() => { + wrapper.instance().setState.restore(); + }); + + it('css should update state and inject custom css', () => { + wrapper.instance().UNSAFE_componentWillReceiveProps({ + ...props, + customCss: mockedProps.customCss, + }); + expect(wrapper.instance().setState.calledOnce).toBe(true); + const stateKeys = Object.keys(wrapper.instance().setState.lastCall.args[0]); + expect(stateKeys).toContain('css'); + }); +}); diff --git a/superset-frontend/src/dashboard/components/Header/HeaderActionsDropdown/index.jsx b/superset-frontend/src/dashboard/components/Header/HeaderActionsDropdown/index.jsx index 619e10ea22..45946ecc8f 100644 --- a/superset-frontend/src/dashboard/components/Header/HeaderActionsDropdown/index.jsx +++ b/superset-frontend/src/dashboard/components/Header/HeaderActionsDropdown/index.jsx @@ -136,10 +136,15 @@ class HeaderActionsDropdown extends React.PureComponent { }); } + UNSAFE_componentWillReceiveProps(nextProps) { + if (this.props.customCss !== nextProps.customCss) { + this.setState({ css: nextProps.customCss }, () => { + injectCustomCss(nextProps.customCss); + }); + } + } + changeCss(css) { - this.setState({ css }, () => { - injectCustomCss(css); - }); this.props.onChange(); this.props.updateCss(css); }