Skip to content

Commit efa7804

Browse files
committed
add unit tests for getHelpText utility function
1 parent 04c20d7 commit efa7804

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { getHelpText } from './stream_name_form_row';
9+
10+
const prefix = 'logs.';
11+
12+
describe('getHelpText', () => {
13+
it('should return empty error help text when stream name is shorter than the prefix', () => {
14+
const streamNameWithEmptyChildName = 'logs.';
15+
const result = getHelpText(prefix, streamNameWithEmptyChildName, false);
16+
expect(result).toBe('Stream name must not be empty.');
17+
});
18+
19+
it('should return name too long error help text when stream name is longer than 200 characters', () => {
20+
const streamNameLongerThan200Chars =
21+
'logs.xwdaqmsegtkamcrofcfcomnlkkkrkqtlkbqizvjvtrbwereqygqaaxmodzccqipzpwymyowrtvljtxevczoohrbpgijilsdptszgssmrkpwhvkukkgiqhvmcuzygmolyyadbxwngbkqjkretmzhgntkjkhrmltgyurufizwlelvmaqtngwhwqhxpfsuxiivxspvtwfcem';
22+
const result = getHelpText(prefix, streamNameLongerThan200Chars, false);
23+
expect(result).toBe('Stream name cannot be longer than 200 characters.');
24+
});
25+
26+
it('should return undefined help text when input is valid', () => {
27+
const validStreamName = 'logs.multiplayer';
28+
const result = getHelpText(prefix, validStreamName, false);
29+
expect(result).toBeUndefined();
30+
});
31+
});

x-pack/platform/plugins/shared/streams_app/public/components/data_management/stream_detail_routing/stream_name_form_row.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ const MIN_NAME_LENGTH = 1;
3030
const MAX_NAME_LENGTH = 200;
3131
const PREFIX_MAX_VISIBLE_CHARACTERS = 25;
3232

33-
// TODO: write a unit test for this function
34-
const getHelpText = (prefix: string, value: string, readOnly: boolean): string | undefined => {
33+
export const getHelpText = (
34+
prefix: string,
35+
value: string,
36+
readOnly: boolean
37+
): string | undefined => {
3538
if (value.length <= prefix.length && !readOnly) {
3639
return i18n.translate('xpack.streams.streamDetailRouting.emptyNameErrorHelpText', {
3740
defaultMessage: 'Stream name must not be empty.',

0 commit comments

Comments
 (0)