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