Implemented view filter group CRUD hooks and utils (#10551)
This PR implements hooks and utils logic for handling CRUD and view filter group comparison. The main hook is useAreViewFilterGroupsDifferentFromRecordFilterGroups, like view filters and view sorts. Inside this hook we implement getViewFilterGroupsToCreate, getViewFilterGroupsToDelete and getViewFilterGroupsToUpdate. All of those come with their unit tests. In this PR we also introduce a new util to prevent nasty bugs happening when we compare undefined === null, This util is called compareStrictlyExceptForNullAndUndefined and it should replace every strict equality comparison between values that can be null or undefined (which we have a lot) This could be enforced by a custom ESLint rule, the autofix may also be implemented (maybe the util should be put in twenty-shared ?)
This commit is contained in:
@ -0,0 +1,49 @@
|
||||
import { compareStrictlyExceptForNullAndUndefined } from '~/utils/compareStrictlyExceptForNullAndUndefined';
|
||||
|
||||
describe('compareStrictlyExceptForNullAndUndefined', () => {
|
||||
it('should return true for undefined === null', () => {
|
||||
expect(compareStrictlyExceptForNullAndUndefined(undefined, null)).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return true for null === undefined', () => {
|
||||
expect(compareStrictlyExceptForNullAndUndefined(null, undefined)).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return true for undefined === undefined', () => {
|
||||
expect(compareStrictlyExceptForNullAndUndefined(undefined, undefined)).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
it('should return true for null === null', () => {
|
||||
expect(compareStrictlyExceptForNullAndUndefined(null, null)).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true for 2 === 2', () => {
|
||||
expect(compareStrictlyExceptForNullAndUndefined(2, 2)).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false for 2 === 3', () => {
|
||||
expect(compareStrictlyExceptForNullAndUndefined(2, 3)).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for undefined === 2', () => {
|
||||
expect(compareStrictlyExceptForNullAndUndefined(undefined, 2)).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for null === 2', () => {
|
||||
expect(compareStrictlyExceptForNullAndUndefined(null, 2)).toBe(false);
|
||||
});
|
||||
|
||||
it('should return false for 2 === "2"', () => {
|
||||
expect(compareStrictlyExceptForNullAndUndefined(2, '2')).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true for "2" === "2"', () => {
|
||||
expect(compareStrictlyExceptForNullAndUndefined('2', '2')).toBe(true);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,15 @@
|
||||
import { isDefined } from 'twenty-shared';
|
||||
import { Nullable } from 'twenty-ui';
|
||||
|
||||
// TODO: we should create a custom eslint rule that enforces the use of this function
|
||||
// instead of using the `===` operator where a and b are | undefined | null
|
||||
export const compareStrictlyExceptForNullAndUndefined = <A, B>(
|
||||
valueA: Nullable<A>,
|
||||
valueB: Nullable<B>,
|
||||
) => {
|
||||
if (!isDefined(valueA) && !isDefined(valueB)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return valueA === valueB;
|
||||
};
|
||||
Reference in New Issue
Block a user