Skip to content

Commit

Permalink
prevent double update
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Dec 22, 2020
1 parent ce546ad commit b7b73d4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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

0 comments on commit b7b73d4

Please sign in to comment.