11import React from 'react' ;
2- import { fireEvent , render , screen } from '@testing-library/react' ;
2+ import { render , screen } from '@testing-library/react' ;
33import userEvent from '@testing-library/user-event' ;
44import { NewTextComponent } from './new-text.component' ;
5+ import { act } from 'react-dom/test-utils' ;
56
67describe ( 'NewTextComponent unit tests' , ( ) => {
78 it ( 'should handle the entered text when clicking on the send button' , ( ) => {
@@ -23,6 +24,23 @@ describe('NewTextComponent unit tests', () => {
2324 expect ( handleAppendTrainerText ) . toHaveBeenCalledTimes ( 1 ) ;
2425 } ) ;
2526
27+ it ( 'should handle the entered text when pressing ctrl + enter' , async ( ) => {
28+ // Arrange
29+ const handleAppendTrainerText = jest . fn ( ) ;
30+
31+ // Act
32+ render (
33+ < NewTextComponent handleAppendTrainerText = { handleAppendTrainerText } />
34+ ) ;
35+
36+ const inputText = screen . getByRole ( 'textbox' ) ;
37+
38+ await act ( async ( ) => userEvent . type ( inputText , 'Test text{ctrl}{enter}{/ctrl}' ) ) ;
39+
40+ // Assert
41+ expect ( handleAppendTrainerText ) . toHaveBeenCalledTimes ( 1 ) ;
42+ } ) ;
43+
2644 it ( 'should not handle any text when there is not entered text and clicking on the send button' , ( ) => {
2745 // Arrange
2846 const handleAppendTrainerText = jest . fn ( ) ;
@@ -39,4 +57,21 @@ describe('NewTextComponent unit tests', () => {
3957 // Assert
4058 expect ( handleAppendTrainerText ) . not . toHaveBeenCalled ( ) ;
4159 } ) ;
60+
61+ it ( 'should not handle any text when there is not entered text and pressing ctrl + enter' , async ( ) => {
62+ // Arrange
63+ const handleAppendTrainerText = jest . fn ( ) ;
64+
65+ // Act
66+ render (
67+ < NewTextComponent handleAppendTrainerText = { handleAppendTrainerText } />
68+ ) ;
69+
70+ const inputText = screen . getByRole ( 'textbox' ) ;
71+
72+ await act ( async ( ) => userEvent . type ( inputText , '{ctrl}{enter}{/ctrl}' ) ) ;
73+
74+ // Assert
75+ expect ( handleAppendTrainerText ) . not . toHaveBeenCalled ( ) ;
76+ } ) ;
4277} ) ;
0 commit comments