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