@@ -2,7 +2,7 @@ import {DiffAddedIcon} from '@primer/octicons-react'
22import { fireEvent , render as _render , waitFor , within } from '@testing-library/react'
33import userEvent from '@testing-library/user-event'
44import { UserEvent } from '@testing-library/user-event/dist/types/setup'
5- import React , { forwardRef , useLayoutEffect , useRef , useState } from 'react'
5+ import React , { forwardRef , useRef , useState } from 'react'
66import MarkdownEditor , { Emoji , MarkdownEditorHandle , MarkdownEditorProps , Mentionable , Reference , SavedReply } from '.'
77import ThemeProvider from '../../ThemeProvider'
88
@@ -21,17 +21,6 @@ const UncontrolledEditor = forwardRef<MarkdownEditorHandle, UncontrolledEditorPr
2121
2222 const onRenderPreview = async ( ) => 'Preview'
2323
24- useLayoutEffect ( ( ) => {
25- // combobox-nav attempts to filter out 'hidden' options by checking if the option has an
26- // offsetHeight or width > 0. In JSDom, all elements have offsetHeight = offsetWidth = 0,
27- // so we need to override at least one to make the class recognize that any options exist.
28- for ( const option of document . querySelectorAll ( '[role=option]' ) )
29- Object . defineProperty ( option , 'offsetHeight' , {
30- value : 1 ,
31- writable : true
32- } )
33- } )
34-
3524 return (
3625 < ThemeProvider >
3726 < MarkdownEditor
@@ -117,6 +106,20 @@ const render = async (ui: React.ReactElement) => {
117106}
118107
119108describe ( 'MarkdownEditor' , ( ) => {
109+ // combobox-nav attempts to filter out 'hidden' options by checking if the option has an
110+ // offsetHeight or width > 0. In JSDom, all elements have offsetHeight = offsetWidth = 0,
111+ // so we need to override at least one to make the class recognize that any options exist.
112+ const originalOffsetHeight = Object . getOwnPropertyDescriptor ( HTMLElement . prototype , 'offsetHeight' )
113+ beforeAll ( ( ) => {
114+ Object . defineProperty ( HTMLElement . prototype , 'offsetHeight' , {
115+ configurable : true ,
116+ value : 10
117+ } )
118+ } )
119+ afterAll ( ( ) => {
120+ if ( originalOffsetHeight ) Object . defineProperty ( HTMLElement . prototype , 'offsetHeight' , originalOffsetHeight )
121+ } )
122+
120123 beforeEach ( ( ) => {
121124 jest . mock ( '@primer/behaviors/utils' , ( ) => ( {
122125 // for all tests, default to Non-Mac (Ctrl) keybindings
@@ -832,8 +835,6 @@ describe('MarkdownEditor', () => {
832835 } )
833836
834837 describe ( 'suggestions' , ( ) => {
835- // we don't test filtering logic here because that's up to the consumer
836-
837838 const emojis : Emoji [ ] = [
838839 { name : '+1' , character : '👍' } ,
839840 { name : '-1' , character : '👎' } ,
@@ -847,7 +848,10 @@ describe('MarkdownEditor', () => {
847848 { identifier : 'github' , description : 'GitHub' } ,
848849 { identifier : 'primer' , description : 'Primer' } ,
849850 { identifier : 'actions' , description : 'Actions' } ,
850- { identifier : 'primer-css' , description : '' }
851+ { identifier : 'primer-css' , description : '' } ,
852+ { identifier : 'mnl' , description : '' } ,
853+ { identifier : 'gth' , description : '' } ,
854+ { identifier : 'mla' , description : '' }
851855 ]
852856
853857 const references : Reference [ ] = [
@@ -982,6 +986,20 @@ describe('MarkdownEditor', () => {
982986 } )
983987 } )
984988
989+ it ( 'applies suggestion and hides list on %s-press' , async ( ) => {
990+ const { queryForSuggestionsList, getAllSuggestions, getInput, user} = await render ( < EditorWithSuggestions /> )
991+
992+ const input = getInput ( )
993+ await user . type ( input , `hello :` )
994+ expect ( queryForSuggestionsList ( ) ) . toBeInTheDocument ( )
995+
996+ await waitFor ( ( ) => expect ( getAllSuggestions ( ) [ 0 ] ) . toHaveAttribute ( 'data-combobox-option-default' ) )
997+
998+ await user . keyboard ( `{Enter}` )
999+ expect ( input . value ) . toBe ( `hello 👍 ` ) // suggestions are inserted with a following space
1000+ expect ( queryForSuggestionsList ( ) ) . not . toBeInTheDocument ( )
1001+ } )
1002+
9851003 it ( 'filters mention suggestions using fuzzy match against name' , async ( ) => {
9861004 const { getInput, getAllSuggestions, user} = await render ( < EditorWithSuggestions /> )
9871005 await user . type ( getInput ( ) , '@octct' )
@@ -991,9 +1009,9 @@ describe('MarkdownEditor', () => {
9911009
9921010 it ( 'filters mention suggestions using fuzzy match against ID' , async ( ) => {
9931011 const { getInput, getAllSuggestions, user} = await render ( < EditorWithSuggestions /> )
994- await user . type ( getInput ( ) , '@prmrcss ' )
1012+ await user . type ( getInput ( ) , '@git ' )
9951013 expect ( getAllSuggestions ( ) ) . toHaveLength ( 1 )
996- expect ( getAllSuggestions ( ) [ 0 ] ) . toHaveTextContent ( 'primer-css ' )
1014+ expect ( getAllSuggestions ( ) [ 0 ] ) . toHaveTextContent ( 'github ' )
9971015 } )
9981016
9991017 it ( 'filters reference suggestions using fuzzy match against name' , async ( ) => {
0 commit comments