fix issues found by sonarqube (#8095)

* fix issues found by sonarqube

* fix unit test
This commit is contained in:
Elias Nahum 2024-07-18 09:40:47 +08:00 committed by GitHub
parent b330847d95
commit 5b510cb780
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 39 additions and 34 deletions

View file

@ -454,7 +454,7 @@ export async function fetchPostsSince(serverUrl: string, channelId: string, sinc
const isCRTEnabled = await getIsCRTEnabled(database);
const data = await client.getPostsSince(channelId, since, isCRTEnabled, isCRTEnabled);
const result = await processPostsFetched(data);
const result = processPostsFetched(data);
if (!fetchOnly) {
const models = await operator.handlePosts({
...result,

View file

@ -524,7 +524,7 @@ export const handleCallsSlashCommand = async (value: string, serverUrl: string,
}
case 'leave':
if (getCurrentCall()?.channelId === channelId) {
await leaveCall();
leaveCall();
return {handled: true};
}
return {

View file

@ -310,7 +310,7 @@ export const recordingAlert = (isHost: boolean, transcriptionsEnabled: boolean,
defaultMessage: 'Leave',
}),
onPress: async () => {
await leaveCall();
leaveCall();
},
style: 'destructive',
},

View file

@ -40,9 +40,9 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.COMMAND);
expect(result.bindings![0].app_id).toBe('test_app');
expect(result.bindings![0].label).toBeTruthy();
expect(result.bindings![0].location).toMatch(/location\/.+/);
expect(result!.bindings![0].app_id).toBe('test_app');
expect(result!.bindings![0].label).toBeTruthy();
expect(result!.bindings![0].location).toMatch(/location\/.+/);
});
it('should remove bindings without app_id', () => {
@ -70,8 +70,8 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.COMMAND);
expect(result.bindings).toHaveLength(1);
expect(result.bindings![0].app_id).toBe('sub_app');
expect(result!.bindings).toHaveLength(1);
expect(result!.bindings![0].app_id).toBe('sub_app');
});
it('should remove bindings with empty or whitespace labels', () => {
@ -109,8 +109,8 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.COMMAND);
expect(result.bindings).toHaveLength(1);
expect(result.bindings![0].label).toBe('valid_label');
expect(result!.bindings).toHaveLength(1);
expect(result!.bindings![0].label).toBe('valid_label');
});
it('should remove bindings with duplicate labels in COMMAND location', () => {
@ -143,8 +143,8 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.COMMAND);
expect(result.bindings).toHaveLength(1);
expect(result.bindings![0].app_id).toBe('sub_app1');
expect(result!.bindings).toHaveLength(1);
expect(result!.bindings![0].app_id).toBe('sub_app1');
});
it('should keep unique labels in IN_POST location', () => {
@ -177,7 +177,7 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.IN_POST);
expect(result.bindings).toHaveLength(2);
expect(result!.bindings).toHaveLength(2);
});
it('should remove invalid sub-bindings without form or submit', () => {
@ -213,9 +213,9 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.COMMAND);
expect(result.bindings).toHaveLength(2);
expect(result.bindings![0].label).toBe('sub_label2');
expect(result.bindings![1].label).toBe('sub_label3');
expect(result!.bindings).toHaveLength(2);
expect(result!.bindings![0].label).toBe('sub_label2');
expect(result!.bindings![1].label).toBe('sub_label3');
});
it('should handle forms correctly', () => {
@ -238,8 +238,8 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.COMMAND);
expect(result.bindings).toHaveLength(1);
expect(result.bindings![0].form).toBeTruthy();
expect(result!.bindings).toHaveLength(1);
expect(result!.bindings![0].form).toBeTruthy();
});
it('should handle submit calls correctly', () => {
@ -260,8 +260,8 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.COMMAND);
expect(result.bindings).toHaveLength(1);
expect(result.bindings![0].submit).toBeTruthy();
expect(result!.bindings).toHaveLength(1);
expect(result!.bindings![0].submit).toBeTruthy();
});
it('should recursively clean sub-bindings', () => {
@ -296,9 +296,9 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.COMMAND);
expect(result.bindings).toHaveLength(1);
expect(result.bindings![0].bindings).toHaveLength(1);
expect(result.bindings![0].bindings![0].label).toBe('sub_sub_label');
expect(result!.bindings).toHaveLength(1);
expect(result!.bindings![0].bindings).toHaveLength(1);
expect(result!.bindings![0].bindings![0].label).toBe('sub_sub_label');
});
it('should handle bindings without bindings, form, or submit', () => {
@ -316,7 +316,7 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.COMMAND);
expect(result.bindings).toHaveLength(0);
expect(result!.bindings).toHaveLength(0);
});
it('should handle multiple levels of nested bindings correctly', () => {
@ -361,10 +361,10 @@ describe('cleanBinding', () => {
};
const result = cleanBinding(binding, AppBindingLocations.COMMAND);
expect(result.bindings).toHaveLength(2);
expect(result.bindings![0].bindings).toHaveLength(1);
expect(result.bindings![0].bindings![0].label).toBe('sub_sub_label');
expect(result.bindings![1].label).toBe('sub_label2');
expect(result!.bindings).toHaveLength(2);
expect(result!.bindings![0].bindings).toHaveLength(1);
expect(result!.bindings![0].bindings![0].label).toBe('sub_sub_label');
expect(result!.bindings![1].label).toBe('sub_label2');
});
});

View file

@ -5,13 +5,13 @@ import {AppBindingLocations, AppCallResponseTypes, AppFieldTypes} from '@constan
import {generateId} from './general';
export function cleanBinding(binding: AppBinding, topLocation: string): AppBinding {
export function cleanBinding(binding: AppBinding, topLocation: string): AppBinding|null {
return cleanBindingRec(binding, topLocation, 0);
}
function cleanBindingRec(binding: AppBinding, topLocation: string, depth: number): AppBinding {
function cleanBindingRec(binding: AppBinding, topLocation: string, depth: number): AppBinding|null {
if (!binding) {
return binding;
return null;
}
const toRemove: number[] = [];

View file

@ -164,6 +164,10 @@ export function isMainActivity() {
return true;
}
function localeCompare(a: string, b: string) {
return a.localeCompare(b);
}
export function areBothStringArraysEqual(a: string[], b: string[]) {
if (a.length !== b.length) {
return false;
@ -173,8 +177,8 @@ export function areBothStringArraysEqual(a: string[], b: string[]) {
return false;
}
const aSorted = a.sort();
const bSorted = b.sort();
const aSorted = a.sort(localeCompare);
const bSorted = b.sort(localeCompare);
const areBothEqual = aSorted.every((value, index) => value === bSorted[index]);
return areBothEqual;

View file

@ -437,7 +437,8 @@ describe('UrlUtils', () => {
const onError = jest.fn();
const onSuccess = jest.fn();
await UrlUtils.tryOpenURL(url, onError, onSuccess);
UrlUtils.tryOpenURL(url, onError, onSuccess);
await TestHelper.wait(100);
expect(onError).not.toHaveBeenCalled();
expect(onSuccess).toHaveBeenCalledTimes(1);
});