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,68 @@ 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 } /> ) ;
36+
37+ expect ( getAceEditors ( ) . length ) . toBe ( 2 ) ;
38+ } ) ;
39+ } ) ;
2440
2541 describe ( 'triggering' , ( ) => {
42+ const getTabById = id =>
43+ screen . getAllByRole ( 'presentation' , { current : "page" } )
44+ . find ( tab => tab . getAttribute ( 'id' ) === id )
45+
2646 it ( 'should trigger input view' , async ( ) => {
2747 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 ) ;
48+ renderEditor ( props ) ;
49+ const inputTab = getTabById ( 'input-navitem' ) ;
50+
51+ expect ( inputTab ) . toBeInTheDocument ( ) ;
52+ expect ( inputTab ) . toHaveClass ( 'pf-m-current' ) ;
53+ expect ( inputTab ) . toHaveTextContent ( 'Editor' ) ;
3654 } ) ;
3755 it ( 'should trigger input view with no template' , async ( ) => {
3856 const props = {
3957 ...editorOptions ,
4058 ...didMountStubs ( ) ,
4159 data : { ...editorOptions . data , template : null } ,
4260 } ;
43- const component = mount ( < Editor { ...props } /> ) ;
44- await act ( async ( ) => jest . advanceTimersByTime ( 1000 ) ) ;
45- expect ( component . props ( ) . template ) . toBe ( '<? />' ) ;
61+ renderEditor ( props ) ;
62+ const aceEditors = document . querySelectorAll ( '.ace_editor_form' ) ;
63+ const hasTemplateText = Array . from ( aceEditors ) . some ( container => container . textContent . includes ( '<? />' ) ) ;
64+
65+ expect ( hasTemplateText ) . toBe ( false ) ;
4666 } ) ;
4767 it ( 'should trigger diff view' , async ( ) => {
4868 const props = {
4969 ...editorOptions ,
5070 ...didMountStubs ( ) ,
5171 selectedView : 'diff' ,
5272 } ;
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 ) ;
73+ renderEditor ( props ) ;
74+ const diffTab = getTabById ( 'diff-navitem' ) ;
75+
76+ expect ( diffTab ) . toBeInTheDocument ( ) ;
77+ expect ( diffTab ) . toHaveClass ( 'pf-m-current' ) ;
78+ expect ( diffTab ) . toHaveTextContent ( 'Changes' ) ;
6179 } ) ;
6280 it ( 'should trigger preview view' , async ( ) => {
6381 const props = {
@@ -66,18 +84,14 @@ describe('Editor', () => {
6684 selectedView : 'preview' ,
6785 isRendering : true ,
6886 } ;
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 ) ;
87+ renderEditor ( props ) ;
88+ const closeButton = screen . queryByLabelText ( / c l o s e d a n g e r a l e r t / i) ;
89+
90+ if ( closeButton ) await act ( async ( ) => await fireEvent . click ( closeButton ) ) ;
91+ const previewTab = getTabById ( 'preview-navitem' ) ;
92+
93+ expect ( previewTab ) . toBeInTheDocument ( ) ;
94+ expect ( previewTab ) . toHaveClass ( 'pf-m-current' ) ;
8195 } ) ;
8296 } ) ;
8397 it ( 'should trigger hidden value editor' , async ( ) => {
@@ -88,20 +102,29 @@ describe('Editor', () => {
88102 isRendering : true ,
89103 isMasked : true ,
90104 } ;
91- const wrapper = mount ( < Editor { ...props } /> ) ;
92- await act ( async ( ) => jest . advanceTimersByTime ( 1000 ) ) ;
93- expect ( wrapper . find ( '.mask-editor' ) . exists ( ) ) . toBe ( true ) ;
105+ renderEditor ( props ) ;
106+ const maskedEditor = document . querySelector ( '.mask-editor' ) ;
107+
108+ expect ( maskedEditor ) . toBeInTheDocument ( ) ;
94109 } ) ;
95110 it ( 'textarea disappears if readOnly' , async ( ) => {
111+ const getTextAreasHidden = ( ) => document . querySelectorAll ( 'textarea.hidden' )
96112 const props = {
97113 ...editorOptions ,
98114 ...didMountStubs ( ) ,
99115 selectedView : 'input' ,
116+ readOnly : false ,
100117 } ;
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 ) ;
118+ const { rerender } = renderEditor ( props ) ;
119+
120+ expect ( getTextAreasHidden ( ) . length ) . toBe ( 1 ) ;
121+
122+ const newProps = {
123+ ...props ,
124+ readOnly : true ,
125+ } ;
126+ rerender ( < Editor { ...newProps } /> ) ;
127+
128+ expect ( getTextAreasHidden ( ) . length ) . toBe ( 0 ) ;
106129 } ) ;
107130} ) ;
0 commit comments