@@ -28,11 +28,6 @@ describe('useRequest', () => {
2828 fetchMock . get ( successApi , { data : 'success' } ) ;
2929 fetchMock . get ( failApi , 404 ) ;
3030
31- const unknownService = 1 ;
32- const serviceWillReturnString = ( ) => successApi ;
33- const serviceWillReturnObject = ( ) => ( { url : successApi } ) ;
34- const serviceWillReturnUnknown = ( ) => unknownService ;
35-
3631 const originalError = console . error ;
3732 beforeEach ( ( ) => {
3833 console . error = jest . fn ( ) ;
@@ -55,120 +50,6 @@ describe('useRequest', () => {
5550 expect ( useRequest ) . toBeDefined ( ) ;
5651 } ) ;
5752
58- test ( 'should use string service' , async ( ) => {
59- const wrapper = shallowMount (
60- defineComponent ( {
61- setup ( ) {
62- const { data } = useRequest < { data : string } > ( successApi ) ;
63- return ( ) => < button > { `${ data . value ?. data } ` } </ button > ;
64- } ,
65- } ) ,
66- ) ;
67- expect ( wrapper . text ( ) ) . toBe ( 'undefined' ) ;
68- await waitForTime ( 1000 ) ;
69- expect ( wrapper . text ( ) ) . toBe ( 'success' ) ;
70- } ) ;
71-
72- test ( 'should throw error when service error' , async ( ) => {
73- const wrapper = shallowMount (
74- defineComponent ( {
75- setup ( ) {
76- const { error } = useRequest ( failApi ) ;
77- return ( ) => < button > { `${ error . value ?. message } ` } </ button > ;
78- } ,
79- } ) ,
80- ) ;
81- expect ( wrapper . text ( ) ) . toBe ( 'undefined' ) ;
82- await waitForTime ( 1000 ) ;
83- expect ( wrapper . text ( ) ) . toBe ( 'Not Found' ) ;
84- } ) ;
85-
86- test ( 'should use object service' , async ( ) => {
87- const wrapper = shallowMount (
88- defineComponent ( {
89- setup ( ) {
90- const { data } = useRequest < { data : string } > ( {
91- test : 'value' ,
92- url : successApi ,
93- } ) ;
94- return ( ) => < button > { `${ data . value ?. data } ` } </ button > ;
95- } ,
96- } ) ,
97- ) ;
98- expect ( wrapper . text ( ) ) . toBe ( 'undefined' ) ;
99- await waitForTime ( 1000 ) ;
100- expect ( wrapper . text ( ) ) . toBe ( 'success' ) ;
101- } ) ;
102-
103- test ( 'should use function service that will return string' , async ( ) => {
104- const wrapper = shallowMount (
105- defineComponent ( {
106- setup ( ) {
107- const { data } = useRequest < { data : string } > (
108- serviceWillReturnString ,
109- ) ;
110- return ( ) => < button > { `${ data . value ?. data } ` } </ button > ;
111- } ,
112- } ) ,
113- ) ;
114- expect ( wrapper . text ( ) ) . toBe ( 'undefined' ) ;
115- await waitForTime ( 1000 ) ;
116- expect ( wrapper . text ( ) ) . toBe ( 'success' ) ;
117- } ) ;
118-
119- test ( 'should use function service that will return object' , async ( ) => {
120- const wrapper = shallowMount (
121- defineComponent ( {
122- setup ( ) {
123- const { data } = useRequest < { data : string } > (
124- serviceWillReturnObject ,
125- ) ;
126- return ( ) => < button > { `${ data . value ?. data } ` } </ button > ;
127- } ,
128- } ) ,
129- ) ;
130- expect ( wrapper . text ( ) ) . toBe ( 'undefined' ) ;
131- await waitForTime ( 1000 ) ;
132- expect ( wrapper . text ( ) ) . toBe ( 'success' ) ;
133- } ) ;
134-
135- test ( 'should use function service that will return unknown type' , ( ) => {
136- const fn = jest . fn ( ) ;
137- shallowMount (
138- defineComponent ( {
139- setup ( ) {
140- try {
141- useRequest ( serviceWillReturnUnknown as any ) ;
142- } catch ( error ) {
143- expect ( error . message ) . toBe ( 'Unknown service type' ) ;
144- fn ( ) ;
145- }
146- return ( ) => < div /> ;
147- } ,
148- } ) ,
149- ) ;
150-
151- expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
152- } ) ;
153-
154- test ( 'should throw error when use unknown service' , ( ) => {
155- const fn = jest . fn ( ) ;
156- shallowMount (
157- defineComponent ( {
158- setup ( ) {
159- try {
160- useRequest ( unknownService as any ) ;
161- } catch ( error ) {
162- expect ( error . message ) . toBe ( 'Unknown service type' ) ;
163- fn ( ) ;
164- }
165- return ( ) => < div /> ;
166- } ,
167- } ) ,
168- ) ;
169- expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
170- } ) ;
171-
17253 test ( 'should auto run' , async ( ) => {
17354 const wrapper = shallowMount (
17455 defineComponent ( {
0 commit comments