@@ -4,34 +4,36 @@ import { render, WithRoute } from 'lib/testHelpers';
4
4
import { clusterSchemaPath } from 'lib/paths' ;
5
5
import { screen } from '@testing-library/dom' ;
6
6
import {
7
- schemasInitialState ,
8
7
schemaVersion ,
9
8
schemaVersionWithNonAsciiChars ,
10
- } from 'redux/reducers/schemas/__test__/fixtures' ;
11
- import fetchMock from 'fetch-mock' ;
9
+ } from 'components/Schemas/Edit/__tests__/fixtures' ;
12
10
import ClusterContext , {
13
11
ContextProps ,
14
12
initialValue as contextInitialValue ,
15
13
} from 'components/contexts/ClusterContext' ;
16
- import { RootState } from 'redux/interfaces' ;
17
- import { act } from '@testing-library/react' ;
14
+ import {
15
+ useDeleteSchema ,
16
+ useGetLatestSchema ,
17
+ useGetSchemasVersions ,
18
+ } from 'lib/hooks/api/schemas' ;
18
19
19
20
import { versionPayload , versionEmptyPayload } from './fixtures' ;
20
21
21
22
const clusterName = 'testClusterName' ;
22
- const schemasAPILatestUrl = `/api/clusters/${ clusterName } /schemas/${ schemaVersion . subject } /latest` ;
23
- const schemasAPIVersionsUrl = `/api/clusters/${ clusterName } /schemas/${ schemaVersion . subject } /versions` ;
24
23
25
24
const mockHistoryPush = jest . fn ( ) ;
26
25
jest . mock ( 'react-router-dom' , ( ) => ( {
27
26
...jest . requireActual ( 'react-router-dom' ) ,
28
27
useNavigate : ( ) => mockHistoryPush ,
29
28
} ) ) ;
30
29
31
- const renderComponent = (
32
- initialState : RootState [ 'schemas' ] = schemasInitialState ,
33
- context : ContextProps = contextInitialValue
34
- ) =>
30
+ jest . mock ( 'lib/hooks/api/schemas' , ( ) => ( {
31
+ useGetSchemasVersions : jest . fn ( ) ,
32
+ useGetLatestSchema : jest . fn ( ) ,
33
+ useDeleteSchema : jest . fn ( ) ,
34
+ } ) ) ;
35
+
36
+ const renderComponent = ( context : ContextProps = contextInitialValue ) =>
35
37
render (
36
38
< WithRoute path = { clusterSchemaPath ( ) } >
37
39
< ClusterContext . Provider value = { context } >
@@ -40,27 +42,33 @@ const renderComponent = (
40
42
</ WithRoute > ,
41
43
{
42
44
initialEntries : [ clusterSchemaPath ( clusterName , schemaVersion . subject ) ] ,
43
- preloadedState : {
44
- schemas : initialState ,
45
- } ,
46
45
}
47
46
) ;
48
47
49
48
describe ( 'Details' , ( ) => {
50
- afterEach ( ( ) => fetchMock . reset ( ) ) ;
49
+ const deleteMockfn = jest . fn ( ) ;
50
+ beforeEach ( ( ) => {
51
+ deleteMockfn . mockClear ( ) ;
52
+
53
+ // TODO test case should be added for this
54
+ ( useDeleteSchema as jest . Mock ) . mockImplementation ( ( ) => ( {
55
+ mutateAsync : deleteMockfn ,
56
+ } ) ) ;
57
+ } ) ;
51
58
52
59
describe ( 'fetch failed' , ( ) => {
53
- it ( 'renders pageloader' , async ( ) => {
54
- const schemasAPILatestMock = fetchMock . getOnce ( schemasAPILatestUrl , 404 ) ;
55
- const schemasAPIVersionsMock = fetchMock . getOnce (
56
- schemasAPIVersionsUrl ,
57
- 404
58
- ) ;
59
- await act ( ( ) => {
60
- renderComponent ( ) ;
61
- } ) ;
62
- expect ( schemasAPILatestMock . called ( schemasAPILatestUrl ) ) . toBeTruthy ( ) ;
63
- expect ( schemasAPIVersionsMock . called ( schemasAPIVersionsUrl ) ) . toBeTruthy ( ) ;
60
+ it ( 'renders page loader' , async ( ) => {
61
+ ( useGetSchemasVersions as jest . Mock ) . mockImplementation ( ( ) => ( {
62
+ data : undefined ,
63
+ isFetching : false ,
64
+ isError : false ,
65
+ } ) ) ;
66
+ ( useGetLatestSchema as jest . Mock ) . mockImplementation ( ( ) => ( {
67
+ data : undefined ,
68
+ isFetching : false ,
69
+ isError : true ,
70
+ } ) ) ;
71
+ renderComponent ( ) ;
64
72
expect ( screen . getByRole ( 'progressbar' ) ) . toBeInTheDocument ( ) ;
65
73
expect ( screen . queryByText ( schemaVersion . subject ) ) . not . toBeInTheDocument ( ) ;
66
74
expect ( screen . queryByText ( 'Edit Schema' ) ) . not . toBeInTheDocument ( ) ;
@@ -71,19 +79,17 @@ describe('Details', () => {
71
79
describe ( 'fetch success' , ( ) => {
72
80
describe ( 'has schema versions' , ( ) => {
73
81
it ( 'renders component with schema info' , async ( ) => {
74
- const schemasAPILatestMock = fetchMock . getOnce (
75
- schemasAPILatestUrl ,
76
- schemaVersion
77
- ) ;
78
- const schemasAPIVersionsMock = fetchMock . getOnce (
79
- schemasAPIVersionsUrl ,
80
- versionPayload
81
- ) ;
82
- await act ( ( ) => {
83
- renderComponent ( ) ;
84
- } ) ;
85
- expect ( schemasAPILatestMock . called ( ) ) . toBeTruthy ( ) ;
86
- expect ( schemasAPIVersionsMock . called ( ) ) . toBeTruthy ( ) ;
82
+ ( useGetSchemasVersions as jest . Mock ) . mockImplementation ( ( ) => ( {
83
+ data : versionPayload ,
84
+ isFetching : false ,
85
+ isError : false ,
86
+ } ) ) ;
87
+ ( useGetLatestSchema as jest . Mock ) . mockImplementation ( ( ) => ( {
88
+ data : useGetSchemasVersions ,
89
+ isFetching : false ,
90
+ isError : false ,
91
+ } ) ) ;
92
+ renderComponent ( ) ;
87
93
expect ( screen . getByText ( 'Edit Schema' ) ) . toBeInTheDocument ( ) ;
88
94
expect ( screen . queryByRole ( 'progressbar' ) ) . not . toBeInTheDocument ( ) ;
89
95
expect ( screen . getByRole ( 'table' ) ) . toBeInTheDocument ( ) ;
@@ -93,19 +99,17 @@ describe('Details', () => {
93
99
describe ( 'fetch success schema with non ascii characters' , ( ) => {
94
100
describe ( 'has schema versions' , ( ) => {
95
101
it ( 'renders component with schema info' , async ( ) => {
96
- const schemasAPILatestMock = fetchMock . getOnce (
97
- schemasAPILatestUrl ,
98
- schemaVersionWithNonAsciiChars
99
- ) ;
100
- const schemasAPIVersionsMock = fetchMock . getOnce (
101
- schemasAPIVersionsUrl ,
102
- versionPayload
103
- ) ;
104
- await act ( ( ) => {
105
- renderComponent ( ) ;
106
- } ) ;
107
- expect ( schemasAPILatestMock . called ( ) ) . toBeTruthy ( ) ;
108
- expect ( schemasAPIVersionsMock . called ( ) ) . toBeTruthy ( ) ;
102
+ ( useGetSchemasVersions as jest . Mock ) . mockImplementation ( ( ) => ( {
103
+ data : versionPayload ,
104
+ isFetching : false ,
105
+ isError : false ,
106
+ } ) ) ;
107
+ ( useGetLatestSchema as jest . Mock ) . mockImplementation ( ( ) => ( {
108
+ data : schemaVersionWithNonAsciiChars ,
109
+ isFetching : false ,
110
+ isError : false ,
111
+ } ) ) ;
112
+ renderComponent ( ) ;
109
113
expect ( screen . getByText ( 'Edit Schema' ) ) . toBeInTheDocument ( ) ;
110
114
expect ( screen . queryByRole ( 'progressbar' ) ) . not . toBeInTheDocument ( ) ;
111
115
expect ( screen . getByRole ( 'table' ) ) . toBeInTheDocument ( ) ;
@@ -115,19 +119,17 @@ describe('Details', () => {
115
119
116
120
describe ( 'empty schema versions' , ( ) => {
117
121
beforeEach ( async ( ) => {
118
- const schemasAPILatestMock = fetchMock . getOnce (
119
- schemasAPILatestUrl ,
120
- schemaVersion
121
- ) ;
122
- const schemasAPIVersionsMock = fetchMock . getOnce (
123
- schemasAPIVersionsUrl ,
124
- versionEmptyPayload
125
- ) ;
126
- await act ( ( ) => {
127
- renderComponent ( ) ;
128
- } ) ;
129
- expect ( schemasAPILatestMock . called ( ) ) . toBeTruthy ( ) ;
130
- expect ( schemasAPIVersionsMock . called ( ) ) . toBeTruthy ( ) ;
122
+ ( useGetSchemasVersions as jest . Mock ) . mockImplementation ( ( ) => ( {
123
+ data : versionEmptyPayload ,
124
+ isFetching : false ,
125
+ isError : false ,
126
+ } ) ) ;
127
+ ( useGetLatestSchema as jest . Mock ) . mockImplementation ( ( ) => ( {
128
+ data : schemaVersionWithNonAsciiChars ,
129
+ isFetching : false ,
130
+ isError : false ,
131
+ } ) ) ;
132
+ renderComponent ( ) ;
131
133
} ) ;
132
134
133
135
// seems like incorrect behaviour
0 commit comments