github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/unit/serializers/node-test.js (about) 1 import { module, test } from 'qunit'; 2 import { setupTest } from 'ember-qunit'; 3 import { run } from '@ember/runloop'; 4 import NodeModel from 'nomad-ui/models/node'; 5 import pushPayloadToStore from '../../utils/push-payload-to-store'; 6 import { settled } from '@ember/test-helpers'; 7 8 module('Unit | Serializer | Node', function(hooks) { 9 setupTest(hooks); 10 hooks.beforeEach(function() { 11 this.store = this.owner.lookup('service:store'); 12 this.subject = () => this.store.serializerFor('node'); 13 }); 14 15 test('local store is culled to reflect the state of findAll requests', async function(assert) { 16 const findAllResponse = [ 17 makeNode('1', 'One', '127.0.0.1:4646'), 18 makeNode('2', 'Two', '127.0.0.2:4646'), 19 makeNode('3', 'Three', '127.0.0.3:4646'), 20 ]; 21 22 const payload = this.subject().normalizeFindAllResponse(this.store, NodeModel, findAllResponse); 23 pushPayloadToStore(this.store, payload, NodeModel.modelName); 24 25 assert.equal( 26 payload.data.length, 27 findAllResponse.length, 28 'Each original record is returned in the response' 29 ); 30 31 assert.equal( 32 this.store 33 .peekAll('node') 34 .filterBy('id') 35 .get('length'), 36 findAllResponse.length, 37 'Each original record is now in the store' 38 ); 39 40 const newFindAllResponse = [ 41 makeNode('2', 'Two', '127.0.0.2:4646'), 42 makeNode('3', 'Three', '127.0.0.3:4646'), 43 makeNode('4', 'Four', '127.0.0.4:4646'), 44 ]; 45 46 let newPayload; 47 run(() => { 48 newPayload = this.subject().normalizeFindAllResponse( 49 this.store, 50 NodeModel, 51 newFindAllResponse 52 ); 53 }); 54 pushPayloadToStore(this.store, newPayload, NodeModel.modelName); 55 56 await settled(); 57 assert.equal( 58 newPayload.data.length, 59 newFindAllResponse.length, 60 'Each new record is returned in the response' 61 ); 62 63 assert.equal( 64 this.store 65 .peekAll('node') 66 .filterBy('id') 67 .get('length'), 68 newFindAllResponse.length, 69 'The node length in the store reflects the new response' 70 ); 71 72 assert.notOk(this.store.peekAll('node').findBy('id', '1'), 'Record One is no longer found'); 73 }); 74 75 function makeNode(id, name, ip) { 76 return { ID: id, Name: name, HTTPAddr: ip }; 77 } 78 79 const normalizationTestCases = [ 80 { 81 name: 'Normal', 82 in: { 83 ID: 'test-node', 84 HTTPAddr: '867.53.0.9:4646', 85 Drain: false, 86 Drivers: { 87 docker: { 88 Detected: true, 89 Healthy: false, 90 }, 91 }, 92 HostVolumes: { 93 one: { 94 Name: 'one', 95 ReadOnly: true, 96 }, 97 two: { 98 Name: 'two', 99 ReadOnly: false, 100 }, 101 }, 102 }, 103 out: { 104 data: { 105 id: 'test-node', 106 type: 'node', 107 attributes: { 108 isDraining: false, 109 httpAddr: '867.53.0.9:4646', 110 drivers: [ 111 { 112 name: 'docker', 113 detected: true, 114 healthy: false, 115 }, 116 ], 117 hostVolumes: [{ name: 'one', readOnly: true }, { name: 'two', readOnly: false }], 118 }, 119 relationships: { 120 allocations: { 121 links: { 122 related: '/v1/node/test-node/allocations', 123 }, 124 }, 125 }, 126 }, 127 }, 128 }, 129 130 { 131 name: 'Dots in driver names', 132 in: { 133 ID: 'test-node', 134 HTTPAddr: '867.53.0.9:4646', 135 Drain: false, 136 Drivers: { 137 'my.driver': { 138 Detected: true, 139 Healthy: false, 140 }, 141 'my.other.driver': { 142 Detected: false, 143 Healthy: false, 144 }, 145 }, 146 }, 147 out: { 148 data: { 149 id: 'test-node', 150 type: 'node', 151 attributes: { 152 isDraining: false, 153 httpAddr: '867.53.0.9:4646', 154 drivers: [ 155 { 156 name: 'my.driver', 157 detected: true, 158 healthy: false, 159 }, 160 { 161 name: 'my.other.driver', 162 detected: false, 163 healthy: false, 164 }, 165 ], 166 hostVolumes: [], 167 }, 168 relationships: { 169 allocations: { 170 links: { 171 related: '/v1/node/test-node/allocations', 172 }, 173 }, 174 }, 175 }, 176 }, 177 }, 178 179 { 180 name: 'Null hash values', 181 in: { 182 ID: 'test-node', 183 Drivers: null, 184 HostVolumes: null, 185 }, 186 out: { 187 data: { 188 id: 'test-node', 189 type: 'node', 190 attributes: { 191 hostVolumes: [], 192 drivers: [], 193 }, 194 relationships: { 195 allocations: { 196 links: { 197 related: '/v1/node/test-node/allocations', 198 }, 199 }, 200 }, 201 }, 202 }, 203 }, 204 ]; 205 206 normalizationTestCases.forEach(testCase => { 207 test(`normalization: ${testCase.name}`, async function(assert) { 208 assert.deepEqual(this.subject().normalize(NodeModel, testCase.in), testCase.out); 209 }); 210 }); 211 });