Fixed broken unit tests

This commit is contained in:
jos 2018-09-19 15:16:45 +02:00
parent 3e795b3275
commit 4f86135050
2 changed files with 48 additions and 37 deletions

View File

@ -221,16 +221,16 @@ export function search (eson, text) {
// TODO: keep active result from previous search if any? // TODO: keep active result from previous search if any?
const updatedEson = transform (eson, function (value, path) { // find search results and add search tags in the ESON object
let updatedEson = transform (eson, function (value, path) {
let updatedValue = value let updatedValue = value
// check property name // check property name
const prop = last(path) const prop = last(path)
if (text !== '' && containsCaseInsensitive(prop, text) && if (text !== '' && containsCaseInsensitive(prop, text) &&
getIn(eson, initial(path))[TYPE] === 'object') { // parent must be an Object getIn(eson, initial(path))[TYPE] === 'object') { // parent must be an Object
const searchState = isEmpty(matches) ? 'active' : 'normal'
matches.push({path, area: 'property'}) matches.push({path, area: 'property'})
updatedValue = setIn(updatedValue, [SEARCH_PROPERTY], searchState) updatedValue = setIn(updatedValue, [SEARCH_PROPERTY], 'normal')
} }
else { else {
updatedValue = deleteIn(updatedValue, [SEARCH_PROPERTY]) updatedValue = deleteIn(updatedValue, [SEARCH_PROPERTY])
@ -238,9 +238,8 @@ export function search (eson, text) {
// check value // check value
if (value[TYPE] === 'value' && text !== '' && containsCaseInsensitive(value[VALUE], text)) { if (value[TYPE] === 'value' && text !== '' && containsCaseInsensitive(value[VALUE], text)) {
const searchState = isEmpty(matches) ? 'active' : 'normal'
matches.push({path, area: 'value'}) matches.push({path, area: 'value'})
updatedValue = setIn(updatedValue, [SEARCH_VALUE], searchState) updatedValue = setIn(updatedValue, [SEARCH_VALUE], 'normal')
} }
else { else {
updatedValue = deleteIn(updatedValue, [SEARCH_VALUE]) updatedValue = deleteIn(updatedValue, [SEARCH_VALUE])
@ -249,6 +248,7 @@ export function search (eson, text) {
return updatedValue return updatedValue
}) })
// sort the results by path and property/value
matches.sort((a, b) => { matches.sort((a, b) => {
const arrayOrder = compareArrays(a.path, b.path) const arrayOrder = compareArrays(a.path, b.path)
if (arrayOrder !== 0) { if (arrayOrder !== 0) {
@ -261,12 +261,23 @@ export function search (eson, text) {
return compareStrings(a.area, b.area) return compareStrings(a.area, b.area)
}) })
// make the first search result active
const active = matches[0] || null
if (active) {
if (active.area === 'property') {
updatedEson = setIn(updatedEson, active.path.concat(SEARCH_PROPERTY), 'active')
}
if (active.area === 'value') {
updatedEson = setIn(updatedEson, active.path.concat(SEARCH_VALUE), 'active')
}
}
return { return {
eson: updatedEson, eson: updatedEson,
searchResult: { searchResult: {
text, text,
matches, matches,
active: matches[0] || null active
} }
} }
} }

View File

@ -229,14 +229,14 @@ test('search', () => {
const active = result.searchResult.active const active = result.searchResult.active
expect(matches).toEqual([ expect(matches).toEqual([
{path: ['obj', 'arr', '2', 'last'], area: 'property'}, {path: ['bool'], area: 'property'},
{path: ['str'], area: 'value'}, {path: ['bool'], area: 'value'},
{path: ['nill'], area: 'property'}, {path: ['nill'], area: 'property'},
{path: ['nill'], area: 'value'}, {path: ['nill'], area: 'value'},
{path: ['bool'], area: 'property'}, {path: ['obj', 'arr', '2', 'last'], area: 'property'},
{path: ['bool'], area: 'value'} {path: ['str'], area: 'value'},
]) ])
expect(active).toEqual({path: ['obj', 'arr', '2', 'last'], area: 'property'}) expect(active).toEqual({path: ['bool'], area: 'property'})
let expected = esonWithSearch let expected = esonWithSearch
expected = setIn(expected, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY], 'active') expected = setIn(expected, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY], 'active')
@ -276,33 +276,33 @@ test('nextSearchResult', () => {
const first = search(eson, 'A') const first = search(eson, 'A')
expect(first.searchResult.matches).toEqual([ expect(first.searchResult.matches).toEqual([
{path: ['bool'], area: 'value'},
{path: ['obj', 'arr'], area: 'property'}, {path: ['obj', 'arr'], area: 'property'},
{path: ['obj', 'arr', '2', 'last'], area: 'property'}, {path: ['obj', 'arr', '2', 'last'], area: 'property'},
{path: ['bool'], area: 'value'}
]) ])
expect(first.searchResult.active).toEqual({path: ['obj', 'arr'], area: 'property'}) expect(first.searchResult.active).toEqual({path: ['bool'], area: 'value'})
expect(getIn(first.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('active') expect(getIn(first.eson, ['bool', SEARCH_VALUE])).toEqual('active')
expect(getIn(first.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(first.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal') expect(getIn(first.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(first.eson, ['bool', SEARCH_VALUE])).toEqual('normal')
const second = nextSearchResult(first.eson, first.searchResult) const second = nextSearchResult(first.eson, first.searchResult)
expect(second.searchResult.active).toEqual({path: ['obj', 'arr', '2', 'last'], area: 'property'}) expect(second.searchResult.active).toEqual({path: ['obj', 'arr'], area: 'property'})
expect(getIn(second.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(second.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('active')
expect(getIn(second.eson, ['bool', SEARCH_VALUE])).toEqual('normal') expect(getIn(second.eson, ['bool', SEARCH_VALUE])).toEqual('normal')
expect(getIn(second.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('active')
expect(getIn(second.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal')
const third = nextSearchResult(second.eson, second.searchResult) const third = nextSearchResult(second.eson, second.searchResult)
expect(third.searchResult.active).toEqual({path: ['bool'], area: 'value'}) expect(third.searchResult.active).toEqual({path: ['obj', 'arr', '2', 'last'], area: 'property'})
expect(getIn(third.eson, ['bool', SEARCH_VALUE])).toEqual('normal')
expect(getIn(third.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('normal') expect(getIn(third.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(third.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal') expect(getIn(third.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('active')
expect(getIn(third.eson, ['bool', SEARCH_VALUE])).toEqual('active')
const wrappedAround = nextSearchResult(third.eson, third.searchResult) const wrappedAround = nextSearchResult(third.eson, third.searchResult)
expect(wrappedAround.searchResult.active).toEqual({path: ['obj', 'arr'], area: 'property'}) expect(wrappedAround.searchResult.active).toEqual({path: ['bool'], area: 'value'})
expect(getIn(wrappedAround.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('active') expect(getIn(wrappedAround.eson, ['bool', SEARCH_VALUE])).toEqual('active')
expect(getIn(wrappedAround.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(wrappedAround.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal') expect(getIn(wrappedAround.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(wrappedAround.eson, ['bool', SEARCH_VALUE])).toEqual('normal')
}) })
test('previousSearchResult', () => { test('previousSearchResult', () => {
@ -317,33 +317,33 @@ test('previousSearchResult', () => {
const init = search(eson, 'A') const init = search(eson, 'A')
expect(init.searchResult.matches).toEqual([ expect(init.searchResult.matches).toEqual([
{path: ['bool'], area: 'value'},
{path: ['obj', 'arr'], area: 'property'}, {path: ['obj', 'arr'], area: 'property'},
{path: ['obj', 'arr', '2', 'last'], area: 'property'}, {path: ['obj', 'arr', '2', 'last'], area: 'property'},
{path: ['bool'], area: 'value'}
]) ])
expect(init.searchResult.active).toEqual({path: ['obj', 'arr'], area: 'property'}) expect(init.searchResult.active).toEqual({path: ['bool'], area: 'value'})
expect(getIn(init.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('active') expect(getIn(init.eson, ['bool', SEARCH_VALUE])).toEqual('active')
expect(getIn(init.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(init.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal') expect(getIn(init.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(init.eson, ['bool', SEARCH_VALUE])).toEqual('normal')
const third = previousSearchResult(init.eson, init.searchResult) const third = previousSearchResult(init.eson, init.searchResult)
expect(third.searchResult.active).toEqual({path: ['bool'], area: 'value'}) expect(third.searchResult.active).toEqual({path: ['obj', 'arr', '2', 'last'], area: 'property'})
expect(getIn(third.eson, ['bool', SEARCH_VALUE])).toEqual('normal')
expect(getIn(third.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('normal') expect(getIn(third.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(third.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal') expect(getIn(third.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('active')
expect(getIn(third.eson, ['bool', SEARCH_VALUE])).toEqual('active')
const second = previousSearchResult(third.eson, third.searchResult) const second = previousSearchResult(third.eson, third.searchResult)
expect(second.searchResult.active).toEqual({path: ['obj', 'arr', '2', 'last'], area: 'property'}) expect(second.searchResult.active).toEqual({path: ['obj', 'arr'], area: 'property'})
expect(getIn(second.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(second.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('active')
expect(getIn(second.eson, ['bool', SEARCH_VALUE])).toEqual('normal') expect(getIn(second.eson, ['bool', SEARCH_VALUE])).toEqual('normal')
expect(getIn(second.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('active')
expect(getIn(second.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal')
const first = previousSearchResult(second.eson, second.searchResult) const first = previousSearchResult(second.eson, second.searchResult)
expect(first.searchResult.active).toEqual({path: ['obj', 'arr'], area: 'property'}) expect(first.searchResult.active).toEqual({path: ['bool'], area: 'value'})
expect(getIn(first.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('active') expect(getIn(first.eson, ['bool', SEARCH_VALUE])).toEqual('active')
expect(getIn(first.eson, ['obj', 'arr', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(first.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal') expect(getIn(first.eson, ['obj', 'arr', '2', 'last', SEARCH_PROPERTY])).toEqual('normal')
expect(getIn(first.eson, ['bool', SEARCH_VALUE])).toEqual('normal')
}) })
test('selection (object)', () => { test('selection (object)', () => {