Skip to content

Commit 338bad6

Browse files
author
grafitto
committed
Added copy and tests
1 parent 4246055 commit 338bad6

File tree

4 files changed

+31
-147
lines changed

4 files changed

+31
-147
lines changed

packages/client/src/project/entities.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ export function getProjectEntities (projectId: string, apiClient: AxiosInstance)
1414
return wrapEntityArray(entities);
1515
},
1616
async put(entity: Entity): Promise<WrappedEntity> {
17-
const entityId = entity._id;
18-
delete entity._id;
19-
const expandedEntity = expandEntity(entity);
17+
const _entity = { ...entity }; // Make a shallow copy
18+
const entityId = _entity._id;
19+
delete _entity._id;
20+
const expandedEntity = expandEntity(_entity);
2021
const res = await apiClient.put<ApiEntity>(
2122
`/projects/${projectId}/entities/${entityId}`, expandedEntity);
2223
const returnedEntity = res.data;

packages/client/src/project/tests/__snapshots__/files.test.ts.snap

Lines changed: 0 additions & 132 deletions
This file was deleted.

packages/client/src/project/tests/__snapshots__/tags.test.ts.snap

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/client/src/project/tests/entities.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,32 @@ describe('Project Entities', () => {
113113
expect(entity).toMatchSnapshot();
114114
});
115115
});
116+
describe('put', () => {
117+
const userEntity = {
118+
data: {
119+
name: 'someone 1'
120+
},
121+
tags: []
122+
}
123+
let apiClient: AxiosInstance;
124+
async function callMockPut (entity: any): Promise<dataHelpers.WrappedEntity> {
125+
apiClient = api.getApiClient('http://baseurl', 'somekey');
126+
jest.spyOn(apiClient, 'put').mockReturnValue(Promise.resolve({ data:
127+
{ entity } }));
128+
jest.spyOn(dataHelpers, 'wrapEntity');
129+
const project = getProjectApi('project1', apiClient);
130+
const ent = await project.entities.put(entity);
131+
return ent;
132+
}
133+
it('should call PUT /projects/:pid/entities/:eid endpoint with entity', async () => {
134+
const entity = { ...userEntity };
135+
entity['_id'] = 'entity1';
136+
const expectedEntity = dataHelpers.expandEntity(entity);
137+
const frozen = Object.freeze(entity);
138+
await callMockPut(frozen);
139+
expect(apiClient.put).toHaveBeenCalledWith(
140+
'/projects/project1/entities/entity1', expectedEntity);
141+
});
142+
});
116143
});
117144
});

0 commit comments

Comments
 (0)