@@ -3,6 +3,7 @@ import { render, screen, fireEvent, waitFor, userEvent } from '@testing-library/
33import { Provider } from 'react-redux' ;
44
55import { MessageComposerContainer } from './MessageComposerContainer' ;
6+ import { calculateLength , getSeparator } from './components/ComposerInput' ;
67import { setPermissions } from '../../actions/permissions' ;
78import { addSettings } from '../../actions/settings' ;
89import { selectServerRequest } from '../../actions/server' ;
@@ -860,3 +861,100 @@ describe('MessageComposer', () => {
860861 } ) ;
861862 } ) ;
862863} ) ;
864+
865+ describe ( 'calculateLength' , ( ) => {
866+ describe ( 'non-code-block (inline markdown)' , ( ) => {
867+ test ( 'empty text - no separator needed' , ( ) => {
868+ const result = calculateLength ( '' , '*' , false , '' ) ;
869+ expect ( result ) . toBe ( 1 ) ;
870+ } ) ;
871+
872+ test ( 'text without trailing space, no selection - separator is empty, cursor between delimiters' , ( ) => {
873+ const separator = getSeparator ( 'hello' , false , false ) ;
874+ expect ( separator ) . toBe ( '' ) ;
875+ const result = calculateLength ( 'hello' , '*' , false , separator ) ;
876+ expect ( result ) . toBe ( 1 ) ;
877+ } ) ;
878+
879+ test ( 'text with trailing space, no selection - separator is empty' , ( ) => {
880+ const separator = getSeparator ( 'hello ' , false , false ) ;
881+ expect ( separator ) . toBe ( '' ) ;
882+ const result = calculateLength ( 'hello ' , '*' , false , separator ) ;
883+ expect ( result ) . toBe ( 1 ) ;
884+ } ) ;
885+
886+ test ( 'text without trailing space, with selection - separator is space' , ( ) => {
887+ const separator = getSeparator ( 'hello' , false , true ) ;
888+ expect ( separator ) . toBe ( ' ' ) ;
889+ const result = calculateLength ( 'hello' , '*' , false , separator ) ;
890+ expect ( result ) . toBe ( 2 ) ;
891+ } ) ;
892+
893+ test ( 'text with trailing space, with selection - separator is empty' , ( ) => {
894+ const separator = getSeparator ( 'hello ' , false , true ) ;
895+ expect ( separator ) . toBe ( '' ) ;
896+ const result = calculateLength ( 'hello ' , '*' , false , separator ) ;
897+ expect ( result ) . toBe ( 1 ) ;
898+ } ) ;
899+ } ) ;
900+
901+ describe ( 'code-block' , ( ) => {
902+ test ( 'empty text - no separator needed' , ( ) => {
903+ const result = calculateLength ( '' , '```' , true , '' ) ;
904+ expect ( result ) . toBe ( 4 ) ;
905+ } ) ;
906+
907+ test ( 'text without trailing backticks - separator is newline' , ( ) => {
908+ const separator = getSeparator ( 'some code' , true , false ) ;
909+ expect ( separator ) . toBe ( '\n' ) ;
910+ const result = calculateLength ( 'some code' , '```' , true , separator ) ;
911+ expect ( result ) . toBe ( 5 ) ;
912+ } ) ;
913+
914+ test ( 'text ending with backticks - separator is empty' , ( ) => {
915+ const separator = getSeparator ( 'code ```' , true , false ) ;
916+ expect ( separator ) . toBe ( '' ) ;
917+ const result = calculateLength ( 'code ```' , '```' , true , separator ) ;
918+ expect ( result ) . toBe ( 4 ) ;
919+ } ) ;
920+ } ) ;
921+ } ) ;
922+
923+ describe ( 'getSeparator' , ( ) => {
924+ describe ( 'non-code-block' , ( ) => {
925+ test ( 'empty text returns empty string' , ( ) => {
926+ expect ( getSeparator ( '' , false , false ) ) . toBe ( '' ) ;
927+ expect ( getSeparator ( '' , false , true ) ) . toBe ( '' ) ;
928+ } ) ;
929+
930+ test ( 'no selection returns empty string' , ( ) => {
931+ expect ( getSeparator ( 'hello' , false , false ) ) . toBe ( '' ) ;
932+ } ) ;
933+
934+ test ( 'with selection, no trailing space returns space' , ( ) => {
935+ expect ( getSeparator ( 'hello' , false , true ) ) . toBe ( ' ' ) ;
936+ } ) ;
937+
938+ test ( 'with selection, trailing space returns empty string' , ( ) => {
939+ expect ( getSeparator ( 'hello ' , false , true ) ) . toBe ( '' ) ;
940+ } ) ;
941+ } ) ;
942+
943+ describe ( 'code-block' , ( ) => {
944+ test ( 'empty text returns empty string' , ( ) => {
945+ expect ( getSeparator ( '' , true , false ) ) . toBe ( '' ) ;
946+ } ) ;
947+
948+ test ( 'text not ending with backticks returns newline' , ( ) => {
949+ expect ( getSeparator ( 'some code' , true , false ) ) . toBe ( '\n' ) ;
950+ } ) ;
951+
952+ test ( 'text ending with ``` returns empty string' , ( ) => {
953+ expect ( getSeparator ( 'code ```' , true , false ) ) . toBe ( '' ) ;
954+ } ) ;
955+
956+ test ( 'text ending with ``` and spaces returns empty string' , ( ) => {
957+ expect ( getSeparator ( 'code ``` ' , true , false ) ) . toBe ( '' ) ;
958+ } ) ;
959+ } ) ;
960+ } ) ;
0 commit comments