Skip to content

Commit

Permalink
Merge pull request #754 from vrk-kpa/fix/component-test-warnings
Browse files Browse the repository at this point in the history
[Fix] CharacterCounter component tests
  • Loading branch information
riitasointi authored Jul 20, 2023
2 parents 3dee01f + bc6ccf5 commit c746ea1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
15 changes: 12 additions & 3 deletions src/core/Form/TextInput/TextInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { act, render, fireEvent } from '@testing-library/react';
import { axeTest } from '../../../utils/test';

import { TextInput } from './TextInput';
Expand Down Expand Up @@ -306,6 +306,14 @@ describe('props', () => {
});

describe('Character counter', () => {
beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

it('should display correct character count', () => {
const { container, getByTestId } = render(
<TextInput
Expand Down Expand Up @@ -340,7 +348,6 @@ describe('Character counter', () => {
});

it('should have correct screen reader status text', () => {
jest.useFakeTimers();
const { container, getByTestId } = render(
<TextInput
labelText="label"
Expand Down Expand Up @@ -370,7 +377,9 @@ describe('Character counter', () => {
container.getElementsByClassName('fi-status-text')[0].firstChild,
).toHaveTextContent('');

jest.advanceTimersByTime(2000);
act(() => {
jest.advanceTimersByTime(2000);
});

expect(
container.getElementsByClassName('fi-status-text')[0].firstChild,
Expand Down
21 changes: 16 additions & 5 deletions src/core/Form/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ const BaseTextInput = (props: characterCounterProps & TextInputProps) => {
typeof setTimeout
> | null>(null);

const [isMounted, setIsMounted] = useState(false);

useEffect(() => {
setIsMounted(true);
return () => {
setIsMounted(false);
};
}, []);

const {
className,
labelText,
Expand Down Expand Up @@ -178,11 +187,13 @@ const BaseTextInput = (props: characterCounterProps & TextInputProps) => {
const charCountInInput = event.target.value.length;
setCharCount(charCountInInput);
const newTypingTimer = setTimeout(() => {
setCharacterCounterAriaText(
charCountInInput <= characterLimit
? ariaCharactersRemainingText(characterLimit - charCountInInput)
: ariaCharactersExceededText(charCountInInput - characterLimit),
);
if (isMounted) {
setCharacterCounterAriaText(
charCountInInput <= characterLimit
? ariaCharactersRemainingText(characterLimit - charCountInInput)
: ariaCharactersExceededText(charCountInInput - characterLimit),
);
}
}, 1500);
setTypingTimer(newTypingTimer);
}
Expand Down
16 changes: 13 additions & 3 deletions src/core/Form/Textarea/Textarea.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { render, fireEvent, act } from '@testing-library/react';
import { axeTest } from '../../../utils/test';

import { Textarea } from './Textarea';
Expand Down Expand Up @@ -290,6 +290,14 @@ describe('props', () => {
});

describe('Character counter', () => {
beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

it('should display character count', () => {
const { container, getByRole } = render(
<Textarea
Expand Down Expand Up @@ -324,7 +332,6 @@ describe('props', () => {
});

it('should have correct screen reader status text', () => {
jest.useFakeTimers();
const { container, getByTestId } = render(
<Textarea
labelText="label"
Expand All @@ -345,6 +352,7 @@ describe('props', () => {
).toHaveTextContent('');

const textInput = getByTestId('cc-textarea') as HTMLTextAreaElement;

fireEvent.change(textInput, {
target: { value: 'Lorem ipsum dolor sit amet' },
});
Expand All @@ -354,7 +362,9 @@ describe('props', () => {
container.getElementsByClassName('fi-status-text')[0].firstChild,
).toHaveTextContent('');

jest.advanceTimersByTime(2000);
act(() => {
jest.advanceTimersByTime(2000);
});

expect(
container.getElementsByClassName('fi-status-text')[0].firstChild,
Expand Down
21 changes: 16 additions & 5 deletions src/core/Form/Textarea/Textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ const BaseTextarea = (props: TextareaProps) => {
typeof setTimeout
> | null>(null);

const [isMounted, setIsMounted] = useState(false);

useEffect(() => {
setIsMounted(true);
return () => {
setIsMounted(false);
};
}, []);

const inputRef = useRef<HTMLTextAreaElement>(null);

const onClickProps = !!disabled ? {} : { onMouseDown: onClick };
Expand All @@ -180,11 +189,13 @@ const BaseTextarea = (props: TextareaProps) => {
const charCountInInput = event.target.value.length;
setCharCount(charCountInInput);
const newTypingTimer = setTimeout(() => {
setCharacterCounterAriaText(
charCountInInput <= characterLimit
? ariaCharactersRemainingText(characterLimit - charCountInInput)
: ariaCharactersExceededText(charCountInInput - characterLimit),
);
if (isMounted) {
setCharacterCounterAriaText(
charCountInInput <= characterLimit
? ariaCharactersRemainingText(characterLimit - charCountInInput)
: ariaCharactersExceededText(charCountInInput - characterLimit),
);
}
}, 1500);
setTypingTimer(newTypingTimer);
}
Expand Down

0 comments on commit c746ea1

Please sign in to comment.