github.com/aminovpavel/nomad@v0.11.8/ui/tests/unit/mixins/searchable-test.js (about) 1 import { alias } from '@ember/object/computed'; 2 import EmberObject, { computed } from '@ember/object'; 3 import { module, test } from 'qunit'; 4 import { setupTest } from 'ember-qunit'; 5 import Searchable from 'nomad-ui/mixins/searchable'; 6 7 module('Unit | Mixin | Searchable', function(hooks) { 8 setupTest(hooks); 9 10 hooks.beforeEach(function() { 11 this.subject = function() { 12 const SearchableObject = EmberObject.extend(Searchable, { 13 source: null, 14 searchProps: computed(() => ['id', 'name']), 15 listToSearch: alias('source'), 16 }); 17 18 this.owner.register('test-container:searchable-object', SearchableObject); 19 return this.owner.lookup('test-container:searchable-object'); 20 }; 21 }); 22 23 test('the searchable mixin does nothing when there is no search term', function(assert) { 24 const subject = this.subject(); 25 subject.set('source', [{ id: '1', name: 'hello' }, { id: '2', name: 'world' }]); 26 27 assert.deepEqual(subject.get('listSearched'), subject.get('source')); 28 }); 29 30 test('the searchable mixin allows for regex search', function(assert) { 31 const subject = this.subject(); 32 subject.set('source', [ 33 { id: '1', name: 'hello' }, 34 { id: '2', name: 'world' }, 35 { id: '3', name: 'oranges' }, 36 ]); 37 38 subject.set('searchTerm', '.+l+[A-Z]$'); 39 assert.deepEqual( 40 subject.get('listSearched'), 41 [{ id: '1', name: 'hello' }, { id: '2', name: 'world' }], 42 'hello and world matched for regex' 43 ); 44 }); 45 46 test('the searchable mixin only searches the declared search props', function(assert) { 47 const subject = this.subject(); 48 subject.set('source', [ 49 { id: '1', name: 'United States of America', continent: 'North America' }, 50 { id: '2', name: 'Canada', continent: 'North America' }, 51 { id: '3', name: 'Mexico', continent: 'North America' }, 52 ]); 53 54 subject.set('searchTerm', 'America'); 55 assert.deepEqual( 56 subject.get('listSearched'), 57 [{ id: '1', name: 'United States of America', continent: 'North America' }], 58 'Only USA matched, since continent is not a search prop' 59 ); 60 }); 61 62 test('the fuzzy search mode is off by default', function(assert) { 63 const subject = this.subject(); 64 subject.set('source', [ 65 { id: '1', name: 'United States of America', continent: 'North America' }, 66 { id: '2', name: 'Canada', continent: 'North America' }, 67 { id: '3', name: 'Mexico', continent: 'North America' }, 68 ]); 69 70 subject.set('searchTerm', 'Ameerica'); 71 assert.deepEqual( 72 subject.get('listSearched'), 73 [], 74 'Nothing is matched since America is spelled incorrectly' 75 ); 76 }); 77 78 test('the fuzzy search mode can be enabled', function(assert) { 79 const subject = this.subject(); 80 subject.set('source', [ 81 { id: '1', name: 'United States of America', continent: 'North America' }, 82 { id: '2', name: 'Canada', continent: 'North America' }, 83 { id: '3', name: 'Mexico', continent: 'North America' }, 84 ]); 85 86 subject.set('fuzzySearchEnabled', true); 87 subject.set('searchTerm', 'Ameerica'); 88 assert.deepEqual( 89 subject.get('listSearched'), 90 [{ id: '1', name: 'United States of America', continent: 'North America' }], 91 'America is matched due to fuzzy matching' 92 ); 93 }); 94 95 test('the exact match search mode can be disabled', function(assert) { 96 const subject = this.subject(); 97 subject.set('source', [ 98 { id: '1', name: 'United States of America', continent: 'North America' }, 99 { id: '2', name: 'Canada', continent: 'North America' }, 100 { id: '3', name: 'Mexico', continent: 'North America' }, 101 ]); 102 103 subject.set('regexSearchProps', []); 104 subject.set('searchTerm', 'Mexico'); 105 106 assert.deepEqual( 107 subject.get('listSearched'), 108 [{ id: '3', name: 'Mexico', continent: 'North America' }], 109 'Mexico is matched exactly' 110 ); 111 112 subject.set('exactMatchEnabled', false); 113 114 assert.deepEqual( 115 subject.get('listSearched'), 116 [], 117 'Nothing is matched now that exactMatch is disabled' 118 ); 119 }); 120 121 test('the regex search mode can be disabled', function(assert) { 122 const subject = this.subject(); 123 subject.set('source', [ 124 { id: '1', name: 'United States of America', continent: 'North America' }, 125 { id: '2', name: 'Canada', continent: 'North America' }, 126 { id: '3', name: 'Mexico', continent: 'North America' }, 127 ]); 128 129 subject.set('searchTerm', '^.{6}$'); 130 assert.deepEqual( 131 subject.get('listSearched'), 132 [ 133 { id: '2', name: 'Canada', continent: 'North America' }, 134 { id: '3', name: 'Mexico', continent: 'North America' }, 135 ], 136 'Canada and Mexico meet the regex criteria' 137 ); 138 139 subject.set('regexEnabled', false); 140 141 assert.deepEqual( 142 subject.get('listSearched'), 143 [], 144 'Nothing is matched now that regex is disabled' 145 ); 146 }); 147 148 test('each search mode has independent search props', function(assert) { 149 const subject = this.subject(); 150 subject.set('source', [ 151 { id: '1', name: 'United States of America', continent: 'North America' }, 152 { id: '2', name: 'Canada', continent: 'North America' }, 153 { id: '3', name: 'Mexico', continent: 'North America' }, 154 ]); 155 156 subject.set('fuzzySearchEnabled', true); 157 subject.set('regexSearchProps', ['id']); 158 subject.set('exactMatchSearchProps', ['continent']); 159 subject.set('fuzzySearchProps', ['name']); 160 161 subject.set('searchTerm', 'Nor America'); 162 assert.deepEqual( 163 subject.get('listSearched'), 164 [], 165 'Not an exact match on continent, not a matchAllTokens match on fuzzy, not a regex match on id' 166 ); 167 168 subject.set('searchTerm', 'America States'); 169 assert.deepEqual( 170 subject.get('listSearched'), 171 [{ id: '1', name: 'United States of America', continent: 'North America' }], 172 'Fuzzy match on one country, but not an exact match on continent' 173 ); 174 175 subject.set('searchTerm', '^(.a){3}$'); 176 assert.deepEqual( 177 subject.get('listSearched'), 178 [], 179 'Canada is not matched by the regex because only id is looked at for regex search' 180 ); 181 }); 182 183 test('the resetPagination method is a no-op', function(assert) { 184 const subject = this.subject(); 185 assert.strictEqual(subject.get('currentPage'), undefined, 'No currentPage value set'); 186 subject.resetPagination(); 187 assert.strictEqual(subject.get('currentPage'), undefined, 'Still no currentPage value set'); 188 }); 189 }); 190 191 module('Unit | Mixin | Searchable (with pagination)', function(hooks) { 192 setupTest(hooks); 193 194 hooks.beforeEach(function() { 195 this.subject = function() { 196 const SearchablePaginatedObject = EmberObject.extend(Searchable, { 197 source: null, 198 searchProps: computed(() => ['id', 'name']), 199 listToSearch: alias('source'), 200 currentPage: 1, 201 }); 202 203 this.owner.register('test-container:searchable-paginated-object', SearchablePaginatedObject); 204 return this.owner.lookup('test-container:searchable-paginated-object'); 205 }; 206 }); 207 208 test('the resetPagination method sets the currentPage to 1', function(assert) { 209 const subject = this.subject(); 210 subject.set('currentPage', 5); 211 assert.equal(subject.get('currentPage'), 5, 'Current page is something other than 1'); 212 subject.resetPagination(); 213 assert.equal(subject.get('currentPage'), 1, 'Current page gets reset to 1'); 214 }); 215 });