1010 * Red Hat, Inc. - initial API and implementation
1111 */
1212
13- import { ApplicationId } from '@eclipse-che/common' ;
1413import userEvent from '@testing-library/user-event' ;
14+ import { dump } from 'js-yaml' ;
1515import React from 'react' ;
1616import { Provider } from 'react-redux' ;
1717import { Store } from 'redux' ;
1818
19+ import EditorTools from '@/components/EditorTools' ;
1920import getComponentRenderer , { screen } from '@/services/__mocks__/getComponentRenderer' ;
20- import devfileApi from '@/services/devfileApi' ;
2121import { MockStoreBuilder } from '@/store/__mocks__/mockStore' ;
2222
23- import EditorTools from '..' ;
23+ // mute console.error
24+ console . error = jest . fn ( ) ;
2425
2526jest . mock ( '@/contexts/ToggleBars' ) ;
2627
@@ -49,19 +50,8 @@ const mockOnExpand = jest.fn();
4950let store : Store ;
5051
5152describe ( 'EditorTools' , ( ) => {
52- const clusterConsole = {
53- id : ApplicationId . CLUSTER_CONSOLE ,
54- url : 'https://console-url' ,
55- icon : 'https://console-icon-url' ,
56- title : 'Cluster console' ,
57- } ;
58-
5953 beforeEach ( ( ) => {
60- store = new MockStoreBuilder ( )
61- . withClusterInfo ( {
62- applications : [ clusterConsole ] ,
63- } )
64- . build ( ) ;
54+ store = new MockStoreBuilder ( ) . build ( ) ;
6555
6656 jest . useFakeTimers ( ) ;
6757 } ) ;
@@ -72,26 +62,22 @@ describe('EditorTools', () => {
7262 } ) ;
7363
7464 describe ( 'Devfile' , ( ) => {
75- let devfile : devfileApi . Devfile ;
76-
77- beforeEach ( ( ) => {
78- devfile = {
79- schemaVersion : '2.1.0' ,
80- metadata : {
81- name : 'my-project' ,
82- namespace : 'user-che' ,
83- } ,
84- } ;
65+ const name = 'my-project' ;
66+ const devfileContent = dump ( {
67+ schemaVersion : '2.1.0' ,
68+ metadata : {
69+ name,
70+ } ,
8571 } ) ;
8672
8773 test ( 'snapshot' , ( ) => {
88- const snapshot = createSnapshot ( devfile ) ;
74+ const snapshot = createSnapshot ( devfileContent , name ) ;
8975 expect ( snapshot . toJSON ( ) ) . toMatchSnapshot ( ) ;
9076 } ) ;
9177
9278 test ( 'expand and compress' , async ( ) => {
9379 const user = userEvent . setup ( { advanceTimers : jest . advanceTimersByTime } ) ;
94- renderComponent ( devfile ) ;
80+ renderComponent ( devfileContent , name ) ;
9581
9682 /* expand the editor */
9783
@@ -120,7 +106,7 @@ describe('EditorTools', () => {
120106 const mockCreateObjectURL = jest . fn ( ) . mockReturnValue ( 'blob-url' ) ;
121107 URL . createObjectURL = mockCreateObjectURL ;
122108
123- renderComponent ( devfile ) ;
109+ renderComponent ( devfileContent , name ) ;
124110
125111 const copyButtonName = 'Copy to clipboard' ;
126112 expect ( screen . queryByRole ( 'button' , { name : copyButtonName } ) ) . toBeTruthy ;
@@ -129,7 +115,7 @@ describe('EditorTools', () => {
129115 await user . click ( copyButton ) ;
130116
131117 expect ( mockClipboard ) . toHaveBeenCalledWith (
132- 'schemaVersion: 2.1.0\nmetadata:\n name: my-project\n namespace: user-che\n ' ,
118+ 'schemaVersion: 2.1.0\nmetadata:\n name: my-project\n' ,
133119 ) ;
134120
135121 /* 'Copy to clipboard' should be hidden for a while */
@@ -146,46 +132,16 @@ describe('EditorTools', () => {
146132 expect ( screen . queryByRole ( 'button' , { name : copyButtonNameAfter } ) ) . toBeFalsy ;
147133 } ) ;
148134 } ) ;
149-
150- describe ( 'DevWorkspace' , ( ) => {
151- let devWorkspace : devfileApi . DevWorkspace ;
152-
153- beforeEach ( ( ) => {
154- devWorkspace = {
155- apiVersion : '1.0.0' ,
156- metadata : {
157- name : 'my-project' ,
158- namespace : 'user-che' ,
159- labels : { } ,
160- uid : '123' ,
161- } ,
162- kind : 'DevWorkspace' ,
163- spec : {
164- template : { } ,
165- started : true ,
166- } ,
167- } ;
168- } ) ;
169-
170- test ( 'snapshot' , ( ) => {
171- const snapshot = createSnapshot ( devWorkspace ) ;
172- expect ( snapshot . toJSON ( ) ) . toMatchSnapshot ( ) ;
173- } ) ;
174-
175- test ( 'Cluster Console' , ( ) => {
176- renderComponent ( devWorkspace ) ;
177-
178- const clusterConsoleButton = screen . getByRole ( 'link' , { name : clusterConsole . title } ) ;
179-
180- expect ( clusterConsoleButton . textContent ) . toEqual ( clusterConsole . title ) ;
181- } ) ;
182- } ) ;
183135} ) ;
184136
185- function getComponent ( devfileOrDevWorkspace : devfileApi . Devfile | devfileApi . DevWorkspace ) {
137+ function getComponent ( contentText : string , workspaceName : string ) {
186138 return (
187139 < Provider store = { store } >
188- < EditorTools devfileOrDevWorkspace = { devfileOrDevWorkspace } handleExpand = { mockOnExpand } />
140+ < EditorTools
141+ contentText = { contentText }
142+ workspaceName = { workspaceName }
143+ handleExpand = { mockOnExpand }
144+ />
189145 </ Provider >
190146 ) ;
191147}
0 commit comments