11import * as React from 'react' ;
2- import { Alert } from '@patternfly/react-core' ;
3- import { shallow , ShallowWrapper } from 'enzyme ' ;
2+ import { ValidatedOptions } from '@patternfly/react-core' ;
3+ import { render , screen } from '@testing-library/react ' ;
44import * as formik from 'formik' ;
55import { formikFormProps } from '@console/shared/src/test-utils/formik-props-utils' ;
66import * as imgUtils from '../../../../utils/imagestream-utils' ;
@@ -10,49 +10,62 @@ import {
1010 internalImageValues ,
1111} from '../../../edit-application/__tests__/edit-application-data' ;
1212import ImageStream from '../ImageStream' ;
13- import ImageStreamDropdown from '../ImageStreamDropdown' ;
14- import ImageStreamNsDropdown from '../ImageStreamNsDropdown' ;
15- import ImageStreamTagDropdown from '../ImageStreamTagDropdown' ;
13+
14+ jest . mock ( '../ImageStreamNsDropdown' , ( ) => ( {
15+ __esModule : true ,
16+ default : ( ) => 'Namespace Dropdown' ,
17+ } ) ) ;
18+
19+ jest . mock ( '../ImageStreamDropdown' , ( ) => ( {
20+ __esModule : true ,
21+ default : ( ) => 'ImageStream Dropdown' ,
22+ } ) ) ;
23+
24+ jest . mock ( '../ImageStreamTagDropdown' , ( ) => ( {
25+ __esModule : true ,
26+ default : ( ) => 'ImageStream Tag Dropdown' ,
27+ } ) ) ;
1628
1729const spyUseFormikContext = jest . spyOn ( formik , 'useFormikContext' ) ;
1830const spyUseReducer = jest . spyOn ( React , 'useReducer' ) ;
19- const spyUseField = jest . spyOn ( formik , 'useField' ) ;
2031const spyGetImageStreamTags = jest . spyOn ( imgUtils , 'getImageStreamTags' ) ;
21- const spyUseState = jest . spyOn ( React , 'useState' ) ;
2232
2333const mockReducerState = {
2434 loading : false ,
2535 accessLoading : false ,
2636 selectedImageStream : appResources . imageStream . data [ 0 ] ,
2737} ;
28- type ImagestreamProps = React . ComponentProps < typeof ImageStream > ;
2938
30- describe ( 'Imagestream' , ( ) => {
31- let wrapper : ShallowWrapper < ImagestreamProps > ;
39+ describe ( 'ImageStream' , ( ) => {
3240 beforeEach ( ( ) => {
3341 spyGetImageStreamTags . mockReturnValue ( { 3.6 : 3.6 } ) ;
3442 spyUseReducer . mockImplementation ( ( ) => [ mockReducerState , jest . fn ( ) ] ) ;
35- spyUseField . mockImplementation ( ( ) => [ { value : '' } , { } ] ) ;
36- spyUseState . mockReturnValue ( [ true , jest . fn ( ) ] ) ;
3743 spyUseFormikContext . mockReturnValue ( {
3844 ...formikFormProps ,
3945 values : internalImageValues ,
4046 initialValues : internalImageValues ,
4147 } ) ;
42- wrapper = shallow ( < ImageStream /> ) ;
4348 } ) ;
4449
45- it ( 'should render namespace, imagestream and imagestream-tag dropdowns ' , ( ) => {
46- expect ( wrapper . find ( ImageStreamNsDropdown ) ) . toHaveLength ( 1 ) ;
47- expect ( wrapper . find ( ImageStreamDropdown ) ) . toHaveLength ( 1 ) ;
48- expect ( wrapper . find ( ImageStreamTagDropdown ) ) . toHaveLength ( 1 ) ;
50+ afterEach ( ( ) => {
51+ jest . clearAllMocks ( ) ;
4952 } ) ;
5053
51- it ( 'should not render any alerts if current project and imagestream project is the same' , ( ) => {
52- expect ( wrapper . find ( Alert ) ) . toHaveLength ( 0 ) ;
54+ it ( 'should render namespace, imagestream and imagestream tag dropdowns' , ( ) => {
55+ render ( < ImageStream /> ) ;
56+
57+ expect ( screen . getByText ( 'Namespace Dropdown' ) ) . toBeVisible ( ) ;
58+ expect ( screen . getByText ( 'ImageStream Dropdown' ) ) . toBeVisible ( ) ;
59+ expect ( screen . getByText ( 'ImageStream Tag Dropdown' ) ) . toBeVisible ( ) ;
5360 } ) ;
5461
55- it ( 'should not render any alerts if the imagestream namespace is openshift' , ( ) => {
62+ it ( 'should not render any alerts when current project and imagestream project are the same' , ( ) => {
63+ render ( < ImageStream /> ) ;
64+
65+ expect ( screen . queryByRole ( 'alert' ) ) . not . toBeInTheDocument ( ) ;
66+ } ) ;
67+
68+ it ( 'should not render any alerts when imagestream namespace is openshift' , ( ) => {
5669 spyUseFormikContext . mockReturnValue ( {
5770 ...formikFormProps ,
5871 values : {
@@ -65,34 +78,53 @@ describe('Imagestream', () => {
6578 } ,
6679 initialValues : internalImageValues ,
6780 } ) ;
68- wrapper = shallow ( < ImageStream /> ) ;
69- expect ( wrapper . find ( Alert ) ) . toHaveLength ( 0 ) ;
81+
82+ render ( < ImageStream /> ) ;
83+
84+ expect ( screen . queryByRole ( 'alert' ) ) . not . toBeInTheDocument ( ) ;
7085 } ) ;
7186
72- it ( 'should render oc command alert if current namespace and imagestream namespace are not equal' , ( ) => {
87+ it ( 'should render oc command alert when current namespace and imagestream namespace are not equal' , ( ) => {
88+ const spyUseState = jest . spyOn ( React , 'useState' ) ;
89+ spyUseState . mockReturnValueOnce ( [ ValidatedOptions . default , jest . fn ( ) ] ) ; // validated state
90+ spyUseState . mockReturnValueOnce ( [ true , jest . fn ( ) ] ) ; // hasImageStreams state
91+
7392 spyUseFormikContext . mockReturnValue ( {
7493 ...formikFormProps ,
75- values : { ...internalImageValues , project : { name : 'project-a' } } ,
94+ values : {
95+ ...internalImageValues ,
96+ project : { name : 'project-a' } ,
97+ imageStream : { image : 'python' , tag : '3.6' , namespace : 'div' } ,
98+ } ,
7699 initialValues : internalImageValues ,
77100 } ) ;
78- wrapper = shallow ( < ImageStream /> ) ;
79- const alert = wrapper . find ( Alert ) ;
80- expect ( alert ) . toHaveLength ( 1 ) ;
81- expect ( alert . props ( ) . title ) . toEqual (
82- 'Service account default will need pull authority to deploy Images from div' ,
83- ) ;
101+
102+ render ( < ImageStream /> ) ;
103+
104+ expect (
105+ screen . getByText (
106+ 'Service account default will need pull authority to deploy Images from div' ,
107+ ) ,
108+ ) . toBeInTheDocument ( ) ;
109+ expect ( screen . getByText ( / Y o u c a n g r a n t a u t h o r i t y w i t h t h e c o m m a n d / ) ) . toBeInTheDocument ( ) ;
110+
111+ spyUseState . mockRestore ( ) ;
84112 } ) ;
85113
86- it ( 'should render imagestream not found alert if there are no imagestreams' , ( ) => {
114+ it ( 'should render imagestream not found alert when there are no imagestreams' , ( ) => {
115+ spyUseReducer . mockImplementation ( ( ) => [ { ...mockReducerState , loading : false } , jest . fn ( ) ] ) ;
87116 spyUseFormikContext . mockReturnValue ( {
88117 ...formikFormProps ,
89- values : { ...internalImageValues , imageStream : { image : '' } } ,
118+ values : { ...internalImageValues , imageStream : { image : '' , namespace : 'div' } } ,
90119 } ) ;
91- wrapper = shallow ( < ImageStream /> ) ;
92- expect ( wrapper . find ( Alert ) ) . toHaveLength ( 1 ) ;
120+
121+ render ( < ImageStream /> ) ;
122+
123+ expect ( screen . getByText ( 'No Image streams found' ) ) . toBeInTheDocument ( ) ;
124+ expect ( screen . getByText ( 'No Image streams are available in Project div' ) ) . toBeInTheDocument ( ) ;
93125 } ) ;
94126
95- it ( 'should render imagestream tag not found alert if there are no imagestreams tags' , ( ) => {
127+ it ( 'should render imagestream tag not found alert when there are no imagestream tags' , ( ) => {
96128 spyGetImageStreamTags . mockReturnValue ( { } ) ;
97129 spyUseFormikContext . mockReturnValue ( {
98130 ...formikFormProps ,
@@ -102,7 +134,10 @@ describe('Imagestream', () => {
102134 } ,
103135 initialValues : internalImageValues ,
104136 } ) ;
105- wrapper = shallow ( < ImageStream /> ) ;
106- expect ( wrapper . find ( Alert ) ) . toHaveLength ( 1 ) ;
137+
138+ render ( < ImageStream /> ) ;
139+
140+ expect ( screen . getByText ( 'No Image streams tags found' ) ) . toBeInTheDocument ( ) ;
141+ expect ( screen . getByText ( 'No tags are available in Image Stream python' ) ) . toBeInTheDocument ( ) ;
107142 } ) ;
108143} ) ;
0 commit comments