-
Notifications
You must be signed in to change notification settings - Fork 22
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
#4994: Make links in Field descriptions clickable #4993
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (C) 2023 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import React from "react"; | ||
import { render } from "@testing-library/react"; | ||
import LinkifiedString from "./LinkifiedString"; | ||
|
||
describe("LinkifiedString", () => { | ||
test("linkifies string", async () => { | ||
const rendered = render( | ||
<LinkifiedString>https://example.com and more</LinkifiedString> | ||
); | ||
|
||
expect(rendered.asFragment()).toMatchInlineSnapshot(` | ||
<DocumentFragment> | ||
<span> | ||
<a | ||
href="https://example.com" | ||
target="_blank" | ||
> | ||
https://example.com | ||
</a> | ||
and more | ||
</span> | ||
</DocumentFragment> | ||
`); | ||
}); | ||
test("ignores string without links", async () => { | ||
const rendered = render( | ||
<LinkifiedString>Here’s regular text</LinkifiedString> | ||
); | ||
|
||
expect(rendered.asFragment()).toMatchInlineSnapshot(` | ||
<DocumentFragment> | ||
<span> | ||
Here’s regular text | ||
</span> | ||
</DocumentFragment> | ||
`); | ||
}); | ||
test("ignores empty string", async () => { | ||
const rendered = render(<LinkifiedString>{""}</LinkifiedString>); | ||
|
||
expect(rendered.asFragment()).toMatchInlineSnapshot(` | ||
<DocumentFragment> | ||
<span /> | ||
</DocumentFragment> | ||
`); | ||
}); | ||
test("ignores non-string children", async () => { | ||
const rendered = render( | ||
<LinkifiedString> | ||
Half<a href="https://example.com">linked</a> | ||
</LinkifiedString> | ||
); | ||
|
||
expect(rendered.asFragment()).toMatchInlineSnapshot(` | ||
<DocumentFragment> | ||
Half | ||
<a | ||
href="https://example.com" | ||
> | ||
linked | ||
</a> | ||
</DocumentFragment> | ||
`); | ||
}); | ||
test("ignores missing children", async () => { | ||
const rendered = render(<LinkifiedString />); | ||
|
||
expect(rendered.asFragment()).toMatchInlineSnapshot("<DocumentFragment />"); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2023 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import linkifyUrls from "linkify-urls"; | ||
import React from "react"; | ||
|
||
const LinkifiedString: React.FC = ({ children }) => { | ||
if (typeof children !== "string") { | ||
return <>{children}</>; | ||
} | ||
|
||
return ( | ||
<span | ||
dangerouslySetInnerHTML={{ | ||
__html: linkifyUrls(children, { | ||
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hopefully in the future: |
||
attributes: { | ||
target: "_blank", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No |
||
}, | ||
}), | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default LinkifiedString; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's sue regular snapshots:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of snapshots is that they're automated, use
jest --updateSnapshot
Are you talking about
.toMatchSnapshot
?The actual test file is indeed easier to scan, but then to see the result you have to find it in the external file. This is fine when there's a single test but it becomes hard to find which is which when there are multiple snapshots.
With inline snapshots the results is right here, it's like manual "expect x to exist" tests, except they're fully automated.
We're already using inline snapshots in many other tests, especially for smaller components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fregante
Pls disregard, I thought auto-update doesn't work for inline snapshots.
Yes.
Personally, I prefer to have tests in one file and results in another file, always, regardless of the component size, so everyone on the team has the same knowledge, understanding, and expectations for reading and authoring the snapshot tests.
Let's not get into a long discussion though, the PR has been approved, leaving the final decision to you as the PR author.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My two cents: I would prefer to use file-based snapshots for React components
Also, I've had problems with Jest failing to update the inline snapshots in files that use
import {type Foo} from "foo"
syntaxThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a temporary issue that has or will be fixed. I do agree however that sometimes they cause annoyances.
File-based snapshots are good to ensure large components don't change, but they make tests unreadable. There's no way for me to see what the expectations are by just looking at this file:
https://github.com/pixiebrix/pixiebrix-extension/blob/main/src/components/LinkifiedString.test.tsx
I don’t think inline snapshots are difficult to understand and are definitely easier to author than manually copy-pasting the results like in this case:
pixiebrix-extension/src/blocks/transformers/parseDate.test.ts
Lines 68 to 82 in 251b6d5
👆 this one was a prime example of when to use (automated) inline snapshots.