Skip to content

Commit 7643198

Browse files
author
Igor Canedo
committed
fix case wherer entityRange exists but entityMap is undefined or emptyObject
1 parent ba7c0fe commit 7643198

File tree

7 files changed

+81
-14
lines changed

7 files changed

+81
-14
lines changed
File renamed without changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2017, Globo.com (https://github.com/globocom)
3+
*
4+
* License: MIT
5+
*/
6+
7+
/* eslint-env jest */
8+
9+
import isEmptyObject from '../../../src/helpers/isEmptyObject';
10+
11+
it('returns true if object is empty', () => {
12+
const mock = {};
13+
const result = isEmptyObject(mock);
14+
expect(result).toEqual(true);
15+
});
16+
17+
it('returns false if object is empty', () => {
18+
const mock = { a: 1 };
19+
const result = isEmptyObject(mock);
20+
expect(result).toEqual(false);
21+
});

sample/__tests__/loadAttributes.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ it('have correct length with inlineStyles, entityMap and text', () => {
129129
};
130130
const result = loadAttributes(params);
131131
expect(result).toHaveLength(4);
132+
const typeOfFunc = typeof result[0].props.onPress;
133+
expect(typeOfFunc).toBe('function');
132134
});
133135

134136
it('have correct length with multiple inlineStyles and text with substring without style', () => {
@@ -266,3 +268,41 @@ it('have inlineStyles with substring and type is given without lineHeight custom
266268
expect(result).toHaveLength(2);
267269
expect(result[0].props.children).toBe(params.text);
268270
});
271+
272+
it('have entityRanges but undefined entityMap', () => {
273+
const params = {
274+
text: 'Hello World',
275+
inlineStyles: [],
276+
entityMap: undefined,
277+
entityRanges: [{
278+
offset: 0,
279+
length: 5,
280+
key: 0,
281+
}],
282+
type: 'unstyled',
283+
};
284+
const result = loadAttributes(params);
285+
expect(result).toHaveLength(2);
286+
287+
const typeOfFunc = typeof result[0].props.onPress;
288+
expect(typeOfFunc).toBe('undefined');
289+
});
290+
291+
it('have entityRanges but empty object entityMap', () => {
292+
const params = {
293+
text: 'Hello World',
294+
inlineStyles: [],
295+
entityMap: {},
296+
entityRanges: [{
297+
offset: 0,
298+
length: 5,
299+
key: 0,
300+
}],
301+
type: 'unstyled',
302+
};
303+
const result = loadAttributes(params);
304+
expect(result).toHaveLength(2);
305+
306+
const typeOfFunc = typeof result[0].props.onPress;
307+
expect(typeOfFunc).toBe('undefined');
308+
});

sample/src/resourceMock.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,5 @@
321321
}
322322
],
323323
"entityMap": {
324-
"0": {
325-
"type": "LINK",
326-
"mutability": "MUTABLE",
327-
"data": {
328-
"url": "https://github.com/globocom/react-native-draftjs-render"
329-
}
330-
}
331324
}
332325
}

src/components/types.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type BlockQuotePropsType = {
1313
customStyles?: Object,
1414
inlineStyles: Array<Object>,
1515
entityRanges: Array<Object>,
16-
entityMap: Object,
16+
entityMap: ?Object,
1717
textProps: ?Object,
1818
};
1919

@@ -24,7 +24,7 @@ export type DraftJsTextPropsType = {
2424
customStyles?: Object,
2525
inlineStyles: Array<Object>,
2626
entityRanges: Array<Object>,
27-
entityMap: Object,
27+
entityMap: ?Object,
2828
navigate?: Function,
2929
textProps: ?Object,
3030
};
@@ -36,7 +36,7 @@ export type OrderedListItemPropsType = {
3636
customStyles?: Object,
3737
inlineStyles: Array<Object>,
3838
entityRanges: Array<Object>,
39-
entityMap: Object,
39+
entityMap: ?Object,
4040
counter: number,
4141
separator?: string,
4242
depth: number,
@@ -51,7 +51,7 @@ export type UnorderedListItemPropsType = {
5151
customStyles?: Object,
5252
inlineStyles: Array<Object>,
5353
entityRanges: Array<Object>,
54-
entityMap: Object,
54+
entityMap: ?Object,
5555
depth: number,
5656
defaultMarginLeft: number,
5757
textProps: ?Object,

src/helpers/isEmptyObject.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2017, Globo.com (https://github.com/globocom)
3+
*
4+
* License: MIT
5+
*/
6+
7+
// @flow
8+
9+
const isEmptyObject = (obj: Object): boolean => Object.keys(obj).length === 0;
10+
11+
export default isEmptyObject;

src/loadAttributes.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import defaultStyles from './components/defaultStyles';
1818
import generateKey from './utils/generateKey';
1919
import flatAttributesList from './flatAttributesList';
2020
import getItemType from './helpers/getItemType';
21+
import isEmptyObject from './helpers/isEmptyObject';
2122

22-
export const getItemOnPress = (item: Object, entityMap: Object, navigate: Function) => {
23-
if (item.key !== undefined) {
23+
export const getItemOnPress = (item: Object, entityMap: ?Object, navigate: Function) => {
24+
if (item.key !== undefined && entityMap && !isEmptyObject(entityMap)) {
25+
// $$FlowFixMe entityMap is valid here
2426
return () => { navigate(entityMap[item.key].data.url); };
2527
}
2628
return undefined;
@@ -32,7 +34,7 @@ type ParamsType = {
3234
customStyles?: Object,
3335
inlineStyles: Array<Object>,
3436
entityRanges: Array<Object>,
35-
entityMap: Object,
37+
entityMap: ?Object,
3638
navigate?: Function,
3739
textProps: ?Object,
3840
};

0 commit comments

Comments
 (0)