github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/ui/tests/unit/adapters/node-test.js (about)

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