From 6fbb3b9d53ac42460e8be4654741e8d5400e7a5e Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Wed, 12 Aug 2020 10:54:16 +0200 Subject: [PATCH] Remove replaceSpacesInTestId and normalize test id in createAttributesFroTesting --- scm-ui/ui-components/src/devBuild.test.ts | 85 +++++++++++++++++++ scm-ui/ui-components/src/devBuild.ts | 12 +-- scm-ui/ui-components/src/index.ts | 2 +- scm-ui/ui-components/src/layout/Footer.tsx | 6 +- .../ui-webapp/src/containers/ProfileInfo.tsx | 7 +- .../src/users/components/table/Details.tsx | 7 +- 6 files changed, 99 insertions(+), 20 deletions(-) create mode 100644 scm-ui/ui-components/src/devBuild.test.ts diff --git a/scm-ui/ui-components/src/devBuild.test.ts b/scm-ui/ui-components/src/devBuild.test.ts new file mode 100644 index 0000000000..6ca0373733 --- /dev/null +++ b/scm-ui/ui-components/src/devBuild.test.ts @@ -0,0 +1,85 @@ +/* + * MIT License + * + * Copyright (c) 2020-present Cloudogu GmbH and Contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import { createAttributesForTesting, isDevBuild } from "./devBuild"; + +describe("devbuild tests", () => { + let env: string | undefined; + + beforeAll(() => { + env = process.env.NODE_ENV; + }); + + afterAll(() => { + process.env.NODE_ENV = env; + }); + + describe("isDevBuild tests", () => { + it("should return true for development", () => { + process.env.NODE_ENV = "development"; + expect(isDevBuild()).toBe(true); + }); + + it("should return false for production", () => { + process.env.NODE_ENV = "production"; + expect(isDevBuild()).toBe(false); + }); + }); + + describe("createAttributesForTesting in non development mode", () => { + beforeAll(() => { + process.env.NODE_ENV = "production"; + }); + + it("should return undefined for non development", () => { + const attributes = createAttributesForTesting("123"); + expect(attributes).toBeUndefined(); + }); + }); + + describe("createAttributesForTesting in development mode", () => { + beforeAll(() => { + process.env.NODE_ENV = "development"; + }); + + it("should return undefined for non development", () => { + const attributes = createAttributesForTesting("123"); + expect(attributes).toBeDefined(); + }); + + it("should return undefined for undefined testid", () => { + const attributes = createAttributesForTesting(); + expect(attributes).toBeUndefined(); + }); + + it("should normalize testid testid", () => { + const attributes = createAttributesForTesting("heart of gold"); + if (attributes) { + expect(attributes["data-testid"]).toBe("heart-of-gold"); + } else { + throw new Error("attributes should be defined"); + } + }); + }); +}); diff --git a/scm-ui/ui-components/src/devBuild.ts b/scm-ui/ui-components/src/devBuild.ts index b7a5e08a62..b8d686b749 100644 --- a/scm-ui/ui-components/src/devBuild.ts +++ b/scm-ui/ui-components/src/devBuild.ts @@ -22,22 +22,18 @@ * SOFTWARE. */ -export const isDevBuild = () => (process.env.NODE_ENV === "development") +export const isDevBuild = () => process.env.NODE_ENV === "development"; export const createAttributesForTesting = (testId?: string) => { if (!testId || !isDevBuild()) { return undefined; } return { - "data-testid": testId - } + "data-testid": normalizeTestId(testId) + }; }; -export const replaceSpacesInTestId = (testId?: string) => { - if (!testId) { - return testId; - } - +const normalizeTestId = (testId: string) => { let id = testId; while (id.includes(" ")) { id = id.replace(" ", "-"); diff --git a/scm-ui/ui-components/src/index.ts b/scm-ui/ui-components/src/index.ts index d9dd5b4c26..b5670144e9 100644 --- a/scm-ui/ui-components/src/index.ts +++ b/scm-ui/ui-components/src/index.ts @@ -85,7 +85,7 @@ export { default as comparators } from "./comparators"; export { apiClient } from "./apiclient"; export * from "./errors"; -export { isDevBuild, createAttributesForTesting, replaceSpacesInTestId } from "./devBuild"; +export { isDevBuild, createAttributesForTesting } from "./devBuild"; export * from "./avatar"; export * from "./buttons"; diff --git a/scm-ui/ui-components/src/layout/Footer.tsx b/scm-ui/ui-components/src/layout/Footer.tsx index 1a0319f0ca..d3b6c4eb65 100644 --- a/scm-ui/ui-components/src/layout/Footer.tsx +++ b/scm-ui/ui-components/src/layout/Footer.tsx @@ -31,7 +31,7 @@ import styled from "styled-components"; import { EXTENSION_POINT } from "../avatar/Avatar"; import ExternalNavLink from "../navigation/ExternalNavLink"; import { useTranslation } from "react-i18next"; -import { createAttributesForTesting, replaceSpacesInTestId } from "../devBuild"; +import { createAttributesForTesting } from "../devBuild"; type Props = { me?: Me; @@ -47,7 +47,7 @@ type TitleWithIconsProps = { const TitleWithIcon: FC = ({ icon, title }) => { return ( <> - {title} + {title} ); }; @@ -69,7 +69,7 @@ const AvatarContainer = styled.span` `; const TitleWithAvatar: FC = ({ me }) => ( -
+
diff --git a/scm-ui/ui-webapp/src/containers/ProfileInfo.tsx b/scm-ui/ui-webapp/src/containers/ProfileInfo.tsx index 22828eef34..c4e8248650 100644 --- a/scm-ui/ui-webapp/src/containers/ProfileInfo.tsx +++ b/scm-ui/ui-webapp/src/containers/ProfileInfo.tsx @@ -28,8 +28,7 @@ import { AvatarImage, AvatarWrapper, MailLink, - createAttributesForTesting, - replaceSpacesInTestId + createAttributesForTesting } from "@scm-manager/ui-components"; type Props = WithTranslation & { @@ -53,11 +52,11 @@ class ProfileInfo extends React.Component { {t("profile.username")} - {me.name} + {me.name} {t("profile.displayName")} - {me.displayName} + {me.displayName} {t("profile.mail")} diff --git a/scm-ui/ui-webapp/src/users/components/table/Details.tsx b/scm-ui/ui-webapp/src/users/components/table/Details.tsx index eaf443c349..18df0cdf49 100644 --- a/scm-ui/ui-webapp/src/users/components/table/Details.tsx +++ b/scm-ui/ui-webapp/src/users/components/table/Details.tsx @@ -28,8 +28,7 @@ import { Checkbox, DateFromNow, MailLink, - createAttributesForTesting, - replaceSpacesInTestId + createAttributesForTesting } from "@scm-manager/ui-components"; type Props = WithTranslation & { @@ -44,11 +43,11 @@ class Details extends React.Component { {t("user.name")} - {user.name} + {user.name} {t("user.displayName")} - {user.displayName} + {user.displayName} {t("user.mail")}