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