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

#4994: Make links in Field descriptions clickable #4993

Merged
merged 4 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"^.+\\.txt$": "<rootDir>/src/testUtils/rawJestTransformer.mjs"
},
"transformIgnorePatterns": [
"node_modules/(?!@cfworker|escape-string-regex|filename-reserved-regex|filenamify|idb|webext-|p-timeout|p-retry|p-defer|p-memoize|serialize-error|strip-outer|trim-repeated|mimic-fn|urlpattern-polyfill|url-join|uuid|nanoid|use-debounce|copy-text-to-clipboard)"
"node_modules/(?!@cfworker|escape-string-regex|filename-reserved-regex|filenamify|idb|webext-|p-timeout|p-retry|p-defer|p-memoize|serialize-error|strip-outer|trim-repeated|mimic-fn|urlpattern-polyfill|url-join|uuid|nanoid|use-debounce|copy-text-to-clipboard|linkify-urls|create-html-element|stringify-attributes|escape-goat)"
],
"setupFiles": [
"dotenv/config",
Expand Down
129 changes: 126 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"json-stringify-safe": "^5.0.1",
"jsonpath-plus": "^7.0.0",
"kbar": "^0.1.0-beta.39",
"linkify-urls": "^4.0.0",
"lodash-es": "^4.17.21",
"marked": "^4.2.5",
"mustache": "^4.2.0",
Expand Down
87 changes: 87 additions & 0 deletions src/components/LinkifiedString.test.tsx
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(`
Copy link
Contributor

@BALEHOK BALEHOK Jan 10, 2023

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:

  1. that's consistent
  2. the tests are easier to grasp
  3. when you need to update the snapshots, you don't need to make the changes manually
  4. you can always explore the snapshot file to see what is the expected output

Copy link
Collaborator Author

@fregante fregante Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when you need to update the snapshots, you don't need to make the changes manually

The point of snapshots is that they're automated, use jest --updateSnapshot

the tests are easier to grasp
you can always explore the snapshot file to see what is the expected output

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.

that's consistent

We're already using inline snapshots in many other tests, especially for smaller components.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fregante

when you need to update the snapshots

Pls disregard, I thought auto-update doesn't work for inline snapshots.

Are you talking about .toMatchSnapshot?

Yes.

We're already using inline snapshots in many other tests, especially for smaller components.

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.

Copy link
Contributor

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" syntax

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've had problems with Jest failing to update the inline snapshots in files that use import {type Foo} from "foo" syntax

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

everyone on the team has the same knowledge, understanding, and expectations for reading and authoring the snapshot tests.

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:

expect(result).toEqual({
utc: {
iso8601: "2021-12-10T03:00:00.000Z",
date: "12/10/2021",
time: "3:00:00 AM",
humanReadable: "Fri, 10 Dec 2021 03:00:00 GMT",
epochMillis: 1_639_105_200_000,
},
local: {
iso8601: "2021-12-09T22:00:00.000-05:00",
date: "12/9/2021",
time: "10:00:00 PM",
humanReadable: "12/9/2021, 10:00:00 PM",
},
});

👆 this one was a prime example of when to use (automated) inline snapshots.

<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 />");
});
});
39 changes: 39 additions & 0 deletions src/components/LinkifiedString.tsx
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributes: {
target: "_blank",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
}),
}}
/>
);
};

export default LinkifiedString;
3 changes: 2 additions & 1 deletion src/components/form/FieldTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import cx from "classnames";
import { castArray, isPlainObject } from "lodash";
import AnnotationAlert from "@/components/annotationAlert/AnnotationAlert";
import { AnnotationType } from "@/analysis/analysisTypes";
import LinkifiedString from "../LinkifiedString";

export type FieldProps<As extends React.ElementType = React.ElementType> =
FormControlProps &
Expand Down Expand Up @@ -223,7 +224,7 @@ const FieldTemplate: React.FC<FieldProps> = ({
{formControl}
{description && (
<BootstrapForm.Text className="text-muted">
{description}
<LinkifiedString>{description}</LinkifiedString>
</BootstrapForm.Text>
)}
</Col>
Expand Down