Skip to content

Commit

Permalink
adding tests and related fixes
Browse files Browse the repository at this point in the history
Not complete yet, just work done so far.  So test fails are expected still.
  • Loading branch information
nerrad committed Dec 11, 2018
1 parent d061a28 commit 28b1884
Show file tree
Hide file tree
Showing 35 changed files with 1,946 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export * from './create-relations-generators.js';
export * from './delete-entity-generators';
export * from './delete-relations-generators';
export * from './persist-entity-generators';
export * from './persist-relations-generators';
export * from './persist-relations-generators';
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ function* createRelations(
) {
relationName = pluralModelName( relationName );
const singularRelationName = singularModelName( relationName );
// warning currently has a bug whereby the first argument is skipped for
// string replacement.
if ( ! ( relationEntities instanceof Map ) ) {
warning(
false,
'Incoming relationEntities argument (%s) is expected to be an ' +
'instance of Map.',
relationEntities,
'Incoming relationEntities argument is expected to be an ' +
'instance of Map. It is not.',
);
return;
}
Expand All @@ -109,7 +110,8 @@ function* createRelations(
false,
'Incoming relation Entities do not contain BaseEntity instances ' +
'for the given relation model (%s)',
singularRelationName
'',
singularRelationName,
);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function* removeRelationForEntity(
) {
const singularRelationName = singularModelName( relationName );
const pluralRelationName = pluralModelName( relationName );
yield removeEntityById( singularRelationName, entityId );
yield removeEntityById( singularRelationName, relationEntityId );
yield removeRelatedEntities(
modelName,
entityId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import {
keyEntitiesByPrimaryKeyValue,
getEntityPrimaryKeyValues,
} from '@eventespresso/model';
import { isEmpty, keys } from 'lodash';
import { isEmpty, keys, isArray } from 'lodash';
import warning from 'warning';

/**
* Internal imports.
*/
import { fetch, select, dispatch } from '../../base-controls';
import {
getFactoryByModel,
resolveGetEntityByIdForIds
resolveGetEntityByIdForIds,
} from '../../base-resolvers';
import {
removeEntityById,
Expand All @@ -42,6 +43,13 @@ import { REDUCER_KEY as CORE_REDUCER_KEY } from '../constants';
function* persistEntityRecord( modelName, entity ) {
// if this is not a model entity or its not dirty then bail.
if ( ! isModelEntityOfModel( entity, modelName ) || entity.isClean ) {
warning(
false,
isModelEntityOfModel( entity, modelName ) ?
'The entity provided has no changes to persist.' :
'The provided entity is not a BaseEntity child for the ' +
'provided model'
);
return null;
}
const factory = yield getFactoryByModel( modelName );
Expand Down Expand Up @@ -115,14 +123,17 @@ function* persistForEntityIds( modelName, entityIds = [] ) {
modelName,
entityIds,
);
const retrievedIds = keys( entities );
const retrievedEntities = isArray( entities ) ?
keyEntitiesByPrimaryKeyValue( 'event', entities ) :
new Map();
const retrievedIds = Array.from( retrievedEntities.keys() );
const persistedEntities = {};
while ( retrievedIds.length > 0 ) {
const id = retrievedIds.shift();
const persistedEntity = yield dispatch(
CORE_REDUCER_KEY,
'persistEntityRecord',
[ modelName, entities[ id ] ]
[ modelName, retrievedEntities.get( id ) ]
);
if ( isModelEntityOfModel( persistedEntity, modelName ) ) {
persistedEntities[ persistedEntity.id ] = persistedEntity;
Expand All @@ -147,7 +158,7 @@ function* persistDeletesForModel( modelName ) {
while ( entityIds.length > 0 ) {
const entityId = entityIds.shift();
const success = yield fetch( {
path: applyQueryString( modelName ) + entityId,
path: applyQueryString( modelName ) + '/' + entityId,
data: { force: true },
method: 'DELETE',
} );
Expand Down Expand Up @@ -175,7 +186,7 @@ function* persistTrashesForModel( modelName ) {
while ( entityIds.length > 0 ) {
const entityId = entityIds.shift();
const success = yield fetch( {
path: applyQueryString( modelName ) + entityId,
path: applyQueryString( modelName ) + '/' + entityId,
method: 'DELETE',
} );
if ( success ) {
Expand All @@ -198,17 +209,25 @@ function* persistAllDeletes() {
CORE_REDUCER_KEY,
'getModelsQueuedForDelete'
);
let deletedIds = [],
trashedIds = [];
const deletedIds = {},
trashedIds = {};
while ( modelsForDelete.length > 0 ) {
deletedIds = yield persistDeletesForModel( modelsForDelete.shift() );
const modelForDelete = modelsForDelete.shift();
const idsDeleted = yield persistDeletesForModel( modelForDelete );
if ( ! isEmpty( idsDeleted ) ) {
deletedIds[ modelForDelete ] = idsDeleted;
}
}
const modelsForTrash = yield select(
CORE_REDUCER_KEY,
'getModelsQueuedForTrash'
);
while ( modelsForTrash.length > 0 ) {
trashedIds = yield persistTrashesForModel( modelsForTrash.shift() );
const modelForTrash = modelsForTrash.shift();
const idsTrashed = yield persistTrashesForModel( modelForTrash );
if ( ! isEmpty( idsTrashed ) ) {
trashedIds[ modelForTrash ] = idsTrashed;
}
}
return { deleted: deletedIds, trashed: trashedIds };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function* persistDeleteRelationsForModel( modelName ) {
function* persistRelationsForModel( modelName, addRelation = true ) {
const relationState = yield getRelationState( modelName, addRelation );
if ( isEmpty( relationState ) ) {
return;
return {};
}
const relationsPersisted = {};
const entityIds = keys( relationState );
Expand Down Expand Up @@ -267,7 +267,7 @@ function* persistRelationsForEntityIdAndRelationId(
// Even when ids have changed, this should catch any potential queued
// relation items for those things that got updated in state in a prior
// dispatch
removeDirtyRelations(
yield removeDirtyRelations(
relationName,
relationId,
modelName,
Expand Down Expand Up @@ -397,4 +397,6 @@ export {
persistRelationsForEntityIdAndRelation,
persistRelationsForEntityIdAndRelationId,
persistNewEntityAndRemoveDirtyRelations,
removeDirtyRelations,
getRelationState,
};
10 changes: 5 additions & 5 deletions assets/src/data/eventespresso/core/actions/receive-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal imports
*/
import { ACTION_TYPES } from './action-types';
const { types } = ACTION_TYPES.entities;
const { entities: types } = ACTION_TYPES;

/**
* Action for receiving entity records.
Expand All @@ -16,11 +16,11 @@ const { types } = ACTION_TYPES.entities;
* {
* type: string,
* modelName: string,
* entities: Object<number|string,Object>
* entities: Map
* }
* } An action object.
*/
function receiveEntityRecords( modelName, entities = {} ) {
function receiveEntityRecords( modelName, entities = new Map() ) {
return {
type: types.RECEIVE_ENTITY_RECORDS,
modelName,
Expand All @@ -32,7 +32,7 @@ function receiveEntityRecords( modelName, entities = {} ) {
* Same as receiveEntityRecords except incoming entities will replace any
* matching records (by ID) in the state.
* @param {string} modelName
* @param {Object<number|string,Object>} entities
* @param {Map} entities
* @return {
* {
* type: string,
Expand All @@ -41,7 +41,7 @@ function receiveEntityRecords( modelName, entities = {} ) {
* }
* } An action object.
*/
function receiveAndReplaceEntityRecords( modelName, entities = {} ) {
function receiveAndReplaceEntityRecords( modelName, entities = new Map() ) {
return {
type: types.RECEIVE_AND_REPLACE_ENTITY_RECORDS,
modelName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TYPE_QUEUE_RELATION_DELETE,
} from '../constants';
import { ACTION_TYPES } from './action-types';
const { types } = ACTION_TYPES.relations;
const { relations: types } = ACTION_TYPES;

/**
* Action creator for adding relation indexes for entities and their relations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal imports
*/
import { ACTION_TYPES } from './action-types';
const { types } = ACTION_TYPES.entities;
const { entities: types } = ACTION_TYPES;

/**
* Action creator for removing the entity from the state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
TYPE_QUEUE_RELATION_DELETE,
} from '../constants';
import { ACTION_TYPES } from './action-types';
const { types } = ACTION_TYPES.relations;
const { relations: types } = ACTION_TYPES;

/**
* Action creator for removing all indexed relations for a specific entity from
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* External dependencies
*/
import { isGenerator, isModelEntity } from '@eventespresso/validators';

/**
* Internal imports
*/
import { createEntity } from '../create-entities-generators';
import { eventFactory } from '../../../test/fixtures/base';
import { ACTION_TYPES } from '../action-types';

const { entities: types } = ACTION_TYPES;

describe( 'createEntity()', () => {
describe( 'yields with expected response', () => {
const TestEvent = { EVT_name: 'test event' };
let fulfillment;
const reset = () =>
fulfillment = createEntity( 'event', TestEvent );
it( 'yields a generator for retrieving an Event factory', () => {
reset();
const { value } = fulfillment.next();
expect( isGenerator( value ) ).toBe( true );
} );
it( 'yields null when invalid factory retrieved', () => {
const { value } = fulfillment.next();
expect( value ).toBe( null );
} );
it( 'yields expected action for valid factory retrieved', () => {
reset();
fulfillment.next();
const { value: action } = fulfillment.next( eventFactory );
expect( action.type ).toEqual( types.RECEIVE_ENTITY );
expect( isModelEntity( action.entity ) ).toBe( true );
expect( action.entity.EVT_name ).toEqual( 'test event' );
} );
it( 'returns entityInstance', () => {
const { value: event } = fulfillment.next();
expect( isModelEntity( event ) ).toBe( true );
expect( event.EVT_name ).toEqual( 'test event' );
} );
} );
} );
Loading

0 comments on commit 28b1884

Please sign in to comment.