1- const getWordPressVersions = require ( './get-versions' ) ;
2-
3- // Mock @actions /core
1+ // Mock core object
42const mockCore = {
5- getInput : jest . fn ( ) ,
63 setOutput : jest . fn ( ) ,
74 setFailed : jest . fn ( ) ,
85} ;
96
10- jest . mock ( '@actions/core' , ( ) => mockCore ) ;
7+ const getWordPressVersions = require ( './get-versions' ) ;
118
129// Mock global fetch
1310global . fetch = jest . fn ( ) ;
@@ -16,91 +13,73 @@ describe('getWordPressVersions', () => {
1613 beforeEach ( ( ) => {
1714 jest . clearAllMocks ( ) ;
1815 console . log = jest . fn ( ) ; // Mock console.log
16+ fetch . mockClear ( ) ; // Clear fetch mock
1917 } ) ;
2018
2119 describe ( 'successful API response' , ( ) => {
2220 const mockApiResponse = {
2321 offers : [
2422 { version : '6.4.1' , response : 'autoupdate' } ,
25- { version : '6.4.0' , response : 'autoupdate' } ,
2623 { version : '6.3.2' , response : 'autoupdate' } ,
27- { version : '6.3.1' , response : 'autoupdate' } ,
28- { version : '6.3.0' , response : 'autoupdate' } ,
2924 { version : '6.2.3' , response : 'autoupdate' } ,
30- { version : '6.2.2' , response : 'autoupdate' } ,
3125 { version : '6.1.4' , response : 'autoupdate' } ,
32- { version : '5.9.8' , response : 'upgrade' } , // Should be filtered out
26+ { version : '5.9.8' , response : 'autoupdate' } ,
27+ { version : '5.8.7' , response : 'autoupdate' } ,
28+ { version : '5.7.9' , response : 'upgrade' } , // Should be filtered out
3329 ]
3430 } ;
3531
36- beforeEach ( ( ) => {
32+ it ( 'should fetch and return default number of versions (3)' , async ( ) => {
3733 fetch . mockResolvedValue ( {
3834 json : jest . fn ( ) . mockResolvedValue ( mockApiResponse ) ,
3935 } ) ;
40- } ) ;
4136
42- it ( 'should fetch and return default number of versions (3)' , async ( ) => {
43- mockCore . getInput . mockReturnValue ( '' ) ;
44-
45- await getWordPressVersions ( ) ;
37+ await getWordPressVersions ( mockCore , 3 ) ;
4638
4739 expect ( fetch ) . toHaveBeenCalledWith ( 'https://api.wordpress.org/core/version-check/1.7/' ) ;
4840 expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( [ '6.4' , '6.3' , '6.2' ] ) ) ;
4941 expect ( mockCore . setFailed ) . not . toHaveBeenCalled ( ) ;
5042 } ) ;
5143
5244 it ( 'should fetch and return custom number of versions' , async ( ) => {
53- mockCore . getInput . mockReturnValue ( '5' ) ;
45+ fetch . mockResolvedValue ( {
46+ json : jest . fn ( ) . mockResolvedValue ( mockApiResponse ) ,
47+ } ) ;
5448
55- await getWordPressVersions ( ) ;
49+ await getWordPressVersions ( mockCore , 5 ) ;
5650
5751 expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( [ '6.4' , '6.3' , '6.2' , '6.1' , '5.9' ] ) ) ;
5852 } ) ;
5953
6054 it ( 'should handle string input for number' , async ( ) => {
61- mockCore . getInput . mockReturnValue ( '2' ) ;
55+ fetch . mockResolvedValue ( {
56+ json : jest . fn ( ) . mockResolvedValue ( mockApiResponse ) ,
57+ } ) ;
6258
63- await getWordPressVersions ( ) ;
59+ await getWordPressVersions ( mockCore , 2 ) ;
6460
6561 expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( [ '6.4' , '6.3' ] ) ) ;
6662 } ) ;
6763
6864 it ( 'should handle invalid number input and default to 3' , async ( ) => {
69- mockCore . getInput . mockReturnValue ( 'invalid' ) ;
65+ fetch . mockResolvedValue ( {
66+ json : jest . fn ( ) . mockResolvedValue ( mockApiResponse ) ,
67+ } ) ;
7068
71- await getWordPressVersions ( ) ;
69+ await getWordPressVersions ( mockCore , 3 ) ;
7270
7371 expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( [ '6.4' , '6.3' , '6.2' ] ) ) ;
7472 } ) ;
7573
7674 it ( 'should filter out non-autoupdate versions' , async ( ) => {
77- mockCore . getInput . mockReturnValue ( '10' ) ;
78-
79- await getWordPressVersions ( ) ;
80-
81- const expectedVersions = [ '6.4' , '6.3' , '6.2' , '6.1' ] ;
82- expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( expectedVersions ) ) ;
83- } ) ;
84-
85- it ( 'should remove duplicate versions after patch removal' , async ( ) => {
86- const duplicateResponse = {
87- offers : [
88- { version : '6.4.2' , response : 'autoupdate' } ,
89- { version : '6.4.1' , response : 'autoupdate' } ,
90- { version : '6.4.0' , response : 'autoupdate' } ,
91- { version : '6.3.1' , response : 'autoupdate' } ,
92- ]
93- } ;
94-
9575 fetch . mockResolvedValue ( {
96- json : jest . fn ( ) . mockResolvedValue ( duplicateResponse ) ,
76+ json : jest . fn ( ) . mockResolvedValue ( mockApiResponse ) ,
9777 } ) ;
9878
99- mockCore . getInput . mockReturnValue ( '3' ) ;
79+ await getWordPressVersions ( mockCore , 10 ) ;
10080
101- await getWordPressVersions ( ) ;
102-
103- expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( [ '6.4' , '6.3' ] ) ) ;
81+ const expectedVersions = [ '6.4' , '6.3' , '6.2' , '6.1' , '5.9' , '5.8' ] ;
82+ expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( expectedVersions ) ) ;
10483 } ) ;
10584
10685 it ( 'should sort versions correctly' , async ( ) => {
@@ -117,17 +96,17 @@ describe('getWordPressVersions', () => {
11796 json : jest . fn ( ) . mockResolvedValue ( unsortedResponse ) ,
11897 } ) ;
11998
120- mockCore . getInput . mockReturnValue ( '4' ) ;
121-
122- await getWordPressVersions ( ) ;
99+ await getWordPressVersions ( mockCore , 4 ) ;
123100
124101 expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( [ '6.4' , '6.3' , '6.2' , '6.1' ] ) ) ;
125102 } ) ;
126103
127104 it ( 'should log the found versions' , async ( ) => {
128- mockCore . getInput . mockReturnValue ( '2' ) ;
105+ fetch . mockResolvedValue ( {
106+ json : jest . fn ( ) . mockResolvedValue ( mockApiResponse ) ,
107+ } ) ;
129108
130- await getWordPressVersions ( ) ;
109+ await getWordPressVersions ( mockCore , 2 ) ;
131110
132111 expect ( console . log ) . toHaveBeenCalledWith ( 'Found 2 WordPress versions:' , [ '6.4' , '6.3' ] ) ;
133112 } ) ;
@@ -137,9 +116,8 @@ describe('getWordPressVersions', () => {
137116 it ( 'should handle fetch errors' , async ( ) => {
138117 const fetchError = new Error ( 'Network error' ) ;
139118 fetch . mockRejectedValue ( fetchError ) ;
140- mockCore . getInput . mockReturnValue ( '3' ) ;
141119
142- await getWordPressVersions ( ) ;
120+ await getWordPressVersions ( mockCore , 3 ) ;
143121
144122 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( 'Failed to fetch WordPress versions: Network error' ) ;
145123 expect ( mockCore . setOutput ) . not . toHaveBeenCalled ( ) ;
@@ -149,9 +127,8 @@ describe('getWordPressVersions', () => {
149127 fetch . mockResolvedValue ( {
150128 json : jest . fn ( ) . mockRejectedValue ( new Error ( 'Invalid JSON' ) ) ,
151129 } ) ;
152- mockCore . getInput . mockReturnValue ( '3' ) ;
153130
154- await getWordPressVersions ( ) ;
131+ await getWordPressVersions ( mockCore , 3 ) ;
155132
156133 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( 'Failed to fetch WordPress versions: Invalid JSON' ) ;
157134 } ) ;
@@ -160,9 +137,8 @@ describe('getWordPressVersions', () => {
160137 fetch . mockResolvedValue ( {
161138 json : jest . fn ( ) . mockResolvedValue ( { someOtherData : 'test' } ) ,
162139 } ) ;
163- mockCore . getInput . mockReturnValue ( '3' ) ;
164140
165- await getWordPressVersions ( ) ;
141+ await getWordPressVersions ( mockCore , 3 ) ;
166142
167143 expect ( mockCore . setFailed ) . toHaveBeenCalledWith ( 'No version offers found in WordPress API response' ) ;
168144 } ) ;
@@ -171,9 +147,8 @@ describe('getWordPressVersions', () => {
171147 fetch . mockResolvedValue ( {
172148 json : jest . fn ( ) . mockResolvedValue ( { offers : [ ] } ) ,
173149 } ) ;
174- mockCore . getInput . mockReturnValue ( '3' ) ;
175150
176- await getWordPressVersions ( ) ;
151+ await getWordPressVersions ( mockCore , 3 ) ;
177152
178153 expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( [ ] ) ) ;
179154 } ) ;
@@ -192,27 +167,23 @@ describe('getWordPressVersions', () => {
192167 json : jest . fn ( ) . mockResolvedValue ( smallResponse ) ,
193168 } ) ;
194169
195- mockCore . getInput . mockReturnValue ( '5' ) ;
196-
197- await getWordPressVersions ( ) ;
170+ await getWordPressVersions ( mockCore , 5 ) ;
198171
199172 expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( [ '6.4' , '6.3' ] ) ) ;
200173 } ) ;
201174
202175 it ( 'should handle zero as input' , async ( ) => {
203- const mockApiResponse = {
176+ const mockResponse = {
204177 offers : [
205178 { version : '6.4.1' , response : 'autoupdate' } ,
206179 ]
207180 } ;
208181
209182 fetch . mockResolvedValue ( {
210- json : jest . fn ( ) . mockResolvedValue ( mockApiResponse ) ,
183+ json : jest . fn ( ) . mockResolvedValue ( mockResponse ) ,
211184 } ) ;
212185
213- mockCore . getInput . mockReturnValue ( '0' ) ;
214-
215- await getWordPressVersions ( ) ;
186+ await getWordPressVersions ( mockCore , 0 ) ;
216187
217188 expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( [ ] ) ) ;
218189 } ) ;
@@ -231,9 +202,7 @@ describe('getWordPressVersions', () => {
231202 json : jest . fn ( ) . mockResolvedValue ( complexVersionResponse ) ,
232203 } ) ;
233204
234- mockCore . getInput . mockReturnValue ( '4' ) ;
235-
236- await getWordPressVersions ( ) ;
205+ await getWordPressVersions ( mockCore , 4 ) ;
237206
238207 expect ( mockCore . setOutput ) . toHaveBeenCalledWith ( 'versions' , JSON . stringify ( [ '6.10' , '6.9' , '6.2' ] ) ) ;
239208 } ) ;
0 commit comments