Skip to content

Commit

Permalink
fix: sonar issues (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
jer3k authored Apr 25, 2024
1 parent 2b82c2c commit daafbd3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 70 deletions.
9 changes: 8 additions & 1 deletion doc-gen-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion doc-gen-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"ts-jest": "^29.1.1",
"ts-loader": "^9.5.1",
"ts-node": "^10.4.0",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"wait-for-expect": "^3.0.2"
},
"jest": {
"moduleFileExtensions": [
Expand Down
38 changes: 17 additions & 21 deletions doc-gen-service/src/server.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Application } from 'express';
import EventEmitter from 'node:events';
import WaitFor from 'wait-for-expect';

jest.mock('./app', () => ({
app: {
Expand Down Expand Up @@ -41,9 +42,9 @@ const mock_initBrowser = jest.fn();
const mock_browserDisconnect = jest.fn();
jest.mock('./v1/services/puppeteer-service', () => ({
initBrowser: () => mock_initBrowser(),
browser: {
disconnect: () => mock_browserDisconnect(),
},
getBrowser: jest.fn().mockResolvedValue({
disconnect: mock_browserDisconnect,
}),
}));

const mock_loggerInfo = jest.fn();
Expand All @@ -59,11 +60,6 @@ jest.mock('./logger', () => ({
let configMap: any;
const mock_getConfig = jest.fn();

// jest.mock('./config', () => ({
// config: {
// get: jest.fn((key) => mock_getConfig(key)),
// },
// }));
jest.mock('./config', () => ({
config: {
get: (key) => {
Expand All @@ -74,7 +70,7 @@ jest.mock('./config', () => ({
describe('server', () => {
beforeEach(() => {
jest.resetModules();
})
});

describe('Start server on specified environment', () => {
describe('local', () => {
Expand Down Expand Up @@ -139,34 +135,34 @@ describe('server', () => {
configMap = {
'server:apiKey': 'api-key',
'server:port': 3000,
'environment': 'prod',
environment: 'prod',
};
});
it('should start listening on the port and use puppeteer', async () => {
mock_initBrowser.mockImplementation(() => Promise.resolve());
mock_browserDisconnect.mockImplementation(() => Promise.resolve());
mock_initBrowser.mockResolvedValue(0);
mock_browserDisconnect.mockResolvedValue(0);
mock_listen.mockImplementation((port) => {
expect(port).toBe(3000);
});
await require('./server');
expect(mock_initBrowser).toHaveBeenCalled();
expect(mock_browserDisconnect).toHaveBeenCalled();
await WaitFor(() => {
expect(mock_browserDisconnect).toHaveBeenCalled();
});
});
it('should handle initBrowser catch', async () => {
mock_initBrowser.mockImplementation(() => Promise.reject());
mock_browserDisconnect.mockImplementation(() => Promise.resolve());
jest.spyOn(process, 'exit').mockImplementation()
mock_initBrowser.mockResolvedValue(0);
mock_browserDisconnect.mockResolvedValue(0);
jest.spyOn(process, 'exit').mockImplementation();
await require('./server');
expect(mock_initBrowser).toHaveBeenCalled();
});
it('should handle browser disconnect catch', async () => {
mock_initBrowser.mockImplementation(() => Promise.resolve());
mock_browserDisconnect.mockImplementation(() => Promise.reject());
jest.spyOn(process, 'exit').mockImplementation()
mock_initBrowser.mockResolvedValue(0);
mock_browserDisconnect.mockResolvedValue(0);
jest.spyOn(process, 'exit').mockImplementation();
await require('./server');
});
});
});


});
30 changes: 18 additions & 12 deletions doc-gen-service/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import http from 'http';
import { config } from './config/index';
import { logger } from './logger';
import { app } from './app';
import { browser, initBrowser } from './v1/services/puppeteer-service';
import { getBrowser, initBrowser } from './v1/services/puppeteer-service';

// run inside `async` function
const port = config.get('server:port');
Expand All @@ -18,15 +18,22 @@ if (env === 'local') {
logger.info(`Running in ${env} environment`);
initBrowser()
.then(() => {
logger.info('Browser initialized')
browser
.disconnect()
.then(() => {
logger.info('Browser disconnected')
app.set('port', port);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
logger.info('Browser initialized');
getBrowser()
.then((browser) => {
browser
.disconnect()
.then(() => {
logger.info('Browser disconnected');
app.set('port', port);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
})
.catch((e) => {
logger.error(e);
process.exit(1);
});
})
.catch((e) => {
logger.error(e);
Expand All @@ -39,7 +46,6 @@ if (env === 'local') {
});
}


/**
* Event listener for HTTP server "error" event.
*/
Expand Down Expand Up @@ -96,7 +102,7 @@ process.on(
// Prevent unhandled promise errors from crashing application
process.on(
'unhandledRejection',
/* istanbul ignore next */
/* istanbul ignore next */
(err: Error) => {
if (err?.stack) {
logger.error(err);
Expand Down
2 changes: 1 addition & 1 deletion doc-gen-service/src/v1/services/puppeteer-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ async function getBrowser() {
return browser;
}

export { browser, initBrowser, getBrowser };
export { initBrowser, getBrowser };
51 changes: 17 additions & 34 deletions frontend/src/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,66 +31,49 @@

<v-spacer />

<template v-slot:append>
<template #append>
<v-menu
v-model="menu"
v-if="isAuthenticated"
v-model="menu"
:close-on-content-click="false"
location="bottom"
data-testid="header-account-menu"
>
<template v-slot:activator="{ props }">
<template #activator="{ props }">
<v-btn
id="header-account-button"
color="rgb(32, 31, 30)"
v-bind="props"
arial-label="User profile"
icon="mdi-account"
id="header-account-button"
data-testid="header-account-button"
title="User profile"
></v-btn>
</template>
<v-card
role="listbox"
aria-labelledby="header-account-button"
:titlex="userInfo?.displayName"
:subtitlex="userInfo?.legalName"
min-width="250"
>
<template v-slot:prepend>
<v-list role="presentation" aria-labelledby="header-account-button">
<v-list-item role="presentation" :link="false">
<v-list-item-title
data-testid="header-display-name"
class="styles-override"
role="menuitem"
:title="userInfo?.displayName"
:aria-label="userInfo?.displayName"
>{{ userInfo?.displayName }}</v-list-item-title
>
<v-list-item-subtitle
data-testid="header-legal-name"
class="styles-override"
role="menuitem"
:title="userInfo?.legalName"
:aria-label="userInfo?.legalName"
>{{ userInfo?.legalName }}</v-list-item-subtitle
>
</v-list-item>
</v-list>
</template>

<v-divider></v-divider>
<v-card-title data-testid="header-display-name">
{{ userInfo?.displayName }}
</v-card-title>
<v-card-subtitle data-testid="header-legal-name">
{{ userInfo?.legalName }}
</v-card-subtitle>

<v-divider class="mt-4" />

<v-card-actions>
<v-tooltip text="Logout" location="bottom">
<template v-slot:activator="{ props }">
<v-spacer></v-spacer>
<template #activator="{ props }">
<v-spacer />
<v-btn
v-bind="props"
icon="mdi-logout"
data-testid="header-logout-button"
@click="redirectToLogout"
title="Logout"
aria-label="Logout"
@click="redirectToLogout"
></v-btn>
</template>
</v-tooltip>
Expand Down

0 comments on commit daafbd3

Please sign in to comment.