11import React from 'react' ;
2- import { act } from '@testing-library/react' ;
3- import { mount } from 'enzyme ';
4- import { testComponentSnapshotsWithFixtures } from '../../../common/testHelpers' ;
2+ import { render , screen , act , fireEvent } from '@testing-library/react' ;
3+ import '@testing-library/jest-dom/extend-expect ';
4+
55import Editor from '../Editor' ;
66import { editorOptions } from '../Editor.fixtures' ;
77
@@ -14,50 +14,67 @@ const didMountStubs = () => ({
1414} ) ;
1515
1616const fixtures = {
17- ' renders editor' : editorOptions ,
17+ renders : editorOptions ,
1818} ;
1919
20+ const renderEditor = ( props = fixtures . renders ) => render ( < Editor { ...props } /> ) ;
21+
2022describe ( 'Editor' , ( ) => {
21- jest . useFakeTimers ( ) ;
22- describe ( 'rendering' , ( ) =>
23- testComponentSnapshotsWithFixtures ( Editor , fixtures ) ) ;
23+ describe ( 'rendering' , ( ) => {
24+ const getAceEditors = ( ) => screen . getAllByRole ( 'textbox' , { name : 'Cursor at row 1' } )
25+
26+ it ( 'renders' , ( ) => {
27+ renderEditor ( ) ;
28+
29+ expect ( screen . getByText ( '<? />' ) ) . toBeInTheDocument ( ) ;
30+ expect ( getAceEditors ( ) . length ) . toBe ( 2 ) ;
31+ } ) ;
32+ it ( 're-renders' , ( ) => {
33+ const { rerender } = renderEditor ( ) ;
34+ const props = { ...editorOptions , ...didMountStubs ( ) } ;
35+ rerender ( < Editor { ...props } /> ) ;
2436
37+ expect ( getAceEditors ( ) . length ) . toBe ( 2 ) ;
38+ } ) ;
39+ } ) ;
2540 describe ( 'triggering' , ( ) => {
41+ const getTabById = id =>
42+ screen . getAllByRole ( 'presentation' , { current : "page" } )
43+ . find ( tab => tab . getAttribute ( 'id' ) === id )
44+
2645 it ( 'should trigger input view' , async ( ) => {
2746 const props = { ...editorOptions , ...didMountStubs ( ) } ;
28- const component = mount ( < Editor { ...props } /> ) ;
29- await act ( async ( ) => jest . advanceTimersByTime ( 1000 ) ) ;
30- expect (
31- component
32- . find ( 'li[role="presentation"]' )
33- . at ( 0 )
34- . hasClass ( 'active' )
35- ) . toBe ( true ) ;
47+ renderEditor ( props ) ;
48+ const inputTab = getTabById ( 'input-navitem' ) ;
49+
50+ expect ( inputTab ) . toBeInTheDocument ( ) ;
51+ expect ( inputTab ) . toHaveClass ( 'pf-m-current' ) ;
52+ expect ( inputTab ) . toHaveTextContent ( 'Editor' ) ;
3653 } ) ;
3754 it ( 'should trigger input view with no template' , async ( ) => {
3855 const props = {
3956 ...editorOptions ,
4057 ...didMountStubs ( ) ,
4158 data : { ...editorOptions . data , template : null } ,
4259 } ;
43- const component = mount ( < Editor { ...props } /> ) ;
44- await act ( async ( ) => jest . advanceTimersByTime ( 1000 ) ) ;
45- expect ( component . props ( ) . template ) . toBe ( '<? />' ) ;
60+ renderEditor ( props ) ;
61+ const aceEditors = document . querySelectorAll ( '.ace_editor_form' ) ;
62+ const hasTemplateText = Array . from ( aceEditors ) . some ( container => container . textContent . includes ( '<? />' ) ) ;
63+
64+ expect ( hasTemplateText ) . toBe ( false ) ;
4665 } ) ;
4766 it ( 'should trigger diff view' , async ( ) => {
4867 const props = {
4968 ...editorOptions ,
5069 ...didMountStubs ( ) ,
5170 selectedView : 'diff' ,
5271 } ;
53- const component = mount ( < Editor { ...props } /> ) ;
54- await act ( async ( ) => jest . advanceTimersByTime ( 1000 ) ) ;
55- expect (
56- component
57- . find ( 'li[role="presentation"]' )
58- . at ( 1 )
59- . hasClass ( 'active' )
60- ) . toBe ( true ) ;
72+ renderEditor ( props ) ;
73+ const diffTab = getTabById ( 'diff-navitem' ) ;
74+
75+ expect ( diffTab ) . toBeInTheDocument ( ) ;
76+ expect ( diffTab ) . toHaveClass ( 'pf-m-current' ) ;
77+ expect ( diffTab ) . toHaveTextContent ( 'Changes' ) ;
6178 } ) ;
6279 it ( 'should trigger preview view' , async ( ) => {
6380 const props = {
@@ -66,18 +83,14 @@ describe('Editor', () => {
6683 selectedView : 'preview' ,
6784 isRendering : true ,
6885 } ;
69- const wrapper = mount ( < Editor { ...props } /> ) ;
70- wrapper . find ( 'button.close' ) . simulate ( 'click' ) ;
71- await act ( async ( ) => jest . advanceTimersByTime ( 1000 ) ) ;
72- const component = mount ( < Editor { ...props } /> ) ;
73- await act ( async ( ) => jest . advanceTimersByTime ( 1000 ) ) ;
74-
75- expect (
76- component
77- . find ( 'li[role="presentation"]' )
78- . at ( 2 )
79- . hasClass ( 'active' )
80- ) . toBe ( true ) ;
86+ renderEditor ( props ) ;
87+ const closeButton = screen . queryByLabelText ( / c l o s e d a n g e r a l e r t / i) ;
88+
89+ if ( closeButton ) await act ( async ( ) => await fireEvent . click ( closeButton ) ) ;
90+ const previewTab = getTabById ( 'preview-navitem' ) ;
91+
92+ expect ( previewTab ) . toBeInTheDocument ( ) ;
93+ expect ( previewTab ) . toHaveClass ( 'pf-m-current' ) ;
8194 } ) ;
8295 } ) ;
8396 it ( 'should trigger hidden value editor' , async ( ) => {
@@ -88,20 +101,29 @@ describe('Editor', () => {
88101 isRendering : true ,
89102 isMasked : true ,
90103 } ;
91- const wrapper = mount ( < Editor { ...props } /> ) ;
92- await act ( async ( ) => jest . advanceTimersByTime ( 1000 ) ) ;
93- expect ( wrapper . find ( '.mask-editor' ) . exists ( ) ) . toBe ( true ) ;
104+ renderEditor ( props ) ;
105+ const maskedEditor = document . querySelector ( '.mask-editor' ) ;
106+
107+ expect ( maskedEditor ) . toBeInTheDocument ( ) ;
94108 } ) ;
95109 it ( 'textarea disappears if readOnly' , async ( ) => {
110+ const getTextAreasHidden = ( ) => document . querySelectorAll ( 'textarea.hidden' )
96111 const props = {
97112 ...editorOptions ,
98113 ...didMountStubs ( ) ,
99114 selectedView : 'input' ,
115+ readOnly : false ,
100116 } ;
101- const wrapper = mount ( < Editor { ...props } /> ) ;
102- await act ( async ( ) => jest . advanceTimersByTime ( 1000 ) ) ;
103- expect ( wrapper . find ( 'textarea.hidden' ) . exists ( ) ) . toBe ( true ) ;
104- wrapper . setProps ( { readOnly : true } ) ;
105- expect ( wrapper . find ( 'textarea.hidden' ) . exists ( ) ) . toBe ( false ) ;
117+ const { rerender } = renderEditor ( props ) ;
118+
119+ expect ( getTextAreasHidden ( ) . length ) . toBe ( 1 ) ;
120+
121+ const newProps = {
122+ ...props ,
123+ readOnly : true ,
124+ } ;
125+ rerender ( < Editor { ...newProps } /> ) ;
126+
127+ expect ( getTextAreasHidden ( ) . length ) . toBe ( 0 ) ;
106128 } ) ;
107129} ) ;
0 commit comments