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