Skip to content

Commit 47998ca

Browse files
author
Said Shah
committed
Updated tests according to PR comments
1 parent d8c59a0 commit 47998ca

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

src/atoms/buttons/button.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ButtonStyles } from "../constants/button-styles";
33
import { ButtonTypes } from "../constants/button-types";
44
import React, { forwardRef } from "react";
55
import { StringUtils } from "andculturecode-javascript-core";
6+
import { AccessibilityLabels } from "../constants/accessibility-labels";
67

78
// -----------------------------------------------------------------------------------------
89
// #region Interfaces
@@ -84,7 +85,9 @@ const Button: React.RefForwardingComponent<
8485
{children}
8586
{accessibleText != null && (
8687
// if
87-
<span className="sr-only">{accessibleText}</span>
88+
<span className={AccessibilityLabels.ScreenReadersOnlyClass}>
89+
{accessibleText}
90+
</span>
8891
)}
8992
</button>
9093
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export enum AccessibilityLabels {
2+
ScreenReadersOnlyClass = "sr-only",
3+
}

src/molecules/form-fields/input-form-field.test.tsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import React from "react";
22
import { render, fireEvent } from "@testing-library/react";
3-
import {
4-
InputFormField,
5-
InvalidInputFormValueClass,
6-
ShowLabelForScreenReadersOnlyClass,
7-
} from "./input-form-field";
3+
import { InputFormField, InputFormFieldInvalidClass } from "./input-form-field";
84
import faker from "faker";
5+
import { AccessibilityLabels } from "../../atoms/constants/accessibility-labels";
96

107
describe("InputFormField", () => {
118
test("when default props, renders input with label", () => {
@@ -23,22 +20,21 @@ describe("InputFormField", () => {
2320

2421
test("when errorsMessages prop set, renders with error messages", () => {
2522
// Arrange
23+
const errorMessages = [faker.random.words(), faker.random.words()];
2624
const label = faker.random.words();
27-
const firstErrorMessage = faker.random.words();
28-
const secondErrorMessage = faker.random.words();
2925

3026
// Act
3127
const { getByText } = render(
3228
<InputFormField
33-
errorMessages={[firstErrorMessage, secondErrorMessage]}
29+
errorMessages={errorMessages}
3430
label={label}
3531
onChange={() => {}}
3632
/>
3733
);
3834

3935
// Assert
40-
expect(getByText(firstErrorMessage)).not.toBeNil();
41-
expect(getByText(secondErrorMessage)).not.toBeNil();
36+
expect(getByText(errorMessages[0])).not.toBeNil();
37+
expect(getByText(errorMessages[1])).not.toBeNil();
4238
});
4339

4440
test("when errorsMessage prop set, renders with error message", () => {
@@ -59,7 +55,7 @@ describe("InputFormField", () => {
5955
expect(getByText(testErrorMessage)).not.toBeNil();
6056
});
6157

62-
test(`when isValid prop set to false, renders with ${InvalidInputFormValueClass} class name`, () => {
58+
test(`when isValid prop set to false, renders with ${InputFormFieldInvalidClass} class name`, () => {
6359
// Arrange
6460
const label = faker.random.words();
6561

@@ -68,14 +64,14 @@ describe("InputFormField", () => {
6864
<InputFormField isValid={false} label={label} onChange={() => {}} />
6965
);
7066
const result = container.getElementsByClassName(
71-
InvalidInputFormValueClass
67+
InputFormFieldInvalidClass
7268
);
7369

7470
// Assert
7571
expect(result).toHaveLength(1);
7672
});
7773

78-
test(`when isValid prop set to true, renders without ${InvalidInputFormValueClass} class name`, () => {
74+
test(`when isValid prop set to true, renders without ${InputFormFieldInvalidClass} class name`, () => {
7975
// Arrange
8076
const label = faker.random.words();
8177

@@ -84,7 +80,7 @@ describe("InputFormField", () => {
8480
<InputFormField isValid={true} label={label} onChange={() => {}} />
8581
);
8682
const result = container.getElementsByClassName(
87-
InvalidInputFormValueClass
83+
InputFormFieldInvalidClass
8884
);
8985

9086
// Assert
@@ -106,7 +102,7 @@ describe("InputFormField", () => {
106102
expect(htmlLabelTag[0].textContent).toContain(requiredText);
107103
});
108104

109-
test(`when showLabelForScreenReadersOnly prop set, renders with ${ShowLabelForScreenReadersOnlyClass} class name`, () => {
105+
test(`when showLabelForScreenReadersOnly prop set, renders with ${AccessibilityLabels.ScreenReadersOnlyClass} class name`, () => {
110106
// Arrange
111107
const label = faker.random.words();
112108

@@ -122,7 +118,7 @@ describe("InputFormField", () => {
122118

123119
// Assert
124120
expect(htmlLabelTag[0].classList).toContain(
125-
ShowLabelForScreenReadersOnlyClass
121+
AccessibilityLabels.ScreenReadersOnlyClass
126122
);
127123
});
128124

src/molecules/form-fields/input-form-field.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { forwardRef, Ref, RefObject } from "react";
22
import uuid from "uuid";
3+
import { AccessibilityLabels } from "../../atoms/constants/accessibility-labels";
34
import { CollectionUtils, StringUtils } from "andculturecode-javascript-core";
45
import { InputCharacterCount } from "../../atoms/forms/input-character-count";
56
import { InputTypes } from "../../atoms/constants/input-types";
@@ -10,8 +11,7 @@ import { InputProperties } from "../../atoms/interfaces/input-properties";
1011
// -----------------------------------------------------------------------------------------
1112

1213
const COMPONENT_CLASS = "c-form-field";
13-
export const InvalidInputFormValueClass = "-invalid";
14-
export const ShowLabelForScreenReadersOnlyClass = "sr-only";
14+
export const InputFormFieldInvalidClass = "-invalid";
1515

1616
// #endregion Constants
1717

@@ -66,14 +66,15 @@ const InputFormField: React.RefForwardingComponent<
6666
value,
6767
} = props;
6868

69-
const cssIsValid = isValid ? "" : InvalidInputFormValueClass;
69+
const cssIsValid = isValid ? "" : InputFormFieldInvalidClass;
7070
const fieldId = props.fieldId ?? uuid.v4();
7171

7272
return (
7373
<div className={`${COMPONENT_CLASS} ${cssIsValid}`}>
7474
<label htmlFor={fieldId}>
7575
{showLabelForScreenReadersOnly ? (
76-
<span className={ShowLabelForScreenReadersOnlyClass}>
76+
<span
77+
className={AccessibilityLabels.ScreenReadersOnlyClass}>
7778
{label}
7879
</span>
7980
) : (

0 commit comments

Comments
 (0)