Skip to content

Commit e5c2cbc

Browse files
author
Conrad Chan
authored
fix(drawing): Fix React warning for missing key in DrawingList (#676)
1 parent 52f22ee commit e5c2cbc

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/store/annotations/__tests__/reducer-test.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ import {
1111
} from '../actions';
1212

1313
describe('store/annotations/reducer', () => {
14+
const getAnnotation = (): AnnotationDrawing => {
15+
return {
16+
id: 'anno_1',
17+
target: {
18+
path_groups: [{ paths: [{ points: [] }], stroke: { color: '#000', size: 1 } }] as Array<PathGroup>,
19+
type: 'drawing',
20+
},
21+
type: 'annotation',
22+
} as AnnotationDrawing;
23+
};
24+
1425
describe('setIsInitialized', () => {
1526
test('should set isInitialized', () => {
1627
const newState = reducer(state, setIsInitialized());
@@ -28,9 +39,27 @@ describe('store/annotations/reducer', () => {
2839
expect(newState.allIds).toContain(payload.id);
2940
expect(newState.byId.anno_1).toEqual(payload);
3041
});
42+
43+
test('should set format drawing if drawing type annotation', () => {
44+
const arg = {} as NewAnnotation;
45+
const payload = getAnnotation();
46+
const newState = reducer(state, createAnnotationAction.fulfilled(payload, 'test', arg));
47+
48+
expect(newState.allIds).toContain(payload.id);
49+
expect(newState.byId.anno_1).toMatchObject({
50+
target: {
51+
path_groups: [
52+
{
53+
clientId: expect.any(String),
54+
paths: [{ clientId: expect.any(String) }],
55+
},
56+
],
57+
},
58+
});
59+
});
3160
});
3261

33-
describe('createAnnotationAction', () => {
62+
describe('fetchAnnotationsAction', () => {
3463
test('should set state when fulfilled', () => {
3564
const annotation = { id: 'anno_1', target: { type: 'region' }, type: 'annotation' };
3665
const payload = { entries: [annotation], limit: 1000, next_marker: null } as APICollection<Annotation>;
@@ -42,14 +71,7 @@ describe('store/annotations/reducer', () => {
4271
});
4372

4473
test('should format drawing when target is drawing type', () => {
45-
const annotation = {
46-
id: 'anno_1',
47-
target: {
48-
path_groups: [{ paths: [{ points: [] }], stroke: { color: '#000', size: 1 } }] as Array<PathGroup>,
49-
type: 'drawing',
50-
},
51-
type: 'annotation',
52-
} as AnnotationDrawing;
74+
const annotation = getAnnotation();
5375
const payload = { entries: [annotation], limit: 1000, next_marker: null } as APICollection<Annotation>;
5476
const newState = reducer(state, fetchAnnotationsAction.fulfilled(payload, 'test', undefined));
5577

src/store/annotations/reducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const annotationsAllIds = createReducer<AnnotationsState['allIds']>([], builder
3131
const annotationsById = createReducer<AnnotationsState['byId']>({}, builder =>
3232
builder
3333
.addCase(createAnnotationAction.fulfilled, (state, { payload }) => {
34-
state[payload.id] = payload;
34+
state[payload.id] = isDrawing(payload) ? formatDrawing(payload) : payload;
3535
})
3636
.addCase(removeAnnotationAction, (state, { payload: id }) => {
3737
delete state[id];

0 commit comments

Comments
 (0)