Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ function getQueryHistoryAutocompletions(completions: Readonly<Completion[]>) {
);
}

function createSavedQueryWithFilter(filter: any): SavedQuery {
return {
type: 'recent',
lastExecuted: new Date('2023-06-04T18:00:00Z'),
queryProperties: {
filter,
project: { userId: 1, isActive: 1 },
collation: { locale: 'simple' },
sort: { userId: 1 },
limit: 10,
maxTimeMS: 500,
},
};
}

describe('query history autocompleter', function () {
const { getCompletions, cleanup } = setupCodemirrorCompleter(
createQueryWithHistoryAutocompleter
Expand Down Expand Up @@ -82,7 +97,7 @@ describe('query history autocompleter', function () {
await getCompletions('{}', {
savedQueries,
options: undefined,
queryProperty: '',
queryProperty: 'filter',
onApply: mockOnApply,
theme: 'light',
})
Expand All @@ -94,7 +109,7 @@ describe('query history autocompleter', function () {
await getCompletions('', {
savedQueries,
options: undefined,
queryProperty: '',
queryProperty: 'filter',
onApply: mockOnApply,
theme: 'light',
})
Expand Down Expand Up @@ -135,6 +150,59 @@ describe('query history autocompleter', function () {
}`);
});

it('will not autocomplete fields with more than 200 characters', async function () {
const completions = getQueryHistoryAutocompletions(
await getCompletions('{ price: 1, category: 1', {
savedQueries: [
createSavedQueryWithFilter({
allA: Array(201).fill('a').join(''),
}),
],
options: undefined,
queryProperty: 'filter',
onApply: mockOnApply,
theme: 'light',
})
);
expect(completions).to.have.lengthOf(0);
});

it('does not autocomplete nested fields', async function () {
const completions = getQueryHistoryAutocompletions(
await getCompletions('{ pineapple: { tre', {
savedQueries: [
createSavedQueryWithFilter({
pineapple: { tree: 'orange' },
}),
],
options: undefined,
queryProperty: 'filter',
onApply: mockOnApply,
theme: 'light',
})
);
expect(completions).to.have.lengthOf(0);
});

it('does not autocomplete more than ten items', async function () {
const completions = getQueryHistoryAutocompletions(
await getCompletions('{ pineapp', {
savedQueries: [
...Array(12).fill(
createSavedQueryWithFilter({
pineapple: true,
})
),
],
options: undefined,
queryProperty: 'filter',
onApply: mockOnApply,
theme: 'light',
})
);
expect(completions).to.have.lengthOf(10);
});

it('does not return fields that match in other query properties', async function () {
const completions = getQueryHistoryAutocompletions(
await getCompletions('local', {
Expand Down Expand Up @@ -178,4 +246,94 @@ describe('query history autocompleter', function () {
).map((completion) => completion.label)
).to.deep.eq(['bar', '1', 'buz', '2', 'foo']);
});

it('does not drop queries where the scalar property value is 0', async function () {
const queriesWithZero: SavedQuery[] = [
{
type: 'recent',
lastExecuted: new Date('2023-06-01T12:00:00Z'),
queryProperties: {
filter: { status: 'active' },
limit: 0,
skip: 0,
maxTimeMS: 0,
},
},
];

for (const prop of ['limit', 'skip', 'maxTimeMS'] as const) {
const completions = getQueryHistoryAutocompletions(
await getCompletions('0', {
savedQueries: queriesWithZero,
options: undefined,
queryProperty: prop,
onApply: mockOnApply,
theme: 'light',
})
);
expect(completions).to.have.lengthOf(1);
}
});

it('handles array-valued properties through Object.entries', async function () {
const queryWithArraySort: SavedQuery[] = [
{
type: 'recent',
lastExecuted: new Date('2023-06-01T12:00:00Z'),
queryProperties: {
filter: { status: 'active' },
sort: [['$natural', -1]],
},
},
];

const completions = getQueryHistoryAutocompletions(
await getCompletions('0', {
savedQueries: queryWithArraySort,
options: undefined,
queryProperty: 'sort',
onApply: mockOnApply,
theme: 'light',
})
);
expect(completions).to.have.lengthOf(0);
});

it('ignores non-key value arrays', async function () {
const invalidArrays = [
[1, 2, 3],
['a', 'b', 'c'],
[{ key: 'value' }, { key2: 'value2' }],
[
['$natural', -1],
['$natural', -1],
['one', 'two', 'three'],
],
[],
];

for (const arr of invalidArrays) {
const queryWithArraySort: SavedQuery[] = [
{
type: 'recent',
lastExecuted: new Date('2023-06-01T12:00:00Z'),
queryProperties: {
filter: { status: 'active' },
sort: arr,
},
},
];

const completions = getQueryHistoryAutocompletions(
await getCompletions('', {
savedQueries: queryWithArraySort,
options: undefined,
queryProperty: 'sort',
onApply: mockOnApply,
theme: 'light',
})
);
expect(completions).to.have.lengthOf(0);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,40 @@ describe('createQueryLabel', function () {
expect(result).to.equal("{\n name: 'pineapple'\n}");
});

it('should return the string representation of the property even when it is falsy', () => {
const result = createQueryLabel(
{
type: 'favorite',
lastExecuted: new Date('2023-06-03T16:00:00Z'),
queryProperties: {
filter: { pineapple: false },
project: {},
sort: undefined,
limit: undefined,
},
},
'filter'
);
expect(result).to.equal('{\n pineapple: false\n}');
});

it('should return the string representation of the property even when it is falsy', () => {
const result = createQueryLabel(
{
type: 'favorite',
lastExecuted: new Date('2023-06-03T16:00:00Z'),
queryProperties: {
filter: { pineapple: false },
project: {},
sort: undefined,
limit: 0,
},
},
'limit'
);
expect(result).to.equal('0');
});

it('should return an empty string if the property value is undefined', () => {
const result = createQueryLabel(
{
Expand Down
Loading
Loading