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

     1  import { run } from '@ember/runloop';
     2  import { test } from 'ember-qunit';
     3  import wait from 'ember-test-helpers/wait';
     4  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
     5  import moduleForAdapter from '../../helpers/module-for-adapter';
     6  
     7  moduleForAdapter('node', 'Unit | Adapter | Node', {
     8    needs: [
     9      'adapter:node',
    10      'model:node-attributes',
    11      'model:allocation',
    12      'model:job',
    13      'serializer:application',
    14      'serializer:node',
    15      'service:token',
    16      'service:config',
    17      'service:watchList',
    18      'transform:fragment',
    19    ],
    20    beforeEach() {
    21      this.server = startMirage();
    22      this.server.create('node', { id: 'node-1' });
    23      this.server.create('node', { id: 'node-2' });
    24      this.server.create('job', { id: 'job-1', createAllocations: false });
    25  
    26      this.server.create('allocation', { id: 'node-1-1', nodeId: 'node-1' });
    27      this.server.create('allocation', { id: 'node-1-2', nodeId: 'node-1' });
    28      this.server.create('allocation', { id: 'node-2-1', nodeId: 'node-2' });
    29      this.server.create('allocation', { id: 'node-2-2', nodeId: 'node-2' });
    30    },
    31    afterEach() {
    32      this.server.shutdown();
    33    },
    34  });
    35  
    36  test('findHasMany removes old related models from the store', function(assert) {
    37    let node;
    38    run(() => {
    39      // Fetch the model
    40      this.store.findRecord('node', 'node-1').then(model => {
    41        node = model;
    42  
    43        // Fetch the related allocations
    44        return findHasMany(model, 'allocations').then(allocations => {
    45          assert.equal(
    46            allocations.get('length'),
    47            this.server.db.allocations.where({ nodeId: node.get('id') }).length,
    48            'Allocations returned from the findHasMany matches the db state'
    49          );
    50        });
    51      });
    52    });
    53  
    54    return wait().then(() => {
    55      server.db.allocations.remove('node-1-1');
    56  
    57      run(() => {
    58        // Reload the related allocations now that one was removed server-side
    59        return findHasMany(node, 'allocations').then(allocations => {
    60          const dbAllocations = this.server.db.allocations.where({ nodeId: node.get('id') });
    61          assert.equal(
    62            allocations.get('length'),
    63            dbAllocations.length,
    64            'Allocations returned from the findHasMany matches the db state'
    65          );
    66          assert.equal(
    67            this.store.peekAll('allocation').get('length'),
    68            dbAllocations.length,
    69            'Server-side deleted allocation was removed from the store'
    70          );
    71        });
    72      });
    73    });
    74  });
    75  
    76  test('findHasMany does not remove old unrelated models from the store', function(assert) {
    77    let node;
    78  
    79    run(() => {
    80      // Fetch the first node and related allocations
    81      this.store.findRecord('node', 'node-1').then(model => {
    82        node = model;
    83        return findHasMany(model, 'allocations');
    84      });
    85  
    86      // Also fetch the second node and related allocations;
    87      this.store.findRecord('node', 'node-2').then(model => findHasMany(model, 'allocations'));
    88    });
    89  
    90    return wait().then(() => {
    91      assert.deepEqual(
    92        this.store
    93          .peekAll('allocation')
    94          .mapBy('id')
    95          .sort(),
    96        ['node-1-1', 'node-1-2', 'node-2-1', 'node-2-2'],
    97        'All allocations for the first and second node are in the store'
    98      );
    99  
   100      server.db.allocations.remove('node-1-1');
   101  
   102      run(() => {
   103        // Reload the related allocations now that one was removed server-side
   104        return findHasMany(node, 'allocations').then(() => {
   105          assert.deepEqual(
   106            this.store
   107              .peekAll('allocation')
   108              .mapBy('id')
   109              .sort(),
   110            ['node-1-2', 'node-2-1', 'node-2-2'],
   111            'The deleted allocation is removed from the store and the allocations associated with the other node are untouched'
   112          );
   113        });
   114      });
   115    });
   116  });
   117  
   118  // Using fetchLink on a model's hasMany relationship exercises the adapter's
   119  // findHasMany method as well normalizing the response and pushing it to the store
   120  function findHasMany(model, relationshipName) {
   121    const relationship = model.relationshipFor(relationshipName);
   122    return model.hasMany(relationship.key).hasManyRelationship.fetchLink();
   123  }