1+ import { userEvent } from '@testing-library/user-event' ;
12import {
23 initializeMocks ,
34 render ,
45 screen ,
56 waitFor ,
6- fireEvent ,
7+ findByDeepTextContent ,
78} from '@src/testUtils' ;
89
910import {
1011 mockLibraryBlockDraftHistory ,
1112 mockLibraryBlockPublishHistory ,
1213 mockLibraryBlockPublishHistoryEntries ,
1314 mockLibraryBlockCreationEntry ,
14- } from '../.. /data/api.mocks' ;
15+ } from '@src/library-authoring /data/api.mocks' ;
1516import { HistoryComponentLog } from './HistoryLog' ;
1617
1718mockLibraryBlockDraftHistory . applyMock ( ) ;
@@ -23,19 +24,6 @@ const renderComponent = (componentId: string) => render(
2324 < HistoryComponentLog componentId = { componentId } displayName = "Test Component" /> ,
2425) ;
2526
26- /**
27- * Finds the innermost element whose full textContent (normalized whitespace) matches the given regex.
28- * Useful when text is split across child elements (e.g. by an icon).
29- * Requiring that no direct child also matches ensures we get the deepest matching element.
30- */
31- const findByTextContent = ( pattern : RegExp ) => screen . findByText ( ( _ , el ) => {
32- if ( ! el ) return false ;
33- const normalizedText = ( el . textContent ?? '' ) . replace ( / \s + / g, ' ' ) . trim ( ) ;
34- if ( ! pattern . test ( normalizedText ) ) return false ;
35- return ! Array . from ( el . children ) . some (
36- ( child ) => pattern . test ( ( child . textContent ?? '' ) . replace ( / \s + / g, ' ' ) . trim ( ) ) ,
37- ) ;
38- } ) ;
3927
4028describe ( '<HistoryComponentLog />' , ( ) => {
4129 beforeEach ( ( ) => {
@@ -48,12 +36,13 @@ describe('<HistoryComponentLog />', () => {
4836 } ) ;
4937
5038 it ( 'renders draft history group with entries when they exist' , async ( ) => {
39+ const user = userEvent . setup ( ) ;
5140 renderComponent ( mockLibraryBlockCreationEntry . usageKey ) ;
52- const trigger = await findByTextContent ( / T e s t C o m p o n e n t i s a d r a f t / i) ;
41+ const trigger = await findByDeepTextContent ( / T e s t C o m p o n e n t i s a d r a f t / i) ;
5342 expect ( trigger ) . toBeInTheDocument ( ) ;
54- fireEvent . click ( trigger ) ;
55- expect ( await findByTextContent ( / t e s t _ u s e r _ 1 e d i t e d .* E l e c t r o n A r c s / i) ) . toBeInTheDocument ( ) ;
56- expect ( await findByTextContent ( / t e s t _ u s e r _ 2 r e n a m e d .* M o r e o n Q u a r k s / i) ) . toBeInTheDocument ( ) ;
43+ await user . click ( trigger ) ;
44+ expect ( await findByDeepTextContent ( / t e s t _ u s e r _ 1 e d i t e d .* E l e c t r o n A r c s / i) ) . toBeInTheDocument ( ) ;
45+ expect ( await findByDeepTextContent ( / t e s t _ u s e r _ 2 r e n a m e d .* M o r e o n Q u a r k s / i) ) . toBeInTheDocument ( ) ;
5746 } ) ;
5847
5948 it ( 'does not render draft history group when there are no draft entries' , async ( ) => {
@@ -64,17 +53,18 @@ describe('<HistoryComponentLog />', () => {
6453
6554 it ( 'renders publish history group when one exists' , async ( ) => {
6655 renderComponent ( mockLibraryBlockCreationEntry . usageKey ) ;
67- expect ( await findByTextContent ( / a u t h o r p u b l i s h e d .* P r o t o n s / i) ) . toBeInTheDocument ( ) ;
56+ expect ( await findByDeepTextContent ( / a u t h o r p u b l i s h e d .* P r o t o n s / i) ) . toBeInTheDocument ( ) ;
6857 expect ( await screen . findByText ( / 5 a u t h o r s c o n t r i b u t e d / i) ) . toBeInTheDocument ( ) ;
6958 } ) ;
7059
7160 it ( 'loads and shows publish history entries after expanding the publish group' , async ( ) => {
61+ const user = userEvent . setup ( ) ;
7262 renderComponent ( mockLibraryBlockCreationEntry . usageKey ) ;
7363 expect ( await screen . findByText ( / 5 a u t h o r s c o n t r i b u t e d / i) ) . toBeInTheDocument ( ) ;
74- const publishTrigger = await findByTextContent ( / a u t h o r p u b l i s h e d .* P r o t o n s / i) ;
64+ const publishTrigger = await findByDeepTextContent ( / a u t h o r p u b l i s h e d .* P r o t o n s / i) ;
7565
76- fireEvent . click ( publishTrigger ) ;
77- expect ( await findByTextContent ( / t e s t _ u s e r e d i t e d .* P r o t o n s / i) ) . toBeInTheDocument ( ) ;
66+ await user . click ( publishTrigger ) ;
67+ expect ( await findByDeepTextContent ( / t e s t _ u s e r e d i t e d .* P r o t o n s / i) ) . toBeInTheDocument ( ) ;
7868 await waitFor ( ( ) => expect ( screen . queryByText ( / 5 a u t h o r s c o n t r i b u t e d / i) ) . not . toBeInTheDocument ( ) ) ;
7969 } ) ;
8070
@@ -86,6 +76,6 @@ describe('<HistoryComponentLog />', () => {
8676
8777 it ( 'always renders the created group with fallback user when createdBy is null' , async ( ) => {
8878 renderComponent ( mockLibraryBlockCreationEntry . usageKey ) ;
89- expect ( await findByTextContent ( / A u t h o r c r e a t e d .* I n t r o d u c t i o n t o T e s t i n g 1 / i) ) . toBeInTheDocument ( ) ;
79+ expect ( await findByDeepTextContent ( / A u t h o r c r e a t e d .* I n t r o d u c t i o n t o T e s t i n g 1 / i) ) . toBeInTheDocument ( ) ;
9080 } ) ;
9181} ) ;
0 commit comments