Skip to content

Commit

Permalink
feat(store): support afterId for EntityStore add and addWithReference…
Browse files Browse the repository at this point in the history
…s #INFR-2083
  • Loading branch information
why520crazy committed Jul 12, 2021
1 parent 8679d47 commit 8bfa5fb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/store/entity-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface EntityStoreOptions<TEntity = unknown, TReferences = unknown> {

export interface EntityAddOptions {
prepend?: boolean;
afterId?: Id;
// 如果是最后追加,自动跳转到最后一页
autoGotoLastPage?: boolean;
}
Expand Down
39 changes: 37 additions & 2 deletions src/store/test/entity-store-refs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ describe('Store: EntityStore with refs', () => {
it('should get 3 tasks and groups refs when add one user with new references', () => {
const newTaskEntity = {
_id: '3',
name: 'user 3',
department_id: 'dept-3'
title: 'task 3',
group_id: 'group-3'
};
const newGroup = {
_id: 'group-3',
Expand Down Expand Up @@ -270,6 +270,41 @@ describe('Store: EntityStore with refs', () => {
{ uid: '2', name: 'user 2' }
]);
});

it('should add tasks success to afterId', () => {
const newTaskEntity = {
_id: '3',
title: 'task 3',
group_id: 'group-3'
};
const newGroup = {
_id: 'group-3',
name: 'group-3 name'
};
tasksStore.addWithReferences(
newTaskEntity,
{
groups: [newGroup]
},
{
afterId: 'task-1'
}
);
const state = tasksStore.snapshot;
expect(state.entities).toEqual([
{ _id: 'task-1', title: 'task 1', group_id: 'group-1', created_by: '1' },
{
_id: '3',
title: 'task 3',
group_id: 'group-3'
},
{ _id: 'task-2', title: 'task 2', group_id: 'group-2', created_by: '2' }
]);
expect(state.references).toEqual({
groups: [...initialGroups, newGroup],
users: initialUsers
});
});
});

describe('update', () => {
Expand Down
37 changes: 28 additions & 9 deletions src/store/test/entity-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,56 @@ describe('Store: EntityStore', () => {
});
});

it('should get 3 users when add one task', () => {
it('should task success for append', () => {
const addEntity = {
_id: '3',
name: 'user 3'
name: 'task 3'
};
tasksEntityStore.add(addEntity);
const state = tasksEntityStore.snapshot;
expect(state.entities).toEqual([...initialTasks, addEntity]);
});

it('store add one entity prepend', () => {
const addUserEntity = {
it('should add task success for prepend=true', () => {
const addEntity = {
_id: '3',
name: 'user 3'
name: 'task 3'
};
tasksEntityStore.add(addUserEntity, {
tasksEntityStore.add(addEntity, {
prepend: true
});
const state = tasksEntityStore.snapshot;
expect(state.entities).toEqual([addUserEntity, ...initialTasks]);
expect(state.entities).toEqual([addEntity, ...initialTasks]);
});

it('should add task 3 success to afterId', () => {
const addEntity = {
_id: '3',
name: 'task 3'
};
tasksEntityStore.add(addEntity, {
afterId: '1'
});
const state = tasksEntityStore.snapshot;
expect(state.entities).toEqual([
{ _id: '1', name: 'task 1' },
{
_id: '3',
name: 'task 3'
},
{ _id: '2', name: 'task 2' }
]);
});

it('store add entities', () => {
const addEntities = [
{
_id: '3',
name: 'user 3'
name: 'task 3'
},
{
_id: '4',
name: 'user 4'
name: 'task 4'
}
];
const originalEntities = tasksEntityStore.snapshot.entities;
Expand Down

0 comments on commit 8bfa5fb

Please sign in to comment.