Remove replaceSpacesInTestId and normalize test id in createAttributesFroTesting

This commit is contained in:
Sebastian Sdorra
2020-08-12 10:54:16 +02:00
parent 1dfb688f21
commit 6fbb3b9d53
6 changed files with 99 additions and 20 deletions

View File

@@ -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");
}
});
});
});

View File

@@ -22,22 +22,18 @@
* SOFTWARE. * SOFTWARE.
*/ */
export const isDevBuild = () => (process.env.NODE_ENV === "development") export const isDevBuild = () => process.env.NODE_ENV === "development";
export const createAttributesForTesting = (testId?: string) => { export const createAttributesForTesting = (testId?: string) => {
if (!testId || !isDevBuild()) { if (!testId || !isDevBuild()) {
return undefined; return undefined;
} }
return { return {
"data-testid": testId "data-testid": normalizeTestId(testId)
} };
}; };
export const replaceSpacesInTestId = (testId?: string) => { const normalizeTestId = (testId: string) => {
if (!testId) {
return testId;
}
let id = testId; let id = testId;
while (id.includes(" ")) { while (id.includes(" ")) {
id = id.replace(" ", "-"); id = id.replace(" ", "-");

View File

@@ -85,7 +85,7 @@ export { default as comparators } from "./comparators";
export { apiClient } from "./apiclient"; export { apiClient } from "./apiclient";
export * from "./errors"; export * from "./errors";
export { isDevBuild, createAttributesForTesting, replaceSpacesInTestId } from "./devBuild"; export { isDevBuild, createAttributesForTesting } from "./devBuild";
export * from "./avatar"; export * from "./avatar";
export * from "./buttons"; export * from "./buttons";

View File

@@ -31,7 +31,7 @@ import styled from "styled-components";
import { EXTENSION_POINT } from "../avatar/Avatar"; import { EXTENSION_POINT } from "../avatar/Avatar";
import ExternalNavLink from "../navigation/ExternalNavLink"; import ExternalNavLink from "../navigation/ExternalNavLink";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { createAttributesForTesting, replaceSpacesInTestId } from "../devBuild"; import { createAttributesForTesting } from "../devBuild";
type Props = { type Props = {
me?: Me; me?: Me;
@@ -47,7 +47,7 @@ type TitleWithIconsProps = {
const TitleWithIcon: FC<TitleWithIconsProps> = ({ icon, title }) => { const TitleWithIcon: FC<TitleWithIconsProps> = ({ icon, title }) => {
return ( return (
<> <>
<i className={`fas fa-${icon} fa-fw`} {...createAttributesForTesting(replaceSpacesInTestId(title))} /> {title} <i className={`fas fa-${icon} fa-fw`} {...createAttributesForTesting(title)} /> {title}
</> </>
); );
}; };
@@ -69,7 +69,7 @@ const AvatarContainer = styled.span`
`; `;
const TitleWithAvatar: FC<TitleWithAvatarProps> = ({ me }) => ( const TitleWithAvatar: FC<TitleWithAvatarProps> = ({ me }) => (
<div {...createAttributesForTesting(replaceSpacesInTestId(me.displayName))}> <div {...createAttributesForTesting(me.displayName)}>
<AvatarContainer className="image is-rounded"> <AvatarContainer className="image is-rounded">
<VCenteredAvatar person={me} representation="rounded" /> <VCenteredAvatar person={me} representation="rounded" />
</AvatarContainer> </AvatarContainer>

View File

@@ -28,8 +28,7 @@ import {
AvatarImage, AvatarImage,
AvatarWrapper, AvatarWrapper,
MailLink, MailLink,
createAttributesForTesting, createAttributesForTesting
replaceSpacesInTestId
} from "@scm-manager/ui-components"; } from "@scm-manager/ui-components";
type Props = WithTranslation & { type Props = WithTranslation & {
@@ -53,11 +52,11 @@ class ProfileInfo extends React.Component<Props> {
<tbody> <tbody>
<tr> <tr>
<th>{t("profile.username")}</th> <th>{t("profile.username")}</th>
<td {...createAttributesForTesting(replaceSpacesInTestId(me.name))}>{me.name}</td> <td {...createAttributesForTesting(me.name)}>{me.name}</td>
</tr> </tr>
<tr> <tr>
<th>{t("profile.displayName")}</th> <th>{t("profile.displayName")}</th>
<td {...createAttributesForTesting(replaceSpacesInTestId(me.displayName))}>{me.displayName}</td> <td {...createAttributesForTesting(me.displayName)}>{me.displayName}</td>
</tr> </tr>
<tr> <tr>
<th>{t("profile.mail")}</th> <th>{t("profile.mail")}</th>

View File

@@ -28,8 +28,7 @@ import {
Checkbox, Checkbox,
DateFromNow, DateFromNow,
MailLink, MailLink,
createAttributesForTesting, createAttributesForTesting
replaceSpacesInTestId
} from "@scm-manager/ui-components"; } from "@scm-manager/ui-components";
type Props = WithTranslation & { type Props = WithTranslation & {
@@ -44,11 +43,11 @@ class Details extends React.Component<Props> {
<tbody> <tbody>
<tr> <tr>
<th>{t("user.name")}</th> <th>{t("user.name")}</th>
<td {...createAttributesForTesting(replaceSpacesInTestId(user.name))}>{user.name}</td> <td {...createAttributesForTesting(user.name)}>{user.name}</td>
</tr> </tr>
<tr> <tr>
<th>{t("user.displayName")}</th> <th>{t("user.displayName")}</th>
<td {...createAttributesForTesting(replaceSpacesInTestId(user.displayName))}>{user.displayName}</td> <td {...createAttributesForTesting(user.displayName)}>{user.displayName}</td>
</tr> </tr>
<tr> <tr>
<th>{t("user.mail")}</th> <th>{t("user.mail")}</th>