Skip to content

Commit

Permalink
fix(dashboard-css): make to load saved css template (#19840)
Browse files Browse the repository at this point in the history
* fix(dashboard-css): make to load saved css template

* fix(dashboard-css): make to update state css with componentDidMount

* fix(dashobard-css): make to inject custom css after updateCss

* fix(dashboard-css): make to add RTL for custom css

* fix(dashboard-css): make to fix lint issue
  • Loading branch information
prosdev0107 authored Apr 27, 2022
1 parent f5e9f0e commit 4a835a4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(<HeaderActionsDropdown {...props} />);
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');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 4a835a4

Please sign in to comment.