Skip to content
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

Expression renderer component: Prevent double update #86794

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/plugins/expressions/public/react_expression_renderer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ describe('ExpressionRenderer', () => {
instance.unmount();
});

it('should not update twice immediately after rendering', () => {
jest.useFakeTimers();

const refreshSubject = new Subject();
const loaderUpdate = jest.fn();

(ExpressionLoader as jest.Mock).mockImplementation(() => {
return {
render$: new Subject(),
data$: new Subject(),
loading$: new Subject(),
update: loaderUpdate,
destroy: jest.fn(),
};
});

const instance = mount(
<ReactExpressionRenderer reload$={refreshSubject} expression="" debounce={1000} />
);

act(() => {
jest.runAllTimers();
});

expect(loaderUpdate).toHaveBeenCalledTimes(1);

instance.unmount();
});

it('waits for debounce period on other loader option change if specified', () => {
jest.useFakeTimers();

Expand Down
5 changes: 5 additions & 0 deletions src/plugins/expressions/public/react_expression_renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ export const ReactExpressionRenderer = ({
);
const [debouncedExpression, setDebouncedExpression] = useState(expression);
const [waitingForDebounceToComplete, setDebouncePending] = useState(false);
const firstRender = useRef(true);
useShallowCompareEffect(() => {
if (firstRender.current) {
firstRender.current = false;
return;
}
if (debounce === undefined) {
return;
}
Expand Down