11import { getByRole } from '@testing-library/dom'
2+ import { userEvent } from '@testing-library/user-event'
23import { outdent } from 'outdent'
34
45import { components } from '#lib'
56
67import { CharacterCount , initCharacterCounts } from './character-count.mjs'
78import { examples } from './fixtures.mjs'
89
10+ const user = userEvent . setup ( )
11+
912describe ( 'Character count' , ( ) => {
1013 /** @type {HTMLElement } */
1114 let $root
@@ -94,6 +97,17 @@ describe('Character count', () => {
9497 } )
9598
9699 describe ( 'Initialisation via class' , ( ) => {
100+ /** @type {typeof Intl.Segmenter } */
101+ let Segmenter
102+
103+ beforeEach ( ( ) => {
104+ Segmenter = Intl . Segmenter
105+ } )
106+
107+ afterEach ( ( ) => {
108+ Object . assign ( Intl , { Segmenter } )
109+ } )
110+
97111 it ( 'should not throw with $root element' , ( ) => {
98112 expect ( ( ) => new CharacterCount ( $root ) ) . not . toThrow ( )
99113 } )
@@ -106,6 +120,19 @@ describe('Character count', () => {
106120 )
107121 } )
108122
123+ it ( 'should throw without Intl.Segmenter support' , ( ) => {
124+ // @ts -expect-error The operand of a 'delete' operator cannot be a read-only property
125+ delete Intl . Segmenter
126+
127+ expect ( ( ) => {
128+ new CharacterCount ( $root , {
129+ countType : 'characters'
130+ } )
131+ } ) . toThrow (
132+ `${ CharacterCount . moduleName } : Support for "Intl.Segmenter" required`
133+ )
134+ } )
135+
109136 it ( 'should throw with missing $root element' , ( ) => {
110137 // @ts -expect-error Parameter '$root' not provided
111138 expect ( ( ) => new CharacterCount ( ) ) . toThrow (
@@ -164,8 +191,9 @@ describe('Character count', () => {
164191 expect ( component . updateIfValueChanged ) . toHaveBeenCalled ( )
165192 } )
166193
167- it ( 'should handle deprecated params' , ( ) => {
168- $textarea . value = 'Existing value'
194+ it ( 'should handle deprecated params' , async ( ) => {
195+ await user . click ( $textarea )
196+ await user . keyboard ( 'Existing value' )
169197
170198 const component = new CharacterCount ( $root )
171199
@@ -234,13 +262,25 @@ describe('Character count', () => {
234262 } )
235263 } )
236264
265+ it ( 'configures `countType: "characters"`' , ( ) => {
266+ initExample ( "with count type 'characters'" )
267+
268+ const characterCount = new CharacterCount ( $root )
269+ expect ( characterCount . config ) . toEqual ( {
270+ ...CharacterCount . defaults ,
271+ maxlength : 200 ,
272+ threshold : 0 ,
273+ countType : 'characters'
274+ } )
275+ } )
276+
237277 it ( 'configures `countType: "words"`' , ( ) => {
238278 initExample ( "with count type 'words'" )
239279
240280 const characterCount = new CharacterCount ( $root )
241281 expect ( characterCount . config ) . toEqual ( {
242282 ...CharacterCount . defaults ,
243- maxlength : 150 ,
283+ maxlength : 50 ,
244284 threshold : 0 ,
245285 countType : 'words'
246286 } )
@@ -327,8 +367,9 @@ describe('Character count', () => {
327367 expect ( component . formatCountMessage ( 0 ) ) . toBe ( 'Different custom text.' )
328368 } )
329369
330- it ( 'uses existing textarea value for `maxlength` limit when initialised' , ( ) => {
331- $textarea . value = 'Existing value'
370+ it ( 'uses existing textarea value for `maxlength` limit when initialised' , async ( ) => {
371+ await user . click ( $textarea )
372+ await user . keyboard ( 'Existing value' )
332373
333374 const component = new CharacterCount ( $root , {
334375 maxlength : 100
@@ -339,8 +380,9 @@ describe('Character count', () => {
339380 )
340381 } )
341382
342- it ( 'uses existing textarea value for `maxwords` limit when initialised' , ( ) => {
343- $textarea . value = 'Existing value'
383+ it ( 'uses existing textarea value for `maxwords` limit when initialised' , async ( ) => {
384+ await user . click ( $textarea )
385+ await user . keyboard ( 'Existing value' )
344386
345387 const component = new CharacterCount ( $root , {
346388 maxwords : 100
@@ -349,12 +391,13 @@ describe('Character count', () => {
349391 expect ( component . getCountMessage ( ) ) . toBe ( 'You have 98 words remaining' )
350392 } )
351393
352- it ( 'uses current textarea value for `maxlength` limit via back/forward navigation' , ( ) => {
394+ it ( 'uses current textarea value for `maxlength` limit via back/forward navigation' , async ( ) => {
353395 const component = new CharacterCount ( $root , {
354396 maxlength : 100
355397 } )
356398
357- $textarea . value = 'Newly updated value'
399+ await user . click ( $textarea )
400+ await user . keyboard ( 'Newly updated value' )
358401
359402 // Trigger back/forward navigation
360403 window . dispatchEvent (
@@ -368,12 +411,13 @@ describe('Character count', () => {
368411 )
369412 } )
370413
371- it ( 'uses current textarea value for `maxwords` limit via back/forward navigation' , ( ) => {
414+ it ( 'uses current textarea value for `maxwords` limit via back/forward navigation' , async ( ) => {
372415 const component = new CharacterCount ( $root , {
373416 maxwords : 100
374417 } )
375418
376- $textarea . value = 'Newly updated value'
419+ await user . click ( $textarea )
420+ await user . keyboard ( 'Newly updated value' )
377421
378422 // Trigger back/forward navigation
379423 window . dispatchEvent (
0 commit comments