Skip to content

Commit 81c6eb8

Browse files
(test) improve unit tests
1 parent 3c58eb4 commit 81c6eb8

File tree

5 files changed

+290
-263
lines changed

5 files changed

+290
-263
lines changed

src/components/suggestionBuilder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import jp from 'jsonpath/jsonpath.min';
22

3-
const suggestions = [
3+
export const suggestions = [
44
{
55
'description': 'pick a value in a collection',
66
'value': '[]',

tests/JsonPathPreviewer-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ describe('Json path preview component', () => {
7373
expect(taggedJSON).toContain(highlightingTags.start + defaultJson.store.book[0].category + highlightingTags.end);
7474
});
7575

76+
it ('should tag all books', () => {
77+
const wraper = mount(<Previewer json={defaultJson} jsonPath='$..book' />);
78+
const paths = wraper.instance().evalJsonPath(defaultJson, '$..book');
79+
const taggedJSON = wraper.instance().tagPartOfJsonToHighlight(defaultJson, paths);
80+
81+
expect(taggedJSON).toContain('£CR££INC£££TAGGED£[£CR££INC£{£CR££INC£"category": "reference",£CR£"author": "Nigel Rees",£CR£"title": "Sayings of the Century",£CR£"price": 8.95£DEC££CR£},£CR£{£CR££INC£"category": "fiction",£CR£"author": "Evelyn Waugh",£CR£"title": "Sword of Honour",£CR£"price": 12.99£DEC££CR£},£CR£{£CR££INC£"category": "fiction",£CR£"author": "Herman Melville",£CR£"title": "Moby Dick",£CR£"isbn": "0-553-21311-3",£CR£"price": 8.99£DEC££CR£},£CR£{£CR££INC£"category": "fiction",£CR£"author": "J. R. R. Tolkien",£CR£"title": "The Lord of the Rings",£CR£"isbn": "0-395-19395-8",£CR£"price": 22.99£DEC££CR£}£DEC££CR£]£TAGGED£££DEC£');
82+
});
83+
7684
it ('should not tag anything', () => {
7785
const wraper = mount(<Previewer json={defaultJson} jsonPath='$..books' />);
7886
const paths = wraper.instance().evalJsonPath(defaultJson, '$..books');

tests/SuggestionBuilder-test.js

Lines changed: 27 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import expect from 'expect';
2-
import { getSuggestions, evalAllProperties } from '../src/components/suggestionBuilder';
2+
import { getSuggestions, evalAllProperties, suggestions } from '../src/components/suggestionBuilder';
33

44

55
describe('get suggestion', () => {
@@ -35,111 +35,23 @@ describe('get suggestion', () => {
3535
// When
3636
const suggestion = getSuggestions('$', 2, { test: 0 });
3737
// Then
38-
expect(suggestion).toEqual([
39-
{
40-
description: 'access a specific property',
41-
scopes: [
42-
'array',
43-
'object'
44-
],
45-
value: '.'
46-
},
47-
{
48-
description: 'search recursively for a property',
49-
scopes: [
50-
'array',
51-
'object'
52-
],
53-
value: '..'
54-
},
55-
{
56-
description: 'get all values',
57-
value: '.*',
58-
scopes: ['array', 'object']
59-
},
60-
]);
38+
expect(suggestion).toEqual(suggestions.filter(sug => sug.scopes.includes('object')));
6139
});
6240

6341
it('should lists array suggestions', () => {
6442
// Given
6543
// When
6644
const suggestion = getSuggestions('$', 2, [0, 1]);
6745
// Then
68-
expect(suggestion).toEqual([
69-
{
70-
description: 'pick a value in a collection',
71-
scopes: [
72-
'array'
73-
],
74-
setCarretAt: 1,
75-
value: '[]'
76-
},
77-
{
78-
description: 'get collection size',
79-
scopes: [
80-
'array'
81-
],
82-
value: '.length'
83-
},
84-
{
85-
description: 'access a specific property',
86-
scopes: [
87-
'array',
88-
'object'
89-
],
90-
value: '.'
91-
},
92-
{
93-
description: 'search recursively for a property',
94-
scopes: [
95-
'array',
96-
'object'
97-
],
98-
value: '..'
99-
},
100-
{
101-
description: 'get all values',
102-
value: '.*',
103-
scopes: ['array', 'object']
104-
},
105-
]);
46+
expect(suggestion).toEqual(suggestions.filter(sug => sug.scopes.includes('array')));
10647
});
10748

10849
it('should lists pick suggestions', () => {
10950
// Given
11051
// When
11152
const suggestion = getSuggestions('$.test[]', 7, { test: [0, 1] });
11253
// Then
113-
expect(suggestion).toEqual([
114-
{
115-
description: 'filter a collection',
116-
value: '?(@)',
117-
setCarretAt: 3,
118-
scopes: ['[]']
119-
},
120-
{
121-
description: 'select an item by it\'s index relatively to the size of the collection',
122-
value: '(@.length-1)',
123-
setCarretAt: 10,
124-
scopes: ['[]']
125-
},
126-
{
127-
description: 'select a range of item by their indexes',
128-
value: '0:1',
129-
setCarretAt: 0,
130-
scopes: ['[]']
131-
},
132-
{
133-
description: 'retrieves last item in a collection',
134-
value: '-1:',
135-
scopes: ['[]']
136-
},
137-
{
138-
description: 'get all values',
139-
value: '*',
140-
scopes: ['[]', '.']
141-
}
142-
]);
54+
expect(suggestion).toEqual(suggestions.filter(sug => sug.scopes.includes('[]')));
14355
});
14456

14557
it('should not lists pick suggestions', () => {
@@ -150,6 +62,29 @@ describe('get suggestion', () => {
15062
expect(suggestion).toEqual([]);
15163
});
15264

65+
it('should lists suggestions when json path ends with part of an attribute', () => {
66+
// Given
67+
// When
68+
const suggestion = getSuggestions('$.tes', 5, { test: [0, 1] });
69+
// then
70+
expect(suggestion).toEqual([
71+
{
72+
'description': 'property',
73+
'scopes': [
74+
'object'
75+
],
76+
'value': 'test'
77+
}, ...suggestions.filter(sug => sug.scopes.includes('object'))]);
78+
});
79+
80+
it('should lists suggestions when json path ends with an exact attribute', () => {
81+
// Given
82+
// When
83+
const suggestion = getSuggestions('$.test', 5, { test: {exemple: 'test'} });
84+
// then
85+
expect(suggestion).toEqual(suggestions.filter(sug => sug.scopes.includes('object')));
86+
});
87+
15388
it('should lists no suggestions when json path is invalid', () => {
15489
// Given
15590
// When

tests/getInputSelection-test.js

Lines changed: 88 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,125 @@
1-
import expect from 'expect'
2-
import React from 'react'
3-
import { render, unmountComponentAtNode } from 'react-dom'
1+
import expect from 'expect';
2+
import React from 'react';
3+
import { render, unmountComponentAtNode } from 'react-dom';
44
import Enzyme, { mount, shallow } from 'enzyme';
55
import Adapter from 'enzyme-adapter-react-16';
66

7-
import JsonPathEditor from 'src/'
7+
import JsonPathEditor from 'src/';
88

99
Enzyme.configure({ adapter: new Adapter() });
1010

11-
import { setCaretPosition, getInputSelection, insertAtCursor } from "../src/components/getInputSelection";
11+
import { setCaretPosition, getInputSelection, insertAtCursor } from '../src/components/getInputSelection';
1212

1313
describe('input selection helpers', () => {
1414
it('should return and set expected positions', () => {
15-
const wrapper = mount(<JsonPathEditor value='a'/>)
15+
const wrapper = mount(<JsonPathEditor value='a'/>);
1616
let input = wrapper.instance().inputRef.current;
1717
expect(getInputSelection(input)).toEqual({start: 1, end: 1});
1818
setCaretPosition(input, 0);
19-
})
19+
});
20+
21+
it('should not set carret when element is null', () => {
22+
const wrapper = mount(<JsonPathEditor value='a' />);
23+
let input = wrapper.instance().inputRef.current;
24+
setCaretPosition(null, 0);
25+
expect(getInputSelection(input)).toEqual({start: 1, end: 1});
26+
});
2027

2128
it('should set carret at 0', () => {
22-
const wrapper = mount(<JsonPathEditor />)
29+
const wrapper = mount(<JsonPathEditor />);
2330
let input = wrapper.instance().inputRef.current;
2431
setCaretPosition(input, 0);
2532
expect(getInputSelection(input)).toEqual({start: 0, end: 0});
26-
})
33+
});
2734

2835
it('should set carret at 0', () => {
29-
const wrapper = mount(<JsonPathEditor />)
36+
const wrapper = mount(<JsonPathEditor />);
3037
let input = wrapper.instance().inputRef.current;
3138
setCaretPosition(input, 0);
3239
expect(getInputSelection(input)).toEqual({start: 0, end: 0});
33-
})
40+
});
3441

3542
it('should set carret at 5', () => {
3643
const spyMove = expect.createSpy();
3744
const spySelect = expect.createSpy();
3845
const spy = expect.createSpy().andCall(function () {
39-
return {move: spyMove, select: spySelect}
40-
});
46+
return {move: spyMove, select: spySelect};
47+
});
4148
setCaretPosition({createTextRange: spy}, 5);
4249
expect(spy).toHaveBeenCalledWith();
4350
expect(spyMove).toHaveBeenCalledWith('character', 5);
4451
expect(spySelect).toHaveBeenCalledWith();
45-
})
52+
});
4653

4754
it('should return text at position', () => {
48-
const wrapper = mount(<JsonPathEditor value='a'/>)
55+
const wrapper = mount(<JsonPathEditor value='a'/>);
4956
let input = wrapper.instance().inputRef.current;
5057
expect(getInputSelection(input)).toEqual({start: 1, end: 1});
5158
const result = insertAtCursor(input, 'b');
5259
expect(result).toEqual('ab');
53-
})
54-
})
60+
});
61+
62+
63+
it('should getInputSelection for old browser and return 0 when no range', () => {
64+
const mockedRange = undefined;
65+
document.selection = {createRange: expect.createSpy()
66+
.andReturn(mockedRange)};
67+
const mockElement = {selectionStart: undefined, value:'hello'};
68+
const position = getInputSelection(mockElement);
69+
expect(position).toEqual({start: 0, end: 0});
70+
});
71+
72+
it('should getInputSelection for old browser and return 0', () => {
73+
const mockedTextRange = {
74+
moveToBookmark: expect.createSpy(),
75+
collapse: expect.createSpy(),
76+
compareEndPoints: expect.createSpy().andReturn(2),
77+
moveStart: expect.createSpy().andReturn(0),
78+
moveEnd: expect.createSpy()
79+
};
80+
const mockElement = {selectionStart: undefined, value:'hello', createTextRange: expect.createSpy().andReturn(mockedTextRange)};
81+
const mockedRange = {parentElement: expect.createSpy().andReturn(mockElement),getBookmark: expect.createSpy()};
82+
document.selection = {createRange: expect.createSpy()
83+
.andReturn(mockedRange)};
84+
const position = getInputSelection(mockElement);
85+
expect(position).toEqual({start: mockElement.value.length, end: mockElement.value.length});
86+
});
87+
88+
it('should getInputSelection for old browser and return 0 when managing range', () => {
89+
const mockedTextRange = {
90+
moveToBookmark: expect.createSpy(),
91+
collapse: expect.createSpy(),
92+
compareEndPoints: expect.createSpy().andReturn(-2),
93+
moveStart: expect.createSpy().andReturn(0),
94+
moveEnd: expect.createSpy().andReturn(0)
95+
};
96+
const mockElement = {selectionStart: undefined, value:'hello', createTextRange: expect.createSpy().andReturn(mockedTextRange)};
97+
const mockedRange = {parentElement: expect.createSpy().andReturn(mockElement),getBookmark: expect.createSpy()};
98+
document.selection = {createRange: expect.createSpy().andReturn(mockedRange)};
99+
const position = getInputSelection(mockElement);
100+
expect(position).toEqual({start: 0, end: 0});
101+
});
102+
103+
it('should getInputSelection for old browser and return 0,len when managing selected range', () => {
104+
let isFirstCall = true;
105+
const mockedTextRange = {
106+
moveToBookmark: expect.createSpy(),
107+
collapse: expect.createSpy(),
108+
compareEndPoints: expect.createSpy().andCall(() => {
109+
if (isFirstCall) {
110+
isFirstCall = false;
111+
return -2;
112+
} else {
113+
return 2;
114+
}
115+
}),
116+
moveStart: expect.createSpy().andReturn(0),
117+
moveEnd: expect.createSpy().andReturn(0)
118+
};
119+
const mockElement = {selectionStart: undefined, value:'hello', createTextRange: expect.createSpy().andReturn(mockedTextRange)};
120+
const mockedRange = {parentElement: expect.createSpy().andReturn(mockElement),getBookmark: expect.createSpy()};
121+
document.selection = {createRange: expect.createSpy().andReturn(mockedRange)};
122+
const position = getInputSelection(mockElement);
123+
expect(position).toEqual({start: 0, end: mockElement.value.length});
124+
});
125+
});

0 commit comments

Comments
 (0)