Skip to content

Commit

Permalink
fix(drawing): Fix React warning for missing key in DrawingList (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan authored Jan 22, 2021
1 parent 52f22ee commit e5c2cbc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
40 changes: 31 additions & 9 deletions src/store/annotations/__tests__/reducer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ import {
} from '../actions';

describe('store/annotations/reducer', () => {
const getAnnotation = (): AnnotationDrawing => {
return {
id: 'anno_1',
target: {
path_groups: [{ paths: [{ points: [] }], stroke: { color: '#000', size: 1 } }] as Array<PathGroup>,
type: 'drawing',
},
type: 'annotation',
} as AnnotationDrawing;
};

describe('setIsInitialized', () => {
test('should set isInitialized', () => {
const newState = reducer(state, setIsInitialized());
Expand All @@ -28,9 +39,27 @@ describe('store/annotations/reducer', () => {
expect(newState.allIds).toContain(payload.id);
expect(newState.byId.anno_1).toEqual(payload);
});

test('should set format drawing if drawing type annotation', () => {
const arg = {} as NewAnnotation;
const payload = getAnnotation();
const newState = reducer(state, createAnnotationAction.fulfilled(payload, 'test', arg));

expect(newState.allIds).toContain(payload.id);
expect(newState.byId.anno_1).toMatchObject({
target: {
path_groups: [
{
clientId: expect.any(String),
paths: [{ clientId: expect.any(String) }],
},
],
},
});
});
});

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

test('should format drawing when target is drawing type', () => {
const annotation = {
id: 'anno_1',
target: {
path_groups: [{ paths: [{ points: [] }], stroke: { color: '#000', size: 1 } }] as Array<PathGroup>,
type: 'drawing',
},
type: 'annotation',
} as AnnotationDrawing;
const annotation = getAnnotation();
const payload = { entries: [annotation], limit: 1000, next_marker: null } as APICollection<Annotation>;
const newState = reducer(state, fetchAnnotationsAction.fulfilled(payload, 'test', undefined));

Expand Down
2 changes: 1 addition & 1 deletion src/store/annotations/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const annotationsAllIds = createReducer<AnnotationsState['allIds']>([], builder
const annotationsById = createReducer<AnnotationsState['byId']>({}, builder =>
builder
.addCase(createAnnotationAction.fulfilled, (state, { payload }) => {
state[payload.id] = payload;
state[payload.id] = isDrawing(payload) ? formatDrawing(payload) : payload;
})
.addCase(removeAnnotationAction, (state, { payload: id }) => {
delete state[id];
Expand Down

0 comments on commit e5c2cbc

Please sign in to comment.