From d0b1309aeea0b45df88fc421a314d9f0a46340e4 Mon Sep 17 00:00:00 2001 From: anastasiia-developer Date: Tue, 10 May 2022 15:21:14 +0300 Subject: [PATCH] feat: downloadFileName --- README.md | 1 + .../DiscriminatorDropdown.test.tsx.snap | 10 ++++ src/services/RedocNormalizedOptions.ts | 3 ++ src/services/__tests__/models/ApiInfo.test.ts | 50 ++++++++++++++++++- src/services/models/ApiInfo.ts | 4 +- 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f8db285f..f33ed231 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,7 @@ You can use all of the following options with the standalone version of the { expect(info.downloadFileName).toEqual('openapi.json'); }); + test('should correctly populate default download file is undefined when using specUrl', () => { + parser = new OpenAPIParser( + { + openapi: '3.0.0', + info: { + description: 'Test description', + }, + } as any, + '/demo/openapi.yaml', + opts, + ); + + const info = new ApiInfoModel(parser); + expect(info.downloadFileName).toEqual(undefined); + }); + test('should correctly populate download file name', () => { + parser.spec = { + info: { + description: 'Test description', + }, + } as any; + + const opts = new RedocNormalizedOptions({ + downloadFileName: 'test.yaml', + }); + + const info = new ApiInfoModel(parser, opts); + expect(info.downloadFileName).toEqual('test.yaml'); + }); + + test('should correctly populate download link', () => { parser.spec = { openapi: '3.0.0', info: { @@ -87,7 +118,24 @@ describe('Models', () => { downloadDefinitionUrl: 'https:test.com/filename.yaml', }); const info = new ApiInfoModel(parser, opts); - expect(info.downloadFileName).toEqual(undefined); + expect(info.downloadLink).toEqual('https:test.com/filename.yaml'); + }); + + test('should correctly populate download link and download file name', () => { + parser.spec = { + openapi: '3.0.0', + info: { + description: 'Test description', + }, + } as any; + + const opts = new RedocNormalizedOptions({ + downloadDefinitionUrl: 'https:test.com/filename.yaml', + downloadFileName: 'test.yaml', + }); + const info = new ApiInfoModel(parser, opts); + expect(info.downloadLink).toEqual('https:test.com/filename.yaml'); + expect(info.downloadFileName).toEqual('test.yaml'); }); }); }); diff --git a/src/services/models/ApiInfo.ts b/src/services/models/ApiInfo.ts index b5d71802..0c4d91eb 100644 --- a/src/services/models/ApiInfo.ts +++ b/src/services/models/ApiInfo.ts @@ -52,8 +52,8 @@ export class ApiInfoModel implements OpenAPIInfo { private getDownloadFileName(): string | undefined { if (!this.parser.specUrl && !this.options.downloadDefinitionUrl) { - return 'openapi.json'; + return this.options.downloadFileName || 'openapi.json'; } - return undefined; + return this.options.downloadFileName; } }