Skip to content

Commit 9df4095

Browse files
committed
Flesh out tests
1 parent 3785ba0 commit 9df4095

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

packages/rtk-query-codegen-openapi/test/__snapshots__/cli.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export type User = {
241241
email?: string;
242242
password?: string;
243243
phone?: string;
244+
website?: string;
244245
/** User Status */
245246
userStatus?: number;
246247
};
@@ -488,6 +489,7 @@ export type User = {
488489
email?: string;
489490
password?: string;
490491
phone?: string;
492+
website?: string;
491493
/** User Status */
492494
userStatus?: number;
493495
};

packages/rtk-query-codegen-openapi/test/__snapshots__/generateEndpoints.test.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export type User = {
241241
email?: string;
242242
password?: string;
243243
phone?: string;
244+
website?: string;
244245
/** User Status */
245246
userStatus?: number;
246247
};
@@ -488,6 +489,7 @@ export type User = {
488489
email?: string;
489490
password?: string;
490491
phone?: string;
492+
website?: string;
491493
/** User Status */
492494
userStatus?: number;
493495
};
@@ -791,6 +793,7 @@ export type User = {
791793
email?: string | undefined;
792794
password?: string | undefined;
793795
phone?: string | undefined;
796+
website?: string | undefined;
794797
/** User Status */
795798
userStatus?: number | undefined;
796799
};
@@ -1196,6 +1199,7 @@ export type User = {
11961199
email?: string | undefined;
11971200
password?: string | undefined;
11981201
phone?: string | undefined;
1202+
website?: string | undefined;
11991203
/** User Status */
12001204
userStatus?: number | undefined;
12011205
};
@@ -1477,6 +1481,7 @@ export type User = {
14771481
email?: string | undefined;
14781482
password?: string | undefined;
14791483
phone?: string | undefined;
1484+
website?: string | undefined;
14801485
/** User Status */
14811486
userStatus?: number | undefined;
14821487
};
@@ -1776,6 +1781,7 @@ export type User = {
17761781
email?: string | undefined;
17771782
password?: string | undefined;
17781783
phone?: string | undefined;
1784+
website?: string | undefined;
17791785
/** User Status */
17801786
userStatus?: number | undefined;
17811787
};
@@ -2057,6 +2063,7 @@ export type User = {
20572063
email?: string | undefined;
20582064
password?: string | undefined;
20592065
phone?: string | undefined;
2066+
website?: string | undefined;
20602067
/** User Status */
20612068
userStatus?: number | undefined;
20622069
};
@@ -2329,6 +2336,7 @@ export type User = {
23292336
email?: string | undefined;
23302337
password?: string | undefined;
23312338
phone?: string | undefined;
2339+
website?: string | undefined;
23322340
/** User Status */
23332341
userStatus?: number | undefined;
23342342
};
@@ -2886,6 +2894,7 @@ export type User = {
28862894
email?: string | undefined;
28872895
password?: string | undefined;
28882896
phone?: string | undefined;
2897+
website?: string | undefined;
28892898
/** User Status */
28902899
userStatus?: number | undefined;
28912900
};
@@ -3513,6 +3522,7 @@ export type User = {
35133522
email?: string | undefined;
35143523
password?: string | undefined;
35153524
phone?: string | undefined;
3525+
website?: string | undefined;
35163526
/** User Status */
35173527
userStatus?: number | undefined;
35183528
};

packages/rtk-query-codegen-openapi/test/fixtures/petstore.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,11 @@
10281028
"pattern": "^\\+?[1-9]\\d{1,14}$",
10291029
"example": "12345"
10301030
},
1031+
"website": {
1032+
"type": "string",
1033+
"pattern": "^https?://[^\\s]+$",
1034+
"example": "https://example.com"
1035+
},
10311036
"userStatus": {
10321037
"type": "integer",
10331038
"pattern": "^[1-9]\\d{0,2}$",

packages/rtk-query-codegen-openapi/test/fixtures/petstore.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,10 @@ components:
705705
type: string
706706
pattern: '^\+?[1-9]\d{1,14}$'
707707
example: '12345'
708+
website:
709+
type: string
710+
pattern: '^https?://[^\s]+$'
711+
example: 'https://example.com'
708712
userStatus:
709713
type: integer
710714
pattern: '^[1-9]\d{0,2}$'

packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,32 @@ describe('regex constants', () => {
693693
expect(api).toContain(String.raw`/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/`);
694694
expect(api).toContain(String.raw`export const userPhonePattern = /^\+?[1-9]\d{1,14}$/`);
695695
});
696+
697+
it('should not export regex constants when outputRegexConstants is false', async () => {
698+
const api = await generateEndpoints({
699+
unionUndefined: true,
700+
apiFile: './fixtures/emptyApi.ts',
701+
schemaFile: resolve(__dirname, 'fixtures/petstore.json'),
702+
outputRegexConstants: false,
703+
});
704+
705+
expect(api).not.toContain('Pattern = /');
706+
expect(api).not.toContain('tagNamePattern');
707+
expect(api).not.toContain('userEmailPattern');
708+
expect(api).not.toContain('userPhonePattern');
709+
});
710+
711+
it('should properly escape forward slashes in patterns', async () => {
712+
const api = await generateEndpoints({
713+
unionUndefined: true,
714+
apiFile: './fixtures/emptyApi.ts',
715+
schemaFile: resolve(__dirname, 'fixtures/petstore.json'),
716+
outputRegexConstants: true,
717+
});
718+
719+
// The userWebsitePattern should have escaped forward slashes
720+
expect(api).toContain(String.raw`export const userWebsitePattern = /^https?:\/\/[^\s]+$/`);
721+
});
696722
});
697723

698724
describe('esmExtensions option', () => {

0 commit comments

Comments
 (0)