Skip to content

Commit

Permalink
deletes query items, adds tests for new entity methods
Browse files Browse the repository at this point in the history
also adds changelog
  • Loading branch information
draganescu authored and adamziel committed May 21, 2020
1 parent 86b3a9e commit c059655
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/core-data/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

## 2.3.1 (2019-05-21)

### New Feature

- The `deleteEntityRecord` and `removeItems` actions have been added.
- Entities now support a `forceDelete` attribute to allow the API to delete things that do not go to trash.
- A `delete<entity.name>` helper is created for every registered entity.

## 2.3.0 (2019-05-21)

### New features
Expand Down
32 changes: 32 additions & 0 deletions packages/core-data/src/queried-data/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,36 @@ describe( 'reducer', () => {
queries: {},
} );
} );

it( 'deletes an item', () => {
const original = deepFreeze( {
items: {
1: { id: 1, name: 'abc' },
2: { id: 2, name: 'def' },
3: { id: 3, name: 'ghi' },
4: { id: 4, name: 'klm' },
},
queries: {
'': [ 1, 2, 3, 4 ],
's=a': [ 1, 3 ],
},
} );
const state = reducer( original, {
type: 'REMOVE_ITEMS',
query: [ 3 ],
items: [ 3 ],
} );

expect( state ).toEqual( {
items: {
1: { id: 1, name: 'abc' },
2: { id: 2, name: 'def' },
4: { id: 4, name: 'klm' },
},
queries: {
'': [ 1, 2, 4 ],
's=a': [ 1 ],
},
} );
} );
} );
46 changes: 46 additions & 0 deletions packages/core-data/src/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import {
editEntityRecord,
saveEntityRecord,
deleteEntityRecord,
removeItems,
receiveEntityRecords,
receiveUserPermission,
receiveAutosaves,
Expand All @@ -30,6 +32,50 @@ describe( 'editEntityRecord', () => {
} );
} );

describe( 'removeItems', () => {
it( 'builds an action object', () => {
const postIds = [ 1, 2, 3 ];
expect( removeItems( 'postType', 'post', postIds ) ).toEqual( {
type: 'REMOVE_ITEMS',
items: postIds,
kind: 'postType',
name: 'post',
invalidateCache: false,
} );
} );
} );

describe( 'deleteEntityRecord', () => {
it( 'triggers a DELETE request for an existing record', async () => {
const post = 10;
const entities = [
{ name: 'post', kind: 'postType', baseURL: '/wp/v2/posts' },
];
const fulfillment = deleteEntityRecord( 'postType', 'post', post );
fulfillment.next();

// delete start action
expect( fulfillment.next( entities ).value.type ).toBe(
'DELETE_ENTITY_RECORD_START'
);

// remove items
fulfillment.next();

// delete api call
const { value: apiFetchAction } = fulfillment.next();
expect( apiFetchAction.request ).toEqual( {
path: '/wp/v2/posts/10',
method: 'DELETE',
} );

// delete finish
expect( fulfillment.next().value.type ).toBe(
'DELETE_ENTITY_RECORD_FINISH'
);
} );
} );

describe( 'saveEntityRecord', () => {
it( 'triggers a POST request for a new record', async () => {
const post = { title: 'new post' };
Expand Down
25 changes: 24 additions & 1 deletion packages/core-data/src/utils/on-sub-key.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* External dependencies
*/
import { filter } from 'lodash';
/**
* Higher-order reducer creator which creates a combined reducer object, keyed
* by a property on the action object.
Expand All @@ -10,6 +14,8 @@ export const onSubKey = ( actionProperty ) => ( reducer ) => (
state = {},
action
) => {
const newState = { ...state };

// Retrieve subkey from action. Do not track if undefined; useful for cases
// where reducer is scoped by action shape.
const key = action[ actionProperty ];
Expand All @@ -24,8 +30,25 @@ export const onSubKey = ( actionProperty ) => ( reducer ) => (
return state;
}

if ( action.type === 'REMOVE_ITEMS' ) {
action.query.forEach( ( queryId ) => {
Object.keys( newState ).forEach( ( stateKey ) => {
newState[ stateKey ] = filter(
newState[ stateKey ],
( stateQueryId ) => {
return stateQueryId !== queryId;
}
);
} );
} );
}

if ( ! nextKeyState ) {
return newState;
}

return {
...state,
...newState,
[ key ]: nextKeyState,
};
};
Expand Down

0 comments on commit c059655

Please sign in to comment.