open-nomad/ui/tests/unit/mixins/searchable-test.js

314 lines
9.1 KiB
JavaScript
Raw Normal View History

import { alias } from '@ember/object/computed';
import EmberObject, { computed } from '@ember/object';
2019-03-13 00:04:16 +00:00
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
2017-09-19 14:47:10 +00:00
import Searchable from 'nomad-ui/mixins/searchable';
2021-12-28 14:45:20 +00:00
module('Unit | Mixin | Searchable', function (hooks) {
2019-03-13 00:04:16 +00:00
setupTest(hooks);
2021-12-28 14:45:20 +00:00
hooks.beforeEach(function () {
this.subject = function () {
// eslint-disable-next-line ember/no-new-mixins
2019-03-13 00:04:16 +00:00
const SearchableObject = EmberObject.extend(Searchable, {
source: null,
2021-12-28 14:45:20 +00:00
searchProps: computed(function () {
return ['id', 'name'];
}),
2019-03-13 00:04:16 +00:00
listToSearch: alias('source'),
});
this.owner.register('test-container:searchable-object', SearchableObject);
return this.owner.lookup('test-container:searchable-object');
};
});
2021-12-28 14:45:20 +00:00
test('the searchable mixin does nothing when there is no search term', function (assert) {
2019-03-13 00:04:16 +00:00
const subject = this.subject();
2021-12-28 14:45:20 +00:00
subject.set('source', [
{ id: '1', name: 'hello' },
{ id: '2', name: 'world' },
]);
2019-03-13 00:04:16 +00:00
assert.deepEqual(subject.get('listSearched'), subject.get('source'));
});
2021-12-28 14:45:20 +00:00
test('the searchable mixin allows for regex search', function (assert) {
2019-03-13 00:04:16 +00:00
const subject = this.subject();
subject.set('source', [
{ id: '1', name: 'hello' },
{ id: '2', name: 'world' },
{ id: '3', name: 'oranges' },
]);
subject.set('searchTerm', '.+l+[A-Z]$');
assert.deepEqual(
subject.get('listSearched'),
2021-12-28 14:45:20 +00:00
[
{ id: '1', name: 'hello' },
{ id: '2', name: 'world' },
],
2019-03-13 00:04:16 +00:00
'hello and world matched for regex'
);
});
2021-12-28 14:45:20 +00:00
test('the searchable mixin only searches the declared search props', function (assert) {
2019-03-13 00:04:16 +00:00
const subject = this.subject();
subject.set('source', [
{ id: '1', name: 'United States of America', continent: 'North America' },
{ id: '2', name: 'Canada', continent: 'North America' },
{ id: '3', name: 'Mexico', continent: 'North America' },
2019-03-13 00:04:16 +00:00
]);
subject.set('searchTerm', 'America');
assert.deepEqual(
subject.get('listSearched'),
2021-12-28 16:08:12 +00:00
[
{
id: '1',
name: 'United States of America',
continent: 'North America',
},
],
2019-03-13 00:04:16 +00:00
'Only USA matched, since continent is not a search prop'
);
});
2021-12-28 14:45:20 +00:00
test('the fuzzy search mode is off by default', function (assert) {
2019-03-13 00:04:16 +00:00
const subject = this.subject();
subject.set('source', [
{ id: '1', name: 'United States of America', continent: 'North America' },
{ id: '2', name: 'Canada', continent: 'North America' },
{ id: '3', name: 'Mexico', continent: 'North America' },
]);
subject.set('searchTerm', 'Ameerica');
assert.deepEqual(
subject.get('listSearched'),
[],
'Nothing is matched since America is spelled incorrectly'
);
});
2021-12-28 14:45:20 +00:00
test('the fuzzy search mode can be enabled', function (assert) {
2019-03-13 00:04:16 +00:00
const subject = this.subject();
subject.set('source', [
{ id: '1', name: 'United States of America', continent: 'North America' },
{ id: '2', name: 'Canada', continent: 'North America' },
{ id: '3', name: 'Mexico', continent: 'North America' },
]);
subject.set('fuzzySearchEnabled', true);
subject.set('searchTerm', 'Ameerica');
assert.deepEqual(
subject.get('listSearched'),
2021-12-28 16:08:12 +00:00
[
{
id: '1',
name: 'United States of America',
continent: 'North America',
},
],
2019-03-13 00:04:16 +00:00
'America is matched due to fuzzy matching'
);
});
2021-12-28 14:45:20 +00:00
test('the fuzzy search can include match results', function (assert) {
const subject = this.subject();
subject.set('source', [
2021-12-28 16:08:12 +00:00
EmberObject.create({
id: '1',
name: 'United States of America',
continent: 'North America',
}),
EmberObject.create({
id: '2',
name: 'Canada',
continent: 'North America',
}),
EmberObject.create({
id: '3',
name: 'Mexico',
continent: 'North America',
}),
]);
subject.set('fuzzySearchEnabled', true);
subject.set('includeFuzzySearchMatches', true);
subject.set('searchTerm', 'Ameerica');
assert.deepEqual(
subject
.get('listSearched')
2021-12-28 16:08:12 +00:00
.map((object) =>
object.getProperties('id', 'name', 'continent', 'fuzzySearchMatches')
),
[
{
id: '1',
name: 'United States of America',
continent: 'North America',
fuzzySearchMatches: [
{
2021-12-28 14:45:20 +00:00
indices: [
[2, 2],
[4, 4],
[9, 9],
[11, 11],
[17, 23],
],
value: 'United States of America',
key: 'name',
},
],
},
],
'America is matched due to fuzzy matching'
);
});
2021-12-28 14:45:20 +00:00
test('the exact match search mode can be disabled', function (assert) {
2019-03-13 00:04:16 +00:00
const subject = this.subject();
subject.set('source', [
{ id: '1', name: 'United States of America', continent: 'North America' },
{ id: '2', name: 'Canada', continent: 'North America' },
{ id: '3', name: 'Mexico', continent: 'North America' },
]);
subject.set('regexSearchProps', []);
subject.set('searchTerm', 'Mexico');
assert.deepEqual(
subject.get('listSearched'),
[{ id: '3', name: 'Mexico', continent: 'North America' }],
'Mexico is matched exactly'
);
subject.set('exactMatchEnabled', false);
assert.deepEqual(
subject.get('listSearched'),
[],
'Nothing is matched now that exactMatch is disabled'
);
});
2021-12-28 14:45:20 +00:00
test('the regex search mode can be disabled', function (assert) {
2019-03-13 00:04:16 +00:00
const subject = this.subject();
subject.set('source', [
{ id: '1', name: 'United States of America', continent: 'North America' },
{ id: '2', name: 'Canada', continent: 'North America' },
{ id: '3', name: 'Mexico', continent: 'North America' },
]);
subject.set('searchTerm', '^.{6}$');
assert.deepEqual(
subject.get('listSearched'),
[
{ id: '2', name: 'Canada', continent: 'North America' },
{ id: '3', name: 'Mexico', continent: 'North America' },
],
'Canada and Mexico meet the regex criteria'
);
subject.set('regexEnabled', false);
assert.deepEqual(
subject.get('listSearched'),
[],
'Nothing is matched now that regex is disabled'
);
});
2021-12-28 14:45:20 +00:00
test('each search mode has independent search props', function (assert) {
2019-03-13 00:04:16 +00:00
const subject = this.subject();
subject.set('source', [
{ id: '1', name: 'United States of America', continent: 'North America' },
{ id: '2', name: 'Canada', continent: 'North America' },
{ id: '3', name: 'Mexico', continent: 'North America' },
]);
subject.set('fuzzySearchEnabled', true);
subject.set('regexSearchProps', ['id']);
subject.set('exactMatchSearchProps', ['continent']);
subject.set('fuzzySearchProps', ['name']);
subject.set('searchTerm', 'Nor America');
assert.deepEqual(
subject.get('listSearched'),
[],
'Not an exact match on continent, not a matchAllTokens match on fuzzy, not a regex match on id'
);
subject.set('searchTerm', 'America States');
assert.deepEqual(
subject.get('listSearched'),
2021-12-28 16:08:12 +00:00
[
{
id: '1',
name: 'United States of America',
continent: 'North America',
},
],
2019-03-13 00:04:16 +00:00
'Fuzzy match on one country, but not an exact match on continent'
);
subject.set('searchTerm', '^(.a){3}$');
assert.deepEqual(
subject.get('listSearched'),
[],
'Canada is not matched by the regex because only id is looked at for regex search'
);
});
2021-12-28 14:45:20 +00:00
test('the resetPagination method is a no-op', function (assert) {
2019-03-13 00:04:16 +00:00
const subject = this.subject();
2021-12-28 16:08:12 +00:00
assert.strictEqual(
subject.get('currentPage'),
undefined,
'No currentPage value set'
);
2019-03-13 00:04:16 +00:00
subject.resetPagination();
2021-12-28 16:08:12 +00:00
assert.strictEqual(
subject.get('currentPage'),
undefined,
'Still no currentPage value set'
);
2019-03-13 00:04:16 +00:00
});
2018-10-30 21:17:23 +00:00
});
2021-12-28 14:45:20 +00:00
module('Unit | Mixin | Searchable (with pagination)', function (hooks) {
2019-03-13 00:04:16 +00:00
setupTest(hooks);
2021-12-28 14:45:20 +00:00
hooks.beforeEach(function () {
this.subject = function () {
// eslint-disable-next-line ember/no-new-mixins
2019-03-13 00:04:16 +00:00
const SearchablePaginatedObject = EmberObject.extend(Searchable, {
source: null,
2021-12-28 14:45:20 +00:00
searchProps: computed(function () {
return ['id', 'name'];
}),
2019-03-13 00:04:16 +00:00
listToSearch: alias('source'),
currentPage: 1,
});
2021-12-28 16:08:12 +00:00
this.owner.register(
'test-container:searchable-paginated-object',
SearchablePaginatedObject
);
2019-03-13 00:04:16 +00:00
return this.owner.lookup('test-container:searchable-paginated-object');
};
});
2021-12-28 14:45:20 +00:00
test('the resetPagination method sets the currentPage to 1', function (assert) {
2019-03-13 00:04:16 +00:00
const subject = this.subject();
subject.set('currentPage', 5);
2021-12-28 16:08:12 +00:00
assert.equal(
subject.get('currentPage'),
5,
'Current page is something other than 1'
);
2019-03-13 00:04:16 +00:00
subject.resetPagination();
assert.equal(subject.get('currentPage'), 1, 'Current page gets reset to 1');
});
2018-10-30 21:17:23 +00:00
});