1+ const { runExport } = require ( '../../../../../src/commands/export.js' )
2+ const api = require ( '../../../../../src/helpers/api.js' )
3+ const starting = require ( '../../../../../src/helpers/starting.js' )
4+ const exportsHelpers = require ( '../../../../../src/helpers/exports.js' )
5+ const common = require ( '../../../../../src/utils/common.js' )
6+ const cmdPrint = require ( '../../../../../src/utils/cmdPrint.js' )
7+ const legacy = require ( '../../../../../src/utils/legacy.js' )
8+
9+ const fs = require ( 'fs' )
10+ const getArgsMock = jest . fn ( ) ;
11+ jest . mock ( 'fs' )
12+
13+ const axios = require ( 'axios' )
14+ jest . mock ( 'axios' )
15+
16+ jest . mock ( '../../../../../src/utils/getArgs' , ( ) => jest . fn ( { } ) )
17+
18+ describe ( 'runExport' , ( ) => {
19+ afterEach ( ( ) => {
20+ jest . resetAllMocks ( )
21+ } )
22+
23+ test ( 'testId exists and test already generated' , async ( ) => {
24+ const apiKey = 'testKey'
25+ const testId = 'test1'
26+ getArgsMock . mockReturnValueOnce ( {
27+ pythagora_api_key : apiKey ,
28+ test_id : testId
29+ } ) ;
30+ // jest.mock('../../../../../src/utils/getArgs', () => ({
31+ // pythagora_api_key: apiKey,
32+ // test_id: testId
33+ // }))
34+ fs . readdirSync . mockReturnValue ( [ ] )
35+ fs . readFileSync . mockReturnValue ( JSON . stringify ( { [ testId ] : { testName : 'testName.test.js' } } ) )
36+ exportsHelpers . testExists . mockReturnValue ( true )
37+
38+ await runExport ( )
39+
40+ expect ( cmdPrint . logAndExit ) . toHaveBeenCalledWith ( `Test with id ${ testId } already generated, you can find it here: ./${ './tests/generated/testName.test.js' } . If you want to generate it again delete old one first.` )
41+ } )
42+
43+ test ( 'testId exists but test not found' , async ( ) => {
44+ const apiKey = 'testKey'
45+ const testId = 'notfound'
46+ getArgsMock . mockReturnValueOnce ( {
47+ pythagora_api_key : apiKey ,
48+ test_id : testId
49+ } ) ;
50+ // jest.mock('../../../../../src/utils/getArgs', () => ({
51+ // pythagora_api_key: apiKey,
52+ // test_id: testId
53+ // }))
54+ fs . readdirSync . mockReturnValue ( [ ] )
55+ fs . readFileSync . mockReturnValue ( '{}' )
56+ exportsHelpers . testExists . mockReturnValue ( false )
57+ common . getAllGeneratedTests . mockReturnValue ( [ ] )
58+
59+ await expect ( runExport ( ) ) . rejects . toThrow ( `Test with id ${ testId } not found` )
60+ } )
61+
62+ test ( 'testId not provided, OPTIONS method' , async ( ) => {
63+ const apiKey = 'testKey'
64+ const testId = undefined
65+ getArgsMock . mockReturnValueOnce ( {
66+ pythagora_api_key : apiKey ,
67+ test_id : testId
68+ } ) ;
69+ // jest.mock('../../../../../src/utils/getArgs', () => ({
70+ // pythagora_api_key: apiKey,
71+ // test_id: testId
72+ // }))
73+ fs . readdirSync . mockReturnValue ( [ ] )
74+ fs . readFileSync . mockReturnValue ( '{}' )
75+ common . getAllGeneratedTests . mockReturnValue ( [ { method : 'OPTIONS' , id : 'test1' } ] )
76+
77+ await runExport ( )
78+
79+ expect ( exportsHelpers . testExists ) . not . toHaveBeenCalled ( )
80+ expect ( legacy . convertOldTestForGPT ) . not . toHaveBeenCalled ( )
81+ expect ( api . isEligibleForExport ) . not . toHaveBeenCalled ( )
82+ expect ( exportsHelpers . exportTest ) . not . toHaveBeenCalled ( )
83+ expect ( cmdPrint . testEligibleForExportLog ) . not . toHaveBeenCalled ( )
84+ } )
85+
86+ test ( 'testId not provided, test already generated' , async ( ) => {
87+ const apiKey = 'testKey'
88+ const testId = undefined
89+ getArgsMock . mockReturnValueOnce ( {
90+ pythagora_api_key : apiKey ,
91+ test_id : testId
92+ } ) ;
93+ // jest.mock('../../../../../src/utils/getArgs', () => ({
94+ // pythagora_api_key: apiKey,
95+ // test_id: testId
96+ // }))
97+ fs . readdirSync . mockReturnValue ( [ ] )
98+ fs . readFileSync . mockReturnValue ( JSON . stringify ( { test1 : { testName : 'testName.test.js' } } ) )
99+ common . getAllGeneratedTests . mockReturnValue ( [ { method : 'GET' , id : 'test1' , endpoint : '/example' } ] )
100+ exportsHelpers . testExists . mockReturnValue ( true )
101+
102+ await runExport ( )
103+
104+ expect ( exportsHelpers . testExists ) . toHaveBeenCalledWith ( { testName : 'testName.test.js' } , 'test1' )
105+ expect ( legacy . convertOldTestForGPT ) . not . toHaveBeenCalled ( )
106+ expect ( api . isEligibleForExport ) . not . toHaveBeenCalled ( )
107+ expect ( exportsHelpers . exportTest ) . not . toHaveBeenCalled ( )
108+ expect ( cmdPrint . testEligibleForExportLog ) . not . toHaveBeenCalled ( )
109+ expect ( console . log ) . toHaveBeenCalledWith ( `Test with id test1 already generated, you can find it here: ./${ './tests/generated/testName.test.js' } .` )
110+ } )
111+
112+ test ( 'testId not provided, test not eligible for export' , async ( ) => {
113+ const apiKey = 'testKey'
114+ const testId = undefined
115+ const originalTest = { method : 'GET' , id : 'test1' , endpoint : '/example' }
116+ const convertedTest = { ...originalTest , testId : 'test1' }
117+ getArgsMock . mockReturnValueOnce ( {
118+ pythagora_api_key : apiKey ,
119+ test_id : testId
120+ } ) ;
121+ // jest.mock('../../../../../src/utils/getArgs', () => ({
122+ // pythagora_api_key: apiKey,
123+ // test_id: testId
124+ // }))
125+ fs . readdirSync . mockReturnValue ( [ ] )
126+ fs . readFileSync . mockReturnValue ( '{}' )
127+ common . getAllGeneratedTests . mockReturnValue ( [ originalTest ] )
128+ exportsHelpers . testExists . mockReturnValue ( false )
129+ legacy . convertOldTestForGPT . mockReturnValue ( convertedTest )
130+ api . isEligibleForExport . mockResolvedValue ( false )
131+
132+ await runExport ( )
133+
134+ expect ( exportsHelpers . testExists ) . toHaveBeenCalledWith ( { } , 'test1' )
135+ expect ( legacy . convertOldTestForGPT ) . toHaveBeenCalledWith ( originalTest )
136+ expect ( api . isEligibleForExport ) . toHaveBeenCalledWith ( convertedTest )
137+ expect ( exportsHelpers . exportTest ) . not . toHaveBeenCalled ( )
138+ expect ( cmdPrint . testEligibleForExportLog ) . toHaveBeenCalledWith ( '/example' , 'test1' , false )
139+ } )
140+
141+ test ( 'testId not provided, test eligible for export' , async ( ) => {
142+ const apiKey = 'testKey'
143+ const testId = undefined
144+ const originalTest = { method : 'GET' , id : 'test1' , endpoint : '/example' }
145+ const convertedTest = { ...originalTest , testId : 'test1' }
146+ getArgsMock . mockReturnValueOnce ( {
147+ pythagora_api_key : apiKey ,
148+ test_id : testId
149+ } ) ;
150+ // jest.mock('../../../../../src/utils/getArgs', () => ({
151+ // pythagora_api_key: apiKey,
152+ // test_id: testId
153+ // }))
154+ fs . readdirSync . mockReturnValue ( [ ] )
155+ fs . readFileSync . mockReturnValue ( '{}' )
156+ common . getAllGeneratedTests . mockReturnValue ( [ originalTest ] )
157+ exportsHelpers . testExists . mockReturnValue ( false )
158+ legacy . convertOldTestForGPT . mockReturnValue ( convertedTest )
159+ api . isEligibleForExport . mockResolvedValue ( true )
160+ exportsHelpers . exportTest . mockResolvedValue ( )
161+
162+ await runExport ( )
163+
164+ expect ( exportsHelpers . testExists ) . toHaveBeenCalledWith ( { } , 'test1' )
165+ expect ( legacy . convertOldTestForGPT ) . toHaveBeenCalledWith ( originalTest )
166+ expect ( api . isEligibleForExport ) . toHaveBeenCalledWith ( convertedTest )
167+ expect ( exportsHelpers . exportTest ) . toHaveBeenCalledWith ( originalTest , { } )
168+ } )
169+ } )
0 commit comments