test: improve utils coverage (#4230)
* test: improve utils coverage * refactor: review - rename isDefined to isNonNullable, update tests and return statement
This commit is contained in:
@ -4,7 +4,7 @@ import { ReadFieldFunction } from '@apollo/client/cache/core/types/common';
|
||||
import { CachedObjectRecordEdge } from '@/apollo/types/CachedObjectRecordEdge';
|
||||
import { OrderBy } from '@/object-metadata/types/OrderBy';
|
||||
import { OrderByField } from '@/object-metadata/types/OrderByField';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
import { isNonNullable } from '~/utils/isNonNullable';
|
||||
import { sortAsc, sortDesc, sortNullsFirst, sortNullsLast } from '~/utils/sort';
|
||||
|
||||
export const sortCachedObjectEdges = ({
|
||||
@ -31,7 +31,7 @@ export const sortCachedObjectEdges = ({
|
||||
orderByFieldName,
|
||||
recordFromCache,
|
||||
) ?? null;
|
||||
const isSubFieldFilter = isDefined(fieldValue) && !!orderBySubFieldName;
|
||||
const isSubFieldFilter = isNonNullable(fieldValue) && !!orderBySubFieldName;
|
||||
|
||||
if (!isSubFieldFilter) return fieldValue as string | number | null;
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@ import { ApolloCache, StoreObject } from '@apollo/client';
|
||||
|
||||
import { isCachedObjectRecordConnection } from '@/apollo/optimistic-effect/utils/isCachedObjectRecordConnection';
|
||||
import { CachedObjectRecordEdge } from '@/apollo/types/CachedObjectRecordEdge';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
import { isNonNullable } from '~/utils/isNonNullable';
|
||||
import { capitalize } from '~/utils/string/capitalize';
|
||||
|
||||
export const triggerAttachRelationOptimisticEffect = ({
|
||||
@ -43,7 +43,7 @@ export const triggerAttachRelationOptimisticEffect = ({
|
||||
__typename: sourceRecordTypeName,
|
||||
});
|
||||
|
||||
if (!isDefined(sourceRecordReference)) {
|
||||
if (!isNonNullable(sourceRecordReference)) {
|
||||
return targetRecordFieldValue;
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ import { CachedObjectRecord } from '@/apollo/types/CachedObjectRecord';
|
||||
import { CachedObjectRecordEdge } from '@/apollo/types/CachedObjectRecordEdge';
|
||||
import { CachedObjectRecordQueryVariables } from '@/apollo/types/CachedObjectRecordQueryVariables';
|
||||
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
import { isNonNullable } from '~/utils/isNonNullable';
|
||||
import { parseApolloStoreFieldName } from '~/utils/parseApolloStoreFieldName';
|
||||
|
||||
export const triggerDeleteRecordsOptimisticEffect = ({
|
||||
@ -38,7 +38,7 @@ export const triggerDeleteRecordsOptimisticEffect = ({
|
||||
|
||||
const rootQueryCachedObjectRecordConnection = rootQueryCachedResponse;
|
||||
|
||||
const { fieldArguments: rootQueryVariables } =
|
||||
const { fieldVariables: rootQueryVariables } =
|
||||
parseApolloStoreFieldName<CachedObjectRecordQueryVariables>(
|
||||
storeFieldName,
|
||||
);
|
||||
@ -68,7 +68,7 @@ export const triggerDeleteRecordsOptimisticEffect = ({
|
||||
|
||||
// TODO: same as in update, should we trigger DELETE ?
|
||||
if (
|
||||
isDefined(rootQueryVariables?.first) &&
|
||||
isNonNullable(rootQueryVariables?.first) &&
|
||||
cachedEdges?.length === rootQueryVariables.first
|
||||
) {
|
||||
return DELETE;
|
||||
|
||||
@ -9,7 +9,7 @@ import { CachedObjectRecordQueryVariables } from '@/apollo/types/CachedObjectRec
|
||||
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
||||
import { getEdgeTypename } from '@/object-record/cache/utils/getEdgeTypename';
|
||||
import { isRecordMatchingFilter } from '@/object-record/record-filter/utils/isRecordMatchingFilter';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
import { isNonNullable } from '~/utils/isNonNullable';
|
||||
import { parseApolloStoreFieldName } from '~/utils/parseApolloStoreFieldName';
|
||||
|
||||
// TODO: add extensive unit tests for this function
|
||||
@ -56,7 +56,7 @@ export const triggerUpdateRecordOptimisticEffect = ({
|
||||
|
||||
const rootQueryConnection = rootQueryCachedResponse;
|
||||
|
||||
const { fieldArguments: rootQueryVariables } =
|
||||
const { fieldVariables: rootQueryVariables } =
|
||||
parseApolloStoreFieldName<CachedObjectRecordQueryVariables>(
|
||||
storeFieldName,
|
||||
);
|
||||
@ -71,7 +71,7 @@ export const triggerUpdateRecordOptimisticEffect = ({
|
||||
const rootQueryOrderBy = rootQueryVariables?.orderBy;
|
||||
const rootQueryLimit = rootQueryVariables?.first;
|
||||
|
||||
const shouldTryToMatchFilter = isDefined(rootQueryFilter);
|
||||
const shouldTryToMatchFilter = isNonNullable(rootQueryFilter);
|
||||
|
||||
if (shouldTryToMatchFilter) {
|
||||
const updatedRecordMatchesThisRootQueryFilter =
|
||||
@ -101,7 +101,7 @@ export const triggerUpdateRecordOptimisticEffect = ({
|
||||
if (updatedRecordShouldBeAddedToRootQueryEdges) {
|
||||
const updatedRecordNodeReference = toReference(updatedRecord);
|
||||
|
||||
if (isDefined(updatedRecordNodeReference)) {
|
||||
if (isNonNullable(updatedRecordNodeReference)) {
|
||||
rootQueryNextEdges.push({
|
||||
__typename: objectEdgeTypeName,
|
||||
node: updatedRecordNodeReference,
|
||||
@ -115,7 +115,8 @@ export const triggerUpdateRecordOptimisticEffect = ({
|
||||
}
|
||||
}
|
||||
|
||||
const rootQueryNextEdgesShouldBeSorted = isDefined(rootQueryOrderBy);
|
||||
const rootQueryNextEdgesShouldBeSorted =
|
||||
isNonNullable(rootQueryOrderBy);
|
||||
|
||||
if (
|
||||
rootQueryNextEdgesShouldBeSorted &&
|
||||
@ -128,7 +129,7 @@ export const triggerUpdateRecordOptimisticEffect = ({
|
||||
});
|
||||
}
|
||||
|
||||
const shouldLimitNextRootQueryEdges = isDefined(rootQueryLimit);
|
||||
const shouldLimitNextRootQueryEdges = isNonNullable(rootQueryLimit);
|
||||
|
||||
// TODO: not sure that we should trigger a DELETE here, as it will trigger a network request
|
||||
// Is it the responsibility of this optimistic effect function to delete a root query that will trigger a network request ?
|
||||
|
||||
@ -12,7 +12,7 @@ import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
||||
import { ObjectRecordConnection } from '@/object-record/types/ObjectRecordConnection';
|
||||
import { FieldMetadataType } from '~/generated-metadata/graphql';
|
||||
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
import { isNonNullable } from '~/utils/isNonNullable';
|
||||
|
||||
export const triggerUpdateRelationsOptimisticEffect = ({
|
||||
cache,
|
||||
@ -36,7 +36,7 @@ export const triggerUpdateRelationsOptimisticEffect = ({
|
||||
}
|
||||
|
||||
const fieldDoesNotExist =
|
||||
isDefined(updatedSourceRecord) &&
|
||||
isNonNullable(updatedSourceRecord) &&
|
||||
!(fieldMetadataItemOnSourceRecord.name in updatedSourceRecord);
|
||||
|
||||
if (fieldDoesNotExist) {
|
||||
@ -87,7 +87,7 @@ export const triggerUpdateRelationsOptimisticEffect = ({
|
||||
? currentFieldValueOnSourceRecord.edges.map(
|
||||
({ node }) => node as CachedObjectRecord,
|
||||
)
|
||||
: [currentFieldValueOnSourceRecord].filter(isDefined);
|
||||
: [currentFieldValueOnSourceRecord].filter(isNonNullable);
|
||||
|
||||
const updatedFieldValueOnSourceRecordIsARecordConnection =
|
||||
isObjectRecordConnection(
|
||||
@ -100,10 +100,11 @@ export const triggerUpdateRelationsOptimisticEffect = ({
|
||||
? updatedFieldValueOnSourceRecord.edges.map(
|
||||
({ node }) => node as CachedObjectRecord,
|
||||
)
|
||||
: [updatedFieldValueOnSourceRecord].filter(isDefined);
|
||||
: [updatedFieldValueOnSourceRecord].filter(isNonNullable);
|
||||
|
||||
const shouldDetachSourceFromAllTargets =
|
||||
isDefined(currentSourceRecord) && targetRecordsToDetachFrom.length > 0;
|
||||
isNonNullable(currentSourceRecord) &&
|
||||
targetRecordsToDetachFrom.length > 0;
|
||||
|
||||
if (shouldDetachSourceFromAllTargets) {
|
||||
// TODO: see if we can de-hardcode this, put cascade delete in relation metadata item
|
||||
|
||||
@ -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 { isNonNullable } from '~/utils/isNonNullable';
|
||||
import { logDebug } from '~/utils/logDebug';
|
||||
|
||||
import { ApolloManager } from '../types/apolloManager.interface';
|
||||
@ -139,7 +139,7 @@ export class ApolloFactory<TCacheShape> implements ApolloManager<TCacheShape> {
|
||||
isDebugMode ? logger : null,
|
||||
retryLink,
|
||||
httpLink,
|
||||
].filter(assertNotNull),
|
||||
].filter(isNonNullable),
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user