Commit d35a71d4 authored by Kyle Keating's avatar Kyle Keating

Fix JS error when searching after creating a page draft

This adjusts view-filter so it can handle draft annotations which can be defined by a missing id.
parent 70716a34
...@@ -74,9 +74,10 @@ describe('sidebar/services/view-filter', () => { ...@@ -74,9 +74,10 @@ describe('sidebar/services/view-filter', () => {
describe('"any" field', () => { describe('"any" field', () => {
it('finds matches in any field', () => { it('finds matches in any field', () => {
const annotations = [ const annotations = [
{ id: 1, text: poem.tiger }, { id: 1, text: poem.tiger, target: [{}] },
{ id: 2, user: 'Tiger' }, { id: 4, user: 'lion', target: [{}] },
{ id: 3, tags: ['Tiger'] }, { id: 2, user: 'Tiger', target: [{}] },
{ id: 3, tags: ['Tiger'], target: [{}] },
]; ];
const filters = { any: { terms: ['Tiger'], operator: 'and' } }; const filters = { any: { terms: ['Tiger'], operator: 'and' } };
...@@ -191,6 +192,7 @@ describe('sidebar/services/view-filter', () => { ...@@ -191,6 +192,7 @@ describe('sidebar/services/view-filter', () => {
const annotation = { const annotation = {
id: 1, id: 1,
updated: isoDateWithAge(50), updated: isoDateWithAge(50),
target: [{}],
}; };
const filters = { const filters = {
since: { terms: [100], operator: 'and' }, since: { terms: [100], operator: 'and' },
...@@ -205,6 +207,7 @@ describe('sidebar/services/view-filter', () => { ...@@ -205,6 +207,7 @@ describe('sidebar/services/view-filter', () => {
const annotation = { const annotation = {
id: 1, id: 1,
updated: isoDateWithAge(150), updated: isoDateWithAge(150),
target: [{}],
}; };
const filters = { const filters = {
since: { terms: [100], operator: 'and' }, since: { terms: [100], operator: 'and' },
...@@ -217,7 +220,11 @@ describe('sidebar/services/view-filter', () => { ...@@ -217,7 +220,11 @@ describe('sidebar/services/view-filter', () => {
}); });
it('ignores filters with no terms in the query', () => { it('ignores filters with no terms in the query', () => {
const annotation = { id: 1, tags: ['foo'] }; const annotation = {
id: 1,
tags: ['foo'],
target: [{}],
};
const filters = { const filters = {
any: { any: {
terms: ['foo'], terms: ['foo'],
...@@ -233,4 +240,57 @@ describe('sidebar/services/view-filter', () => { ...@@ -233,4 +240,57 @@ describe('sidebar/services/view-filter', () => {
assert.deepEqual(result, [1]); assert.deepEqual(result, [1]);
}); });
it('ignores annotations (drafts) with no id', () => {
const annotation = {
tags: ['foo'],
target: [{}],
};
const filters = {
any: {
terms: ['foo'],
operator: 'and',
},
};
const result = viewFilter.filter([annotation], filters);
assert.deepEqual(result, []);
});
describe('malformed target object', () => {
it('should not fail on annotations without a target object', () => {
const annotation = {
id: 1,
text: 'foo',
// Missing target
};
const filters = {
any: {
terms: ['foo'],
operator: 'or',
},
};
viewFilter.filter([annotation], filters);
});
it('should not fail on annotations without a target object item', () => {
const annotation = {
id: 1,
text: 'foo',
target: [], // Missing target item
};
const filters = {
any: {
terms: ['foo'],
operator: 'or',
},
};
viewFilter.filter([annotation], filters);
});
});
}); });
...@@ -100,9 +100,10 @@ function viewFilter(unicode) { ...@@ -100,9 +100,10 @@ function viewFilter(unicode) {
quote: { quote: {
autofalse: ann => (ann.references || []).length > 0, autofalse: ann => (ann.references || []).length > 0,
value(annotation) { value(annotation) {
if (!annotation.target) { if (!annotation.target || !annotation.target.length) {
// FIXME: All annotations *must* have a target, so this check should // Sanity check that ignores any annotation without a target. We should
// not be required. // never arrive at this place in the code, but its a safe guard against
// anything from the server that may be malformed.
return ''; return '';
} }
const target = annotation.target[0]; const target = annotation.target[0];
...@@ -183,7 +184,9 @@ function viewFilter(unicode) { ...@@ -183,7 +184,9 @@ function viewFilter(unicode) {
const rootFilter = new BinaryOpFilter('and', fieldFilters); const rootFilter = new BinaryOpFilter('and', fieldFilters);
return annotations return annotations
.filter(ann => rootFilter.matches(ann)) .filter(ann => {
return ann.id && rootFilter.matches(ann);
})
.map(ann => ann.id); .map(ann => ann.id);
}; };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment