Skip to content

Commit

Permalink
Fix the title and description not being updated when props change
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbarbara committed Oct 3, 2023
1 parent 9158b87 commit 61891ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ReactInlineSVG extends React.PureComponent<Props, State> {
}

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 */
Expand All @@ -88,6 +88,10 @@ class ReactInlineSVG extends React.PureComponent<Props, State> {

this.load();
}

if (previousProps.title !== title || previousProps.description !== description) {
this.getElement();
}
}

public componentWillUnmount(): void {
Expand Down
1 change: 0 additions & 1 deletion test/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ exports[`react-inlinesvg basic functionality should render a svg string src 1`]
<svg
height="24px"
preserveAspectRatio="xMidYMid"
version="1.1"
viewBox="0 0 24 24"
width="24px"
xmlns="http://www.w3.org/2000/svg"
Expand Down
28 changes: 27 additions & 1 deletion test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const fixtures = {
'data:image/svg+xml,%3Csvg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1098px" height="768px" viewBox="-0.5 -0.5 1098 768"%3E%3Cdefs /%3E%3Cg%3E%3Ca xlink:href="/dashboard/dash-l1MVuUCUAQ67LmpaHyH6"%3E%3Cimage x="428.5" y="617.5" width="86.25" height="80" xlink:href="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLj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Zm9ybWF0PgogICAgICAgIDxkYzp0eXBlCiAgICAgICAgICAgcmRmOnJlc291cmNlPSJodHRwOi8vcHVybC5vcmcvZGMvZGNtaXR5cGUvU3RpbGxJbWFnZSIgLz4KICAgICAgICA8ZGM6dGl0bGU+PC9kYzp0aXRsZT4KICAgICAgPC9jYzpXb3JrPgogICAgPC9yZGY6UkRGPgogIDwvbWV0YWRhdGE+Cjwvc3ZnPgo=" /%3E%3C/a%3E%3C/g%3E%3C/svg%3E%0A',
html: 'data:image/svg+xml,%3Chtml%20lang%3D%22en%22%3E%3Cbody%3EText%3C%2Fbody%3E%3C%2Fhtml%3E',
string:
'<svg width="24px" height="24px" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid"><g> <polygon fill="#000000" points="7 5 7 19 18 12"></polygon></g></svg>',
'<svg width="24px" height="24px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid"><g> <polygon fill="#000000" points="7 5 7 19 18 12"></polygon></g></svg>',
} as const;

const mockOnError = jest.fn();
Expand Down Expand Up @@ -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(
<ReactInlineSVG
description="A view library"
loader={<Loader />}
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', () => {
Expand Down

0 comments on commit 61891ae

Please sign in to comment.