Skip to content

Commit de4303d

Browse files
author
Raphael Freitas
committed
Add tests for loadAttributes.js
1 parent b6fc1e2 commit de4303d

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

sample/__tests__/loadAttributes.test.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,24 @@
66

77
/* eslint-env jest */
88

9-
import loadAttributes from '../../src/loadAttributes';
9+
import loadAttributes, { getItemOnPress } from '../../src/loadAttributes';
10+
11+
describe('getItemOnPress()', () => {
12+
test('returns the right function', () => {
13+
const navigate = jest.fn();
14+
const resultFn = getItemOnPress(
15+
{ key: 'test' },
16+
{ test: { data: { url: 'http://ok' } } },
17+
navigate,
18+
);
19+
resultFn();
20+
expect(navigate.mock.calls.length).toBe(1);
21+
});
22+
23+
test('returns the right function', () => {
24+
expect(getItemOnPress({})).toBe();
25+
});
26+
});
1027

1128
it('returns only text if other objects are empty', () => {
1229
const params = {
@@ -128,10 +145,40 @@ it('have correct length with multiple inlineStyles and text with substring witho
128145
length: 3,
129146
style: 'ITALIC',
130147
},
148+
{
149+
offset: 7,
150+
length: 3,
151+
style: 'LINK',
152+
},
131153
],
132-
entityMap: {},
154+
entityMap: {
155+
0: {
156+
type: 'LINK',
157+
mutability: 'MUTABLE',
158+
data: {
159+
url: 'https://github.com/globocom/react-native-draftjs-render',
160+
},
161+
},
162+
},
133163
entityRanges: [],
134164
};
135165
const result = loadAttributes(params);
136166
expect(result).toHaveLength(5);
167+
expect(result[4].props.children).toBe('d Hello World Hello World');
168+
});
169+
170+
it('have correct length with multiple inlineStyles and text with substring without style', () => {
171+
const params = {
172+
text: 'Hello World Hello World Hello World',
173+
inlineStyles: [{
174+
offset: 300,
175+
length: 2,
176+
style: 'BOLD',
177+
}],
178+
entityMap: {},
179+
entityRanges: [],
180+
};
181+
const result = loadAttributes(params);
182+
expect(result).toHaveLength(2);
183+
expect(result[0].props.children).toBe(params.text);
137184
});

src/loadAttributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import generateKey from './utils/generateKey';
1818
import flatAttributesList from './flatAttributesList';
1919
import getItemType from './helpers/getItemType';
2020

21-
const getItemOnPress = (item: Object, entityMap: Object, navigate: Function) => {
21+
export const getItemOnPress = (item: Object, entityMap: Object, navigate: Function) => {
2222
if (item.key !== undefined) {
2323
return () => { navigate(entityMap[item.key].data.url); };
2424
}

0 commit comments

Comments
 (0)