@@ -4,8 +4,6 @@ import { describe, expect, it } from 'vitest';
44import {
55 applyStrictMockReturnType ,
66 buildStrictMockTypeFileHeader ,
7- collectStrictMockSchemaNamesFromUsage ,
8- collectStrictMockSchemaTypeNames ,
97 dedupeStrictMockTypeDeclarations ,
108 getMockFactoryReturnType ,
119 getMockFactorySignatureParts ,
@@ -152,34 +150,26 @@ describe('mock-types', () => {
152150 strictSchemaTypeNames : [ 'Pet' ] ,
153151 } ;
154152
155- it ( 'hoists helpers and schema mock aliases once for concatenated operation mocks ' , ( ) => {
156- const perOp = ` ${ getStrictMockHelperTypeDeclarations ( ) } \n\n ${ getStrictMockTypeDeclaration ( 'Pet' ) } \n\nexport const getGetPetResponseMock = () => ({})` ;
157- const duplicated = ` ${ perOp } \n\n ${ perOp } \n\n ${ perOp } ` ;
153+ it ( 'prepends helpers and schema mock aliases once from structured type names ' , ( ) => {
154+ const body =
155+ 'export const getGetPetResponseMock = () => ({}) \n\nexport const getListPetsResponseMock = () => []' ;
158156
159- const result = dedupeStrictMockTypeDeclarations (
160- duplicated ,
161- strictOptions ,
162- ) ;
157+ const result = dedupeStrictMockTypeDeclarations ( body , strictOptions ) ;
163158
164159 expect ( result . match ( / e x p o r t t y p e K e y s W i t h N u l l / g) ?. length ) . toBe ( 1 ) ;
165160 expect (
166161 result . match ( / e x p o r t t y p e M o c k W i t h N u l l a b l e O v e r r i d e s / g) ?. length ,
167162 ) . toBe ( 1 ) ;
168163 expect ( result . match ( / e x p o r t t y p e P e t M o c k / g) ?. length ) . toBe ( 1 ) ;
164+ expect ( result . indexOf ( 'export type PetMock' ) ) . toBeLessThan (
165+ result . indexOf ( 'export const getGetPetResponseMock' ) ,
166+ ) ;
169167 expect ( result . match ( / e x p o r t c o n s t g e t G e t P e t R e s p o n s e M o c k / g) ?. length ) . toBe (
170- 3 ,
168+ 1 ,
171169 ) ;
172- } ) ;
173-
174- it ( 'strips invalid strict mock aliases for factory value imports' , ( ) => {
175- const invalid = `export type getPetMockMock = {
176- [K in keyof Required<getPetMock>]: NonNullable<Required<getPetMock>[K]>;
177- };\n\nexport const getListPetsResponseMock = () => []` ;
178-
179- const result = dedupeStrictMockTypeDeclarations ( invalid , strictOptions ) ;
180-
181- expect ( result ) . not . toContain ( 'getPetMockMock' ) ;
182- expect ( result ) . not . toContain ( 'Required<getPetMock>' ) ;
170+ expect (
171+ result . match ( / e x p o r t c o n s t g e t L i s t P e t s R e s p o n s e M o c k / g) ?. length ,
172+ ) . toBe ( 1 ) ;
183173 } ) ;
184174
185175 it ( 'is a no-op for non-strict mocks even when a schema is named WidgetMock' , ( ) => {
@@ -191,6 +181,16 @@ describe('mock-types', () => {
191181 expect ( result ) . not . toContain ( 'export type KeysWithNull' ) ;
192182 expect ( result ) . not . toContain ( 'Required<Widget>' ) ;
193183 } ) ;
184+
185+ it ( 'returns implementation unchanged when strict mode is on but no type names are provided' , ( ) => {
186+ const body = 'export const getGetPetResponseMock = () => ({})' ;
187+
188+ const result = dedupeStrictMockTypeDeclarations ( body , {
189+ mockOptions : { required : true , nonNullable : true } ,
190+ } ) ;
191+
192+ expect ( result ) . toBe ( body ) ;
193+ } ) ;
194194 } ) ;
195195
196196 describe ( 'getSchemaTypeNamesFromResponses' , ( ) => {
@@ -271,47 +271,4 @@ describe('mock-types', () => {
271271 expect ( header . match ( / e x p o r t t y p e P e t M o c k / g) ?. length ) . toBe ( 1 ) ;
272272 } ) ;
273273 } ) ;
274-
275- describe ( 'collectStrictMockSchemaTypeNames' , ( ) => {
276- it ( 'reads schema names from strict mock alias declarations' , ( ) => {
277- const names = collectStrictMockSchemaTypeNames (
278- getStrictMockTypeDeclaration ( 'Pet' ) ,
279- ) ;
280-
281- expect ( names ) . toEqual ( [ 'Pet' ] ) ;
282- } ) ;
283- } ) ;
284-
285- describe ( 'collectStrictMockSchemaNamesFromUsage' , ( ) => {
286- it ( 'collects schema names referenced by MockWithNullableOverrides factories' , ( ) => {
287- const names = collectStrictMockSchemaNamesFromUsage (
288- '(): MockWithNullableOverrides<Pet, O, PetMock> => ({}) as MockWithNullableOverrides<Pet, O, PetMock>;\nexport const getListPetsResponseMock = (): PetMock[] => []' ,
289- ) ;
290-
291- expect ( names ) . toEqual ( [ 'Pet' ] ) ;
292- } ) ;
293-
294- it ( 'does not treat a schema named WidgetMock as a strict alias usage' , ( ) => {
295- const names = collectStrictMockSchemaNamesFromUsage (
296- 'export const getGetWidgetResponseMock = (overrideResponse: Partial<WidgetMock> = {}) => ({})' ,
297- ) ;
298-
299- expect ( names ) . toEqual ( [ ] ) ;
300- } ) ;
301- } ) ;
302-
303- describe ( 'dedupeStrictMockTypeDeclarations with usage-only mocks' , ( ) => {
304- it ( 'hoists helpers when factories reference strict types without inline declarations' , ( ) => {
305- const body = `export const getGetPetResponseMock = (): MockWithNullableOverrides<Pet, O, PetMock> => ({}) as MockWithNullableOverrides<Pet, O, PetMock>;\nexport const getListPetsResponseMock = (): PetMock[] => []` ;
306-
307- const result = dedupeStrictMockTypeDeclarations ( body , {
308- mockOptions : { required : true , nonNullable : true } ,
309- strictSchemaTypeNames : [ 'Pet' ] ,
310- } ) ;
311-
312- expect ( result ) . toContain ( 'export type KeysWithNull' ) ;
313- expect ( result ) . toContain ( 'export type PetMock' ) ;
314- expect ( result . match ( / e x p o r t t y p e P e t M o c k / g) ?. length ) . toBe ( 1 ) ;
315- } ) ;
316- } ) ;
317274} ) ;
0 commit comments