Skip to content

Commit

Permalink
Merge branch 'joshunrau-fix-swagger-options'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 19, 2023
2 parents cf245f9 + c980ca2 commit febbf7a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
65 changes: 65 additions & 0 deletions e2e/express.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,69 @@ describe('Express Swagger', () => {
expect(response.text.length).toBeGreaterThan(0);
});
});

describe('custom swagger options', () => {
const CUSTOM_CSS = 'body { background-color: hotpink !important }';
const CUSTOM_JS = '/foo.js';
const CUSTOM_JS_STR = 'console.log("foo")';
const CUSTOM_FAVICON = '/foo.ico';
const CUSTOM_SITE_TITLE = 'Foo';
const CUSTOM_CSS_URL = '/foo.css';

beforeEach(async () => {
const swaggerDocument = SwaggerModule.createDocument(
app,
builder.build()
);

SwaggerModule.setup('/', app, swaggerDocument, {
customCss: CUSTOM_CSS,
customJs: CUSTOM_JS,
customJsStr: CUSTOM_JS_STR,
customfavIcon: CUSTOM_FAVICON,
customSiteTitle: CUSTOM_SITE_TITLE,
customCssUrl: CUSTOM_CSS_URL
});

await app.init();
});

it('should contain the custom css string', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(CUSTOM_CSS);
});

it('should source the custom js url', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(`script src='${CUSTOM_JS}'></script>`);
});

it('should contain the custom js string', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(CUSTOM_JS_STR);
});

it('should contain the custom favicon', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(
`<link rel='icon' href='${CUSTOM_FAVICON}' />`
);
});

it('should contain the custom site title', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(`<title>${CUSTOM_SITE_TITLE}</title>`);
});

it('should include the custom stylesheet', async () => {
const response: Response = await request(app.getHttpServer()).get('/');
expect(response.text).toContain(
`<link href='${CUSTOM_CSS_URL}' rel='stylesheet'>`
);
});

afterEach(async () => {
await app.close();
});
});
});
78 changes: 78 additions & 0 deletions e2e/fastify.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,82 @@ describe('Fastify Swagger', () => {
expect(response.text.length).toBeGreaterThan(0);
});
});

describe('custom swagger options', () => {
const CUSTOM_CSS = 'body { background-color: hotpink !important }';
const CUSTOM_JS = '/foo.js';
const CUSTOM_JS_STR = 'console.log("foo")';
const CUSTOM_FAVICON = '/foo.ico';
const CUSTOM_SITE_TITLE = 'Foo';
const CUSTOM_CSS_URL = '/foo.css';

beforeEach(async () => {
const swaggerDocument = SwaggerModule.createDocument(
app,
builder.build()
);

SwaggerModule.setup('/custom', app, swaggerDocument, {
customCss: CUSTOM_CSS,
customJs: CUSTOM_JS,
customJsStr: CUSTOM_JS_STR,
customfavIcon: CUSTOM_FAVICON,
customSiteTitle: CUSTOM_SITE_TITLE,
customCssUrl: CUSTOM_CSS_URL
});

await app.init();
await app.getHttpAdapter().getInstance().ready();
});

it('should contain the custom css string', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(CUSTOM_CSS);
});

it('should source the custom js url', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(`script src='${CUSTOM_JS}'></script>`);
});

it('should contain the custom js string', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(CUSTOM_JS_STR);
});

it('should contain the custom favicon', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(
`<link rel='icon' href='${CUSTOM_FAVICON}' />`
);
});

it('should contain the custom site title', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(`<title>${CUSTOM_SITE_TITLE}</title>`);
});

it('should include the custom stylesheet', async () => {
const response: Response = await request(app.getHttpServer()).get(
'/custom'
);
expect(response.text).toContain(
`<link href='${CUSTOM_CSS_URL}' rel='stylesheet'>`
);
});

afterEach(async () => {
await app.close();
});
});
});
2 changes: 1 addition & 1 deletion lib/swagger-ui/swagger-ui.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { favIconHtml, htmlTemplateString, jsTemplateString } from './constants';
import { OpenAPIObject, SwaggerCustomOptions } from '../interfaces';
import { favIconHtml, htmlTemplateString, jsTemplateString } from './constants';
import { buildJSInitOptions } from './helpers';

/**
Expand Down

0 comments on commit febbf7a

Please sign in to comment.