Skip to content

Commit

Permalink
test: improve utils coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thaisguigon committed Feb 28, 2024
1 parent e0bf8e4 commit 81e362a
Show file tree
Hide file tree
Showing 28 changed files with 223 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const triggerDeleteRecordsOptimisticEffect = ({

const rootQueryCachedObjectRecordConnection = rootQueryCachedResponse;

const { fieldArguments: rootQueryVariables } =
const { fieldVariables: rootQueryVariables } =
parseApolloStoreFieldName<CachedObjectRecordQueryVariables>(
storeFieldName,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const triggerUpdateRecordOptimisticEffect = ({

const rootQueryConnection = rootQueryCachedResponse;

const { fieldArguments: rootQueryVariables } =
const { fieldVariables: rootQueryVariables } =
parseApolloStoreFieldName<CachedObjectRecordQueryVariables>(
storeFieldName,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createUploadLink } from 'apollo-upload-client';

import { renewToken } from '@/auth/services/AuthService';
import { AuthTokenPair } from '~/generated/graphql';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';
import { logDebug } from '~/utils/logDebug';

import { ApolloManager } from '../types/apolloManager.interface';
Expand Down Expand Up @@ -139,7 +139,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
isDebugMode ? logger : null,
retryLink,
httpLink,
].filter(assertNotNull),
].filter(isDefined),
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getLogoUrlFromDomainName } from '~/utils';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

export const mapFavorites = (favorites: any) => {
Expand Down Expand Up @@ -33,6 +32,6 @@ export const mapFavorites = (favorites: any) => {
position: favorite?.position,
};
})
.filter(assertNotNull)
.filter(isDefined)
.sort((a: any, b: any) => a.position - b.position);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { recordBoardColumnsFamilyStateScopeMap } from '@/object-record/record-bo
import { RecordBoardColumnDefinition } from '@/object-record/record-board/types/RecordBoardColumnDefinition';
import { createFamilySelectorScopeMap } from '@/ui/utilities/recoil-scope/utils/createFamilySelectorScopeMap';
import { guardRecoilDefaultValue } from '@/ui/utilities/recoil-scope/utils/guardRecoilDefaultValue';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

export const recordBoardColumnsFamilySelectorScopeMap =
createFamilySelectorScopeMap<RecordBoardColumnDefinition | undefined, string>(
Expand Down Expand Up @@ -57,7 +57,7 @@ export const recordBoardColumnsFamilySelectorScopeMap =
}),
);
})
.filter(assertNotNull);
.filter(isDefined);

