Skip to content

Commit

Permalink
Sonarcloud code smell fixes part II
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalKinas committed Oct 30, 2023
1 parent d8bd002 commit ac74f0d
Show file tree
Hide file tree
Showing 42 changed files with 118 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ test.describe('Special permissions : ', () => {
await managerSiteActions.addSiteMember(sitePrivate, userCollaborator, Site.RoleEnum.SiteCollaborator);
await managerSiteActions.addSiteMember(sitePrivate, userDemoted, Site.RoleEnum.SiteManager);

await managerFileActions.uploadFileWithRename(TEST_FILES.DOCX.path, docLibId, testData.fileDocx.name);
fileDocxFavId = (await managerFileActions.uploadFileWithRename(TEST_FILES.DOCX.path, docLibId, testData.fileDocxFav.name)).entry.id;
await managerFileActions.uploadFileWithRename(TEST_FILES.DOCX.path, testData.fileDocx.name, docLibId);
fileDocxFavId = (await managerFileActions.uploadFileWithRename(TEST_FILES.DOCX.path, testData.fileDocxFav.name, docLibId)).entry.id;
await managerNodeActions.createFile(testData.file.name, docLibId);
fileFavId = (await managerNodeActions.createFile(testData.fileFav.name, docLibId)).entry.id;
fileDocxSharedId = (await managerFileActions.uploadFileWithRename(TEST_FILES.DOCX.path, docLibId, testData.fileDocxShared.name)).entry.id;
fileDocxSharedFavId = (await managerFileActions.uploadFileWithRename(TEST_FILES.DOCX.path, docLibId, testData.fileDocxSharedFav.name)).entry.id;
fileDocxSharedId = (await managerFileActions.uploadFileWithRename(TEST_FILES.DOCX.path, testData.fileDocxShared.name, docLibId)).entry.id;
fileDocxSharedFavId = (await managerFileActions.uploadFileWithRename(TEST_FILES.DOCX.path, testData.fileDocxSharedFav.name, docLibId)).entry.id;
fileSharedId = (await managerNodeActions.createFile(testData.fileShared.name, docLibId)).entry.id;
fileSharedFavId = (await managerNodeActions.createFile(testData.fileSharedFav.name, docLibId)).entry.id;
fileLockedId = (await managerNodeActions.createFile(testData.fileLocked.name, docLibId)).entry.id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ describe('SearchLibrariesQueryBuilderService', () => {
expect(builder.userQuery).toEqual('something');
});

it('should build query and raise an event on update', async () => {
it('should build query and raise an event on update', () => {
spyOn(builder, 'buildQuery').and.returnValue(query);

let eventArgs = null;
builder.updated.subscribe((args) => (eventArgs = args));

await builder.update();
builder.update();
expect(eventArgs).toBe(query);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ export class SearchLibrariesQueryBuilderService {

buildQuery(): LibrarySearchQuery {
const query = this.userQuery;
if (query && query.length > 1) {
if (query?.length > 1) {
return {
term: query,
opts: {
skipCount: this.paging && this.paging.skipCount,
maxItems: this.paging && this.paging.maxItems
skipCount: this.paging?.skipCount,
maxItems: this.paging?.maxItems
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class SearchLibrariesResultsComponent extends PageComponent implements On
}

getNumberOfResults() {
if (this.data && this.data.list && this.data.list.pagination) {
if (this.data?.list?.pagination) {
return this.data.list.pagination.totalItems;
}
return 0;
Expand All @@ -173,7 +173,7 @@ export class SearchLibrariesResultsComponent extends PageComponent implements On
}

navigateTo(node: SiteEntry) {
if (node && node.entry && node.entry.guid) {
if (node?.entry?.guid) {
this.store.dispatch(new NavigateLibraryAction(node.entry.guid));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class SearchResultsRowComponent implements OnInit, OnDestroy {

if (entry.id === node.id) {
entry.name = node.name;
entry.properties = Object.assign({}, node.properties);
entry.properties = { ...node.properties };

this.updateValues();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ describe('SearchComponent', () => {
});

it('should raise an error if search fails', fakeAsync(() => {
spyOn(queryBuilder['searchApi'], 'search').and.returnValue(
Promise.reject({
message: `{ "error": { "statusCode": 500 } } `
})
);
spyOn(queryBuilder['searchApi'], 'search').and.returnValue(Promise.reject(new Error('{ "error": { "statusCode": 500 } }')));

spyOn(queryBuilder, 'buildQuery').and.returnValue(searchRequest);
spyOn(store, 'dispatch').and.stub();
Expand All @@ -118,11 +114,7 @@ describe('SearchComponent', () => {
return key;
});

spyOn(queryBuilder['searchApi'], 'search').and.returnValue(
Promise.reject({
message: `{ "error": { "statusCode": 401 } } `
})
);
spyOn(queryBuilder['searchApi'], 'search').and.returnValue(Promise.reject(new Error('{ "error": { "statusCode": 401 } }')));

spyOn(queryBuilder, 'buildQuery').and.returnValue(searchRequest);
spyOn(store, 'dispatch').and.stub();
Expand All @@ -141,11 +133,7 @@ describe('SearchComponent', () => {
return key;
});

spyOn(queryBuilder['searchApi'], 'search').and.returnValue(
Promise.reject({
message: `{ "error": { "statusCode": 401 } } `
})
);
spyOn(queryBuilder['searchApi'], 'search').and.returnValue(Promise.reject(new Error('{ "error": { "statusCode": 401 } }')));

spyOn(queryBuilder, 'buildQuery').and.returnValue(searchRequest);
spyOn(store, 'dispatch').and.stub();
Expand Down Expand Up @@ -191,7 +179,7 @@ describe('SearchComponent', () => {
});

it('should format user input as cm:name if configuration not provided', () => {
const query = component.formatSearchQuery('hello', undefined);
const query = component.formatSearchQuery('hello');
expect(query).toBe(`(cm:name:"hello*")`);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export class SearchResultsComponent extends PageComponent implements OnInit {
}

onNodeDoubleClick(node: NodeEntry) {
if (node && node.entry) {
if (node?.entry) {
if (node.entry.isFolder) {
this.store.dispatch(new NavigateToFolder(node));
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('SharedLinkViewComponent', () => {
}));

it('should not update store on error', fakeAsync(() => {
spyGetSharedLink.and.returnValue(Promise.reject('error'));
spyGetSharedLink.and.returnValue(Promise.reject(new Error('error')));

fixture.detectChanges();
tick();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ describe('ActionDirective', () => {
it('should add active route class name', () => {
fixture.detectChanges();
router.navigateByUrl('/dummy');
// fixture.detectChanges();
expect(document.body.querySelector('#test-element').className.includes('active-link-class')).toBe(true);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class ActiveLinkDirective implements OnInit, AfterContentInit {
}

private render(routerUrl: string, actionUrl: string) {
if (routerUrl && routerUrl.substring(1).startsWith(actionUrl)) {
if (routerUrl?.substring(1).startsWith(actionUrl)) {
this.isLinkActive = true;
this.renderer.addClass(this.element.nativeElement, this.acaActiveLink);
} else {
Expand All @@ -82,6 +82,6 @@ export class ActiveLinkDirective implements OnInit, AfterContentInit {
}

private resolveUrl(item): string {
return (item.action && item.action.click && item.action.click.payload) || item.action.route;
return item.action?.click?.payload || item.action?.route;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class ExpansionPanelDirective implements OnInit, OnDestroy {
constructor(private store: Store<any>, private router: Router, private expansionPanel: MatExpansionPanel) {}

hasActiveLinks() {
if (this.acaExpansionPanel && this.acaExpansionPanel.children) {
if (this.acaExpansionPanel?.children) {
return this.acaExpansionPanel.children.some((child) => this.router.url.startsWith(child.url || child.action.payload));
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class MenuPanelDirective implements OnInit, OnDestroy {
constructor(private store: Store<any>, private router: Router) {}

hasActiveLinks() {
if (this.acaMenuPanel && this.acaMenuPanel.children) {
if (this.acaMenuPanel?.children) {
return this.acaMenuPanel.children.some((child) => this.router.url.startsWith(child.url || child.action.payload));
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class UserMenuComponent implements OnInit {

ngOnInit() {
this.getUserInfo();
if (this.data && this.data.items) {
if (this.data?.items) {
this.data.items.sort((a, b) => a.order - b.order);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('ToggleEditOfflineComponent', () => {

fixture.detectChanges();

expect(component.selection).toEqual(selection.file as any);
expect(component.selection).toEqual(selection.file);
});

it('should download content when node is locked', async () => {
Expand Down
14 changes: 7 additions & 7 deletions projects/aca-content/src/lib/services/node-actions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ export class NodeActionsService {
}

isEntryEntitiesArray(contentEntities: any[]): boolean {
if (contentEntities && contentEntities.length) {
const nonEntryNode = contentEntities.find((node) => !node || !node.entry || !(node.entry.nodeId || node.entry.id));
if (contentEntities?.length) {
const nonEntryNode = contentEntities.find((node) => !(node?.entry?.nodeId || node?.entry?.id));
return !nonEntryNode;
}
return false;
Expand All @@ -160,7 +160,7 @@ export class NodeActionsService {

if (nodeEntry.parentId) {
entryParentId = nodeEntry.parentId;
} else if (nodeEntry.path && nodeEntry.path.elements && nodeEntry.path.elements.length) {
} else if (nodeEntry.path?.elements?.length) {
entryParentId = nodeEntry.path.elements[nodeEntry.path.elements.length - 1].id;
}

Expand Down Expand Up @@ -255,7 +255,7 @@ export class NodeActionsService {

// todo: review this approach once 5.2.3 is out
private customizeBreadcrumb(node: Node) {
if (node && node.path && node.path.elements) {
if (node?.path?.elements) {
const elements = node.path.elements;

if (elements.length > 1) {
Expand Down Expand Up @@ -316,7 +316,7 @@ export class NodeActionsService {
}

isSiteContainer(node: Node): boolean {
if (node && node.aspectNames && node.aspectNames.length > 0) {
if (node?.aspectNames?.length > 0) {
return node.aspectNames.indexOf('st:siteContainer') >= 0;
}
return false;
Expand Down Expand Up @@ -658,7 +658,7 @@ export class NodeActionsService {
const folderMoveResponseData = this.flatten(next);
const foundError = folderMoveResponseData.find((node) => node instanceof Error);
// data might contain also items of form: { itemMoved, initialParentId }
const foundEntry = folderMoveResponseData.find((node) => (node.itemMoved && node.itemMoved.entry) || (node && node.entry));
const foundEntry = folderMoveResponseData.find((node) => node.itemMoved?.entry || node?.entry);

if (!foundError) {
// consider success if NONE of the items from the folder move response is an error
Expand All @@ -677,7 +677,7 @@ export class NodeActionsService {
return acc;
}, moveStatus);
} else {
if ((data.itemMoved && data.itemMoved.entry) || (data && data.entry)) {
if (data.itemMoved?.entry || data?.entry) {
moveStatus.succeeded.push(data);
} else {
moveStatus.failed.push(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,7 @@ describe('NodeTemplateService', () => {
}));

it('should raise an error when getNodeInfo fails', fakeAsync(() => {
spyOn(nodeTemplateService['searchApi'], 'search').and.returnValue(
Promise.reject({
message: `{ "error": { "statusCode": 404 } } `
})
);
spyOn(nodeTemplateService['searchApi'], 'search').and.returnValue(Promise.reject(new Error('{ "error": { "statusCode": 404 } }')));
spyOn(store, 'dispatch');

nodeTemplateService.selectTemplateDialog(fileTemplateConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class NodeTemplateService {
}

private transformNode(node: Node): Node {
if (node && node.path && node.path && node.path.elements instanceof Array) {
if (node?.path?.elements instanceof Array) {
node.path.elements = this.getPathElements(node);
}
return node;
Expand Down
6 changes: 3 additions & 3 deletions projects/aca-content/src/lib/store/effects/library.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class LibraryEffects {
.select(getAppSelection)
.pipe(take(1))
.subscribe((selection) => {
if (selection && selection.library) {
if (selection?.library) {
this.content.deleteLibrary(selection.library.entry.id);
}
});
Expand All @@ -84,7 +84,7 @@ export class LibraryEffects {
.select(getAppSelection)
.pipe(take(1))
.subscribe((selection) => {
if (selection && selection.library) {
if (selection?.library) {
this.content.leaveLibrary(selection.library.entry.id, action.configuration?.focusedElementOnCloseSelector);
}
});
Expand Down Expand Up @@ -138,7 +138,7 @@ export class LibraryEffects {
.select(getAppSelection)
.pipe(take(1))
.subscribe((selection) => {
if (selection && selection.library) {
if (selection?.library) {
const { id } = selection.library.entry;
const { title, description, visibility } = action.payload;

Expand Down
Loading

0 comments on commit ac74f0d

Please sign in to comment.