From 61891aeba6a74be001bad7b11d1313b6d4d8ba8e Mon Sep 17 00:00:00 2001 From: Gil Barbara Date: Tue, 3 Oct 2023 11:54:32 -0300 Subject: [PATCH] Fix the title and description not being updated when props change --- src/index.tsx | 6 +++++- test/__snapshots__/index.spec.tsx.snap | 1 - test/index.spec.tsx | 28 +++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index be58808..384e68e 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -70,7 +70,7 @@ class ReactInlineSVG extends React.PureComponent { } const { isCached, status } = this.state; - const { onLoad, src } = this.props; + const { description, onLoad, src, title } = this.props; if (previousState.status !== STATUS.READY && status === STATUS.READY) { /* istanbul ignore else */ @@ -88,6 +88,10 @@ class ReactInlineSVG extends React.PureComponent { this.load(); } + + if (previousProps.title !== title || previousProps.description !== description) { + this.getElement(); + } } public componentWillUnmount(): void { diff --git a/test/__snapshots__/index.spec.tsx.snap b/test/__snapshots__/index.spec.tsx.snap index 66575bc..f88968e 100644 --- a/test/__snapshots__/index.spec.tsx.snap +++ b/test/__snapshots__/index.spec.tsx.snap @@ -343,7 +343,6 @@ exports[`react-inlinesvg basic functionality should render a svg string src 1`] ', + ' ', } as const; const mockOnError = jest.fn(); @@ -396,6 +396,32 @@ describe('react-inlinesvg', () => { expect(container.querySelector('svg')).toMatchSnapshot(); }); + + it('should update the title and description after the initial render', async () => { + const { container, rerender } = setup({ + src: fixtures.react, + }); + + await waitFor(() => { + expect(mockOnLoad).toHaveBeenCalledTimes(1); + }); + expect(container.querySelector('title')).toHaveTextContent('React'); + expect(container.querySelector('desc')).not.toBeInTheDocument(); + + rerender( + } + onError={mockOnError} + onLoad={mockOnLoad} + src={fixtures.react} + title="React Rocks" + />, + ); + + expect(container.querySelector('title')).toHaveTextContent('React Rocks'); + expect(container.querySelector('desc')).toHaveTextContent('A view library'); + }); }); describe('cached requests', () => {