const lastColumn = [...columns].sort(
(a, b) => b.position - a.position,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { isFieldSelect } from '@/object-record/record-field/types/guards/isField
import { isFieldSelectValue } from '@/object-record/record-field/types/guards/isFieldSelectValue';
import { isFieldText } from '@/object-record/record-field/types/guards/isFieldText';
import { isFieldUuid } from '@/object-record/record-field/types/guards/isFieldUuid';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

const isValueEmpty = (value: unknown) => !assertNotNull(value) || value === '';
const isValueEmpty = (value: unknown) => !isDefined(value) || value === '';

export const isFieldValueEmpty = ({
fieldDefinition,
Expand All @@ -46,7 +46,7 @@ export const isFieldValueEmpty = ({
}

if (isFieldSelect(fieldDefinition)) {
return isFieldSelectValue(fieldValue) && !assertNotNull(fieldValue);
return isFieldSelectValue(fieldValue) && !isDefined(fieldValue);
}

if (isFieldCurrency(fieldDefinition)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { SelectableList } from '@/ui/layout/selectable-list/components/Selectabl
import { MenuItem } from '@/ui/navigation/menu-item/components/MenuItem';
import { MenuItemSelect } from '@/ui/navigation/menu-item/components/MenuItemSelect';
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

import { EntityForSelect } from '../types/EntityForSelect';
import { RelationPickerHotkeyScope } from '../types/RelationPickerHotkeyScope';
Expand Down Expand Up @@ -55,7 +55,7 @@ export const SingleEntitySelectMenuItems = ({

const entitiesInDropdown = [selectedEntity, ...entitiesToSelect].filter(
(entity): entity is EntityForSelect =>
assertNotNull(entity) && isNonEmptyString(entity.name),
isDefined(entity) && isNonEmptyString(entity.name),
);

useScopedHotkeys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EntityForSelect } from '@/object-record/relation-picker/types/EntityFor
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
import { makeAndFilterVariables } from '@/object-record/utils/makeAndFilterVariables';
import { makeOrFilterVariables } from '@/object-record/utils/makeOrFilterVariables';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

type SearchFilter = { fieldNames: string[]; filter: string | number };

Expand Down Expand Up @@ -102,15 +102,11 @@ export const useFilteredSearchEntityQuery = ({
});

return {
selectedEntities: selectedRecords
.map(mappingFunction)
.filter(assertNotNull),
selectedEntities: selectedRecords.map(mappingFunction).filter(isDefined),
filteredSelectedEntities: filteredSelectedRecords
.map(mappingFunction)
.filter(assertNotNull),
entitiesToSelect: recordsToSelect
.map(mappingFunction)
.filter(assertNotNull),
.filter(isDefined),
entitiesToSelect: recordsToSelect.map(mappingFunction).filter(isDefined),
loading:
recordsToSelectLoading ||
filteredSelectedRecordsLoading ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useObjectNameSingularFromPlural } from '@/object-metadata/hooks/useObjectNameSingularFromPlural';
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

export const useFieldPreviewValue = ({
fieldName,
Expand All @@ -21,7 +21,7 @@ export const useFieldPreviewValue = ({
});

const firstRecordWithValue = records.find(
(record) => assertNotNull(record[fieldName]) && record[fieldName] !== '',
(record) => isDefined(record[fieldName]) && record[fieldName] !== '',
);

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import styled from '@emotion/styled';

import { RawData } from '@/spreadsheet-import/types';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

import { Column } from '../MatchColumnsStep';

Expand Down Expand Up @@ -39,7 +39,7 @@ export const UserTableColumn = <T extends string>({
entries,
}: UserTableColumnProps<T>) => {
const { header } = column;
const entry = entries.find(assertNotNull);
const entry = entries.find(isDefined);

return (
<StyledContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { MOBILE_VIEWPORT } from '@/ui/theme/constants/MobileViewport';
import { HotkeyScope } from '@/ui/utilities/hotkey/types/HotkeyScope';
import { VIEWS_DROPDOWN_ID } from '@/views/constants/ViewsDropdownId';
import { useViewBar } from '@/views/hooks/useViewBar';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

import { useViewScopedStates } from '../hooks/internal/useViewScopedStates';

Expand Down Expand Up @@ -161,7 +161,7 @@ export const ViewsDropdownButton = ({
handleDeleteViewButtonClick(event, view.id),
}
: null,
].filter(assertNotNull)}
].filter(isDefined)}
onClick={() => handleViewSelect(view.id)}
LeftIcon={IconList}
text={view.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useGenerateFindManyRecordsQuery } from '@/object-record/hooks/useGenera
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
import { ViewFilter } from '@/views/types/ViewFilter';
import { ViewFilterOperand } from '@/views/types/ViewFilterOperand';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

const filterQueryParamsSchema = z.object({
filter: z.record(
Expand Down Expand Up @@ -142,7 +142,7 @@ export const useFiltersFromQueryParams = () => {
},
),
)
).filter(assertNotNull);
).filter(isDefined);
},
[
apolloClient,
Expand Down
10 changes: 4 additions & 6 deletions packages/twenty-front/src/modules/views/hooks/useViewBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { useAvailableScopeIdOrThrow } from '@/ui/utilities/recoil-scope/scopes-i
import { ViewField } from '@/views/types/ViewField';
import { ViewFilter } from '@/views/types/ViewFilter';
import { ViewSort } from '@/views/types/ViewSort';
import { assertNotNull } from '~/utils/assert';
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
import { isDefined } from '~/utils/isDefined';

import { ViewScopeInternalContext } from '../scopes/scope-internal-context/ViewScopeInternalContext';
import { currentViewFieldsScopedFamilyState } from '../states/currentViewFieldsScopedFamilyState';
Expand Down Expand Up @@ -116,9 +116,7 @@ export const useViewBar = (props?: UseViewProps) => {
return;
}

const queriedViewFields = viewFields
.map((viewField) => viewField)
.filter(assertNotNull);
const queriedViewFields = viewFields.filter(isDefined);

if (isPersistingView) {
return;
Expand Down Expand Up @@ -173,7 +171,7 @@ export const useViewBar = (props?: UseViewProps) => {
definition: availableFilterDefinition,
};
})
.filter(assertNotNull);
.filter(isDefined);

if (!isDeeplyEqual(savedViewFilters, queriedViewFilters)) {
set(savedViewFiltersState, queriedViewFilters);
Expand Down Expand Up @@ -220,7 +218,7 @@ export const useViewBar = (props?: UseViewProps) => {
definition: availableSortDefinition,
};
})
.filter(assertNotNull);
.filter(isDefined);

if (!isDeeplyEqual(savedViewSorts, queriedViewSorts)) {
set(savedViewSortsState, queriedViewSorts);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BoardFieldDefinition } from '@/object-record/record-board-deprecated/types/BoardFieldDefinition';
import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

import { ViewField } from '../types/ViewField';

Expand Down Expand Up @@ -28,5 +28,5 @@ export const mapViewFieldsToBoardFieldDefinitions = (
}
: null;
})
.filter(assertNotNull);
.filter(isDefined);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata'
import { ColumnDefinition } from '@/object-record/record-table/types/ColumnDefinition';
import { mapArrayToObject } from '~/utils/array/mapArrayToObject';
import { moveArrayItem } from '~/utils/array/moveArrayItem';
import { assertNotNull } from '~/utils/assert';
import { isDefined } from '~/utils/isDefined';

import { ViewField } from '../types/ViewField';

Expand Down Expand Up @@ -48,7 +48,7 @@ export const mapViewFieldsToColumnDefinitions = ({
viewFieldId: viewField.id,
};
})
.filter(assertNotNull);
.filter(isDefined);

// No label identifier set for this object
if (!labelIdentifierFieldMetadataId) return columnDefinitionsFromViewFields;
Expand Down
19 changes: 0 additions & 19 deletions packages/twenty-front/src/utils/__tests__/assert.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { parseApolloStoreFieldName } from '../parseApolloStoreFieldName';

describe('parseApolloStoreFieldName', () => {
it('returns an empty object if string is not a valid store field name', () => {
const result = parseApolloStoreFieldName('////');
expect(result).toEqual({});
});

it('returns the field name and parsed variables if they exist', () => {
const result = parseApolloStoreFieldName('fieldName({"key":"value"})');
expect(result).toEqual({
fieldName: 'fieldName',
fieldVariables: { key: 'value' },
});
});

it('returns only the field name if the variables cannot be parsed', () => {
const result = parseApolloStoreFieldName('fieldName(notJson)');
expect(result).toEqual({
fieldName: 'fieldName',
});
});

it('returns only the field name if there are no variables', () => {
const result = parseApolloStoreFieldName('fieldName');
expect(result).toEqual({
fieldName: 'fieldName',
});
});
});
35 changes: 35 additions & 0 deletions packages/twenty-front/src/utils/__tests__/sort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { sortAsc, sortDesc, sortNullsFirst, sortNullsLast } from '../sort';

describe('sort', () => {
describe('sortNullsFirst', () => {
it('should sort nulls first', () => {
expect(sortNullsFirst(null, 'a')).toBe(-1);
expect(sortNullsFirst('a', null)).toBe(1);
expect(sortNullsFirst('a', 'a')).toBe(0);
});
});

describe('sortNullsLast', () => {
it('should sort nulls last', () => {
expect(sortNullsLast(null, 'a')).toBe(1);
expect(sortNullsLast('a', null)).toBe(-1);
expect(sortNullsLast('a', 'a')).toBe(0);
});
});

describe('sortAsc', () => {
it('should sort in ascending order', () => {
expect(sortAsc('a', 'b')).toBe(-1);
expect(sortAsc('b', 'a')).toBe(1);
expect(sortAsc('a', 'a')).toBe(0);
});
});

describe('sortDesc', () => {
it('should sort in descending order', () => {
expect(sortDesc('a', 'b')).toBe(1);
expect(sortDesc('b', 'a')).toBe(-1);
expect(sortDesc('a', 'a')).toBe(0);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { groupArrayItemsBy } from '../groupArrayItemsBy';

describe('groupArrayItemsBy', () => {
it('groups an array of objects by a computed key', () => {
// Given
const array = [
{ id: '1', type: 'fruit', value: 'apple' },
{ id: '2', type: 'fruit', value: 'banana' },
{ id: '3', type: 'vegetable', value: 'carrot' },
];
const computeGroupKey = ({ type }: (typeof array)[0]) => type;

// When
const result = groupArrayItemsBy(array, computeGroupKey);

// Then
expect(result).toEqual({
fruit: [
{ id: '1', type: 'fruit', value: 'apple' },
{ id: '2', type: 'fruit', value: 'banana' },
],
vegetable: [{ id: '3', type: 'vegetable', value: 'carrot' }],
});
});
});
Loading

0 comments on commit 81e362a

Please sign in to comment.