github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/ui/tests/unit/mixins/searchable-test.js (about)

     1  import { alias } from '@ember/object/computed';
     2  import { getOwner } from '@ember/application';
     3  import EmberObject, { computed } from '@ember/object';
     4  import { moduleFor, test } from 'ember-qunit';
     5  import Searchable from 'nomad-ui/mixins/searchable';
     6  
     7  moduleFor('mixin:searchable', 'Unit | Mixin | Searchable', {
     8    subject() {
     9      const SearchableObject = EmberObject.extend(Searchable, {
    10        source: null,
    11        searchProps: computed(() => ['id', 'name']),
    12        listToSearch: alias('source'),
    13      });
    14  
    15      this.register('test-container:searchable-object', SearchableObject);
    16      return getOwner(this).lookup('test-container:searchable-object');
    17    },
    18  });
    19  
    20  test('the searchable mixin does nothing when there is no search term', function(assert) {
    21    const subject = this.subject();
    22    subject.set('source', [{ id: '1', name: 'hello' }, { id: '2', name: 'world' }]);
    23  
    24    assert.deepEqual(subject.get('listSearched'), subject.get('source'));
    25  });
    26  
    27  test('the searchable mixin allows for regex search', function(assert) {
    28    const subject = this.subject();
    29    subject.set('source', [
    30      { id: '1', name: 'hello' },
    31      { id: '2', name: 'world' },
    32      { id: '3', name: 'oranges' },
    33    ]);
    34  
    35    subject.set('searchTerm', '.+l+[A-Z]$');
    36    assert.deepEqual(
    37      subject.get('listSearched'),
    38      [{ id: '1', name: 'hello' }, { id: '2', name: 'world' }],
    39      'hello and world matched for regex'
    40    );
    41  });
    42  
    43  test('the searchable mixin only searches the declared search props', function(assert) {
    44    const subject = this.subject();
    45    subject.set('source', [
    46      { id: '1', name: 'United States of America', continent: 'North America' },
    47      { id: '2', name: 'Canada', continent: 'North America' },
    48      { id: '3', name: 'Mexico', continent: 'North America' },
    49    ]);
    50  
    51    subject.set('searchTerm', 'America');
    52    assert.deepEqual(
    53      subject.get('listSearched'),
    54      [{ id: '1', name: 'United States of America', continent: 'North America' }],
    55      'Only USA matched, since continent is not a search prop'
    56    );
    57  });