@ndustrial/contxt-sdk 4.3.6 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/.gitattributes +1 -0
  2. package/CHANGELOG.md +8 -0
  3. package/docs/Facilities.md +0 -144
  4. package/docs/FacilityGroupings.md +0 -160
  5. package/docs/Nionic.md +63 -0
  6. package/docs/README.md +3 -29
  7. package/docs/Typedefs.md +0 -101
  8. package/esm/config/audiences.js +13 -13
  9. package/esm/config/audiences.js.map +1 -1
  10. package/esm/index.js +3 -6
  11. package/esm/index.js.map +1 -1
  12. package/esm/nionic/index.js +126 -0
  13. package/esm/nionic/index.js.map +1 -0
  14. package/lib/config/audiences.js +13 -13
  15. package/lib/config/audiences.js.map +1 -1
  16. package/lib/index.js +19 -13
  17. package/lib/index.js.map +1 -1
  18. package/lib/nionic/index.js +130 -0
  19. package/lib/nionic/index.js.map +1 -0
  20. package/meta.yaml +11 -0
  21. package/package.json +9 -10
  22. package/src/bus/index.spec.js +3 -2
  23. package/src/bus/webSocketConnection.spec.js +31 -31
  24. package/src/config/audiences.js +13 -13
  25. package/src/config/index.spec.js +6 -6
  26. package/src/coordinator/applications.spec.js +8 -8
  27. package/src/coordinator/consent.spec.js +1 -1
  28. package/src/coordinator/edgeNodes.spec.js +3 -3
  29. package/src/coordinator/organizations.spec.js +4 -4
  30. package/src/coordinator/permissions.spec.js +4 -4
  31. package/src/coordinator/roles.spec.js +3 -3
  32. package/src/coordinator/users.spec.js +14 -15
  33. package/src/events/index.spec.js +16 -16
  34. package/src/files/index.spec.js +7 -7
  35. package/src/index.js +3 -6
  36. package/src/index.spec.js +8 -22
  37. package/src/iot/feeds.spec.js +4 -4
  38. package/src/iot/fieldCategories.spec.js +4 -4
  39. package/src/iot/fieldGroupings.spec.js +5 -5
  40. package/src/iot/outputs.spec.js +6 -6
  41. package/src/nionic/index.js +151 -0
  42. package/src/request.spec.js +3 -3
  43. package/src/sessionTypes/auth0WebAuth.spec.js +2 -2
  44. package/src/sessionTypes/index.spec.js +3 -3
  45. package/src/sessionTypes/machineAuth.spec.js +3 -3
  46. package/src/sessionTypes/passwordGrantAuth.spec.js +1 -1
  47. package/src/utils/iot/formatOutputFieldDataFromServer.spec.js +6 -6
  48. package/src/utils/iot/parseOutputFieldNextPageUrlMetadata.spec.js +2 -2
  49. package/src/utils/pagination/formatPaginatedDataFromServer.spec.js +2 -2
  50. package/src/utils/url/stringifyParams.spec.js +2 -3
  51. package/support/fixtures/factories/IOSDevice.js +3 -3
  52. package/support/fixtures/factories/applicationGrouping.js +4 -4
  53. package/support/fixtures/factories/applicationModule.js +2 -2
  54. package/support/fixtures/factories/asset.js +1 -1
  55. package/support/fixtures/factories/assetAttribute.js +2 -2
  56. package/support/fixtures/factories/assetAttributeValue.js +2 -2
  57. package/support/fixtures/factories/assetMetric.js +1 -1
  58. package/support/fixtures/factories/assetMetricValue.js +3 -3
  59. package/support/fixtures/factories/assetMetricValueCompact.js +2 -2
  60. package/support/fixtures/factories/assetType.js +1 -1
  61. package/support/fixtures/factories/authResults.js +3 -3
  62. package/support/fixtures/factories/channel.js +1 -1
  63. package/support/fixtures/factories/contxtApplication.js +5 -5
  64. package/support/fixtures/factories/contxtOrganization.js +2 -2
  65. package/support/fixtures/factories/contxtOrganizationFeaturedApplication.js +3 -3
  66. package/support/fixtures/factories/contxtProject.js +2 -2
  67. package/support/fixtures/factories/contxtProjectEnvironment.js +2 -2
  68. package/support/fixtures/factories/contxtRole.js +3 -3
  69. package/support/fixtures/factories/contxtRoleApplication.js +1 -1
  70. package/support/fixtures/factories/contxtRoleProjectEnvironment.js +1 -1
  71. package/support/fixtures/factories/contxtUser.js +2 -2
  72. package/support/fixtures/factories/contxtUserApplication.js +1 -1
  73. package/support/fixtures/factories/contxtUserFavoriteApplication.js +2 -2
  74. package/support/fixtures/factories/contxtUserPermissions.js +5 -5
  75. package/support/fixtures/factories/contxtUserProjectEnvironment.js +1 -1
  76. package/support/fixtures/factories/contxtUserRole.js +2 -2
  77. package/support/fixtures/factories/costCenter.js +2 -2
  78. package/support/fixtures/factories/costCenterFacility.js +3 -3
  79. package/support/fixtures/factories/edgeNode.js +1 -1
  80. package/support/fixtures/factories/event.js +5 -5
  81. package/support/fixtures/factories/eventType.js +4 -4
  82. package/support/fixtures/factories/eventUser.js +5 -5
  83. package/support/fixtures/factories/facility.js +4 -4
  84. package/support/fixtures/factories/facilityGrouping.js +3 -3
  85. package/support/fixtures/factories/facilityGroupingFacility.js +3 -3
  86. package/support/fixtures/factories/facilityInfo.js +2 -2
  87. package/support/fixtures/factories/facilityTag.js +1 -1
  88. package/support/fixtures/factories/feedStatus.js +1 -1
  89. package/support/fixtures/factories/feedType.js +1 -1
  90. package/support/fixtures/factories/feeds.js +5 -5
  91. package/support/fixtures/factories/fieldCategory.js +3 -3
  92. package/support/fixtures/factories/fieldGrouping.js +4 -4
  93. package/support/fixtures/factories/fieldGroupingField.js +3 -3
  94. package/support/fixtures/factories/fieldGroupingStatus.js +1 -1
  95. package/support/fixtures/factories/file.js +1 -1
  96. package/support/fixtures/factories/fileUploadInfo.js +1 -1
  97. package/support/fixtures/factories/organization.js +1 -1
  98. package/support/fixtures/factories/outputField.js +10 -10
  99. package/support/fixtures/factories/outputFieldData.js +1 -1
  100. package/support/fixtures/factories/owner.js +2 -2
  101. package/support/fixtures/factories/paginationMetadata.js +2 -2
  102. package/support/fixtures/factories/triggeredEvent.js +4 -4
  103. package/support/fixtures/factories/userEventSubscription.js +1 -1
  104. package/support/fixtures/factories/userMobileNumber.js +1 -1
  105. package/support/fixtures/factories/userPermissionsMap.js +2 -2
  106. package/.huskyrc.js +0 -11
  107. package/.nycrc +0 -12
  108. package/esm/assets/assetAttributes.js +0 -574
  109. package/esm/assets/assetAttributes.js.map +0 -1
  110. package/esm/assets/assetMetrics.js +0 -690
  111. package/esm/assets/assetMetrics.js.map +0 -1
  112. package/esm/assets/assetTypes.js +0 -273
  113. package/esm/assets/assetTypes.js.map +0 -1
  114. package/esm/assets/index.js +0 -283
  115. package/esm/assets/index.js.map +0 -1
  116. package/esm/facilities/costCenters.js +0 -306
  117. package/esm/facilities/costCenters.js.map +0 -1
  118. package/esm/facilities/groupings.js +0 -323
  119. package/esm/facilities/groupings.js.map +0 -1
  120. package/esm/facilities/index.js +0 -343
  121. package/esm/facilities/index.js.map +0 -1
  122. package/esm/health/index.js +0 -229
  123. package/esm/health/index.js.map +0 -1
  124. package/esm/utils/facilities/formatFacilityWithInfoFromServer.js +0 -65
  125. package/esm/utils/facilities/formatFacilityWithInfoFromServer.js.map +0 -1
  126. package/esm/utils/facilities/index.js +0 -4
  127. package/esm/utils/facilities/index.js.map +0 -1
  128. package/lib/assets/assetAttributes.js +0 -588
  129. package/lib/assets/assetAttributes.js.map +0 -1
  130. package/lib/assets/assetMetrics.js +0 -705
  131. package/lib/assets/assetMetrics.js.map +0 -1
  132. package/lib/assets/assetTypes.js +0 -287
  133. package/lib/assets/assetTypes.js.map +0 -1
  134. package/lib/assets/index.js +0 -302
  135. package/lib/assets/index.js.map +0 -1
  136. package/lib/facilities/costCenters.js +0 -316
  137. package/lib/facilities/costCenters.js.map +0 -1
  138. package/lib/facilities/groupings.js +0 -333
  139. package/lib/facilities/groupings.js.map +0 -1
  140. package/lib/facilities/index.js +0 -360
  141. package/lib/facilities/index.js.map +0 -1
  142. package/lib/health/index.js +0 -232
  143. package/lib/health/index.js.map +0 -1
  144. package/lib/utils/facilities/formatFacilityWithInfoFromServer.js +0 -71
  145. package/lib/utils/facilities/formatFacilityWithInfoFromServer.js.map +0 -1
  146. package/lib/utils/facilities/index.js +0 -15
  147. package/lib/utils/facilities/index.js.map +0 -1
  148. package/src/assets/assetAttributes.js +0 -619
  149. package/src/assets/assetAttributes.spec.js +0 -1143
  150. package/src/assets/assetMetrics.js +0 -736
  151. package/src/assets/assetMetrics.spec.js +0 -1180
  152. package/src/assets/assetTypes.js +0 -260
  153. package/src/assets/assetTypes.spec.js +0 -556
  154. package/src/assets/index.js +0 -271
  155. package/src/assets/index.spec.js +0 -515
  156. package/src/facilities/costCenters.js +0 -295
  157. package/src/facilities/costCenters.spec.js +0 -529
  158. package/src/facilities/groupings.js +0 -320
  159. package/src/facilities/groupings.spec.js +0 -567
  160. package/src/facilities/index.js +0 -343
  161. package/src/facilities/index.spec.js +0 -551
  162. package/src/health/index.js +0 -200
  163. package/src/health/index.spec.js +0 -501
  164. package/src/utils/facilities/formatFacilityWithInfoFromServer.js +0 -62
  165. package/src/utils/facilities/formatFacilityWithInfoFromServer.spec.js +0 -73
  166. package/src/utils/facilities/index.js +0 -3
@@ -1,556 +0,0 @@
1
- import omit from 'lodash.omit';
2
- import AssetTypes from './assetTypes';
3
- import * as objectUtils from '../utils/objects';
4
- import * as paginationUtils from '../utils/pagination';
5
-
6
- describe('Assets/Types', function() {
7
- let baseRequest;
8
- let baseSdk;
9
- let expectedHost;
10
-
11
- beforeEach(function() {
12
- baseRequest = {
13
- delete: sinon.stub().resolves(),
14
- get: sinon.stub().resolves(),
15
- post: sinon.stub().resolves(),
16
- put: sinon.stub().resolves()
17
- };
18
- baseSdk = {
19
- config: {
20
- audiences: {
21
- facilities: fixture.build('audience')
22
- }
23
- }
24
- };
25
- expectedHost = faker.internet.url();
26
- });
27
-
28
- afterEach(function() {
29
- sinon.restore();
30
- });
31
-
32
- describe('constructor', function() {
33
- let assetTypes;
34
-
35
- beforeEach(function() {
36
- assetTypes = new AssetTypes(baseSdk, baseRequest, expectedHost);
37
- });
38
-
39
- it('sets a base url for the class instance', function() {
40
- expect(assetTypes._baseUrl).to.equal(expectedHost);
41
- });
42
-
43
- it('appends the supplied request module to the class instance', function() {
44
- expect(assetTypes._request).to.deep.equal(baseRequest);
45
- });
46
-
47
- it('appends the supplied sdk to the class instance', function() {
48
- expect(assetTypes._sdk).to.deep.equal(baseSdk);
49
- });
50
- });
51
-
52
- describe('create', function() {
53
- context(
54
- 'when creating an asset type tied to a specific organization',
55
- function() {
56
- let assetTypeFromServerAfterFormat;
57
- let assetTypeFromServerBeforeFormat;
58
- let assetTypeToServerAfterFormat;
59
- let assetTypeToServerBeforeFormat;
60
- let promise;
61
- let request;
62
- let toCamelCase;
63
- let toSnakeCase;
64
-
65
- beforeEach(function() {
66
- assetTypeFromServerAfterFormat = fixture.build('assetType');
67
- assetTypeFromServerBeforeFormat = fixture.build('assetType', null, {
68
- fromServer: true
69
- });
70
- assetTypeToServerAfterFormat = fixture.build('assetType', null, {
71
- fromServer: true
72
- });
73
- assetTypeToServerBeforeFormat = fixture.build('assetType');
74
-
75
- request = {
76
- ...baseRequest,
77
- post: sinon.stub().resolves(assetTypeFromServerBeforeFormat)
78
- };
79
- toCamelCase = sinon
80
- .stub(objectUtils, 'toCamelCase')
81
- .returns(assetTypeFromServerAfterFormat);
82
- toSnakeCase = sinon
83
- .stub(objectUtils, 'toSnakeCase')
84
- .returns(assetTypeToServerAfterFormat);
85
-
86
- const assetTypes = new AssetTypes(baseSdk, request, expectedHost);
87
-
88
- promise = assetTypes.create(assetTypeToServerBeforeFormat);
89
- });
90
-
91
- it('formats the submitted asset type object to send to the server', function() {
92
- expect(toSnakeCase).to.be.deep.calledWith(
93
- assetTypeToServerBeforeFormat
94
- );
95
- });
96
-
97
- it('creates a new asset type', function() {
98
- expect(request.post).to.be.deep.calledWith(
99
- `${expectedHost}/assets/types`,
100
- assetTypeToServerAfterFormat
101
- );
102
- });
103
-
104
- it('formats the returned asset type object', function() {
105
- return promise.then(() => {
106
- expect(toCamelCase).to.be.deep.calledWith(
107
- assetTypeFromServerBeforeFormat
108
- );
109
- });
110
- });
111
-
112
- it('returns a fulfilled promise with the new asset type information', function() {
113
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
114
- assetTypeFromServerAfterFormat
115
- );
116
- });
117
- }
118
- );
119
-
120
- context('when creating a global asset type', function() {
121
- let assetTypeFromServerAfterFormat;
122
- let assetTypeFromServerBeforeFormat;
123
- let assetTypeToServerAfterFormat;
124
- let assetTypeToServerBeforeFormat;
125
- let promise;
126
- let request;
127
- let toCamelCase;
128
- let toSnakeCase;
129
-
130
- beforeEach(function() {
131
- assetTypeFromServerBeforeFormat = fixture.build('assetType', {
132
- organizationId: null
133
- });
134
- assetTypeFromServerAfterFormat = fixture.build(
135
- 'assetType',
136
- assetTypeToServerBeforeFormat,
137
- { fromServer: true }
138
- );
139
- assetTypeToServerBeforeFormat = fixture.build('assetType', {
140
- organizationId: null
141
- });
142
- assetTypeToServerAfterFormat = fixture.build(
143
- 'assetType',
144
- assetTypeToServerBeforeFormat,
145
- { fromServer: true }
146
- );
147
-
148
- request = {
149
- ...baseRequest,
150
- post: sinon.stub().resolves(assetTypeFromServerBeforeFormat)
151
- };
152
- toCamelCase = sinon
153
- .stub(objectUtils, 'toCamelCase')
154
- .returns(assetTypeFromServerAfterFormat);
155
- toSnakeCase = sinon
156
- .stub(objectUtils, 'toSnakeCase')
157
- .returns(assetTypeToServerAfterFormat);
158
-
159
- const assetTypes = new AssetTypes(baseSdk, request, expectedHost);
160
-
161
- promise = assetTypes.create(assetTypeToServerBeforeFormat);
162
- });
163
-
164
- it('formats the submitted asset type object to send to the server', function() {
165
- expect(toSnakeCase).to.be.deep.calledWith(
166
- assetTypeToServerBeforeFormat
167
- );
168
- });
169
-
170
- it('creates a new asset type', function() {
171
- expect(request.post).to.be.deep.calledWith(
172
- `${expectedHost}/assets/types`,
173
- assetTypeToServerAfterFormat
174
- );
175
- });
176
-
177
- it('formats the returned asset type object', function() {
178
- return promise.then(() => {
179
- expect(toCamelCase).to.be.deep.calledWith(
180
- assetTypeFromServerBeforeFormat
181
- );
182
- });
183
- });
184
-
185
- it('returns a fulfilled promise with the new asset type information', function() {
186
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
187
- assetTypeFromServerAfterFormat
188
- );
189
- });
190
- });
191
-
192
- context('when there is missing required information', function() {
193
- ['description', 'label', 'organizationId'].forEach((field) => {
194
- it(`it throws an error when ${field} is missing`, function() {
195
- const assetType = fixture.build('assetType');
196
- const assetTypes = new AssetTypes(baseSdk, baseRequest, expectedHost);
197
-
198
- const promise = assetTypes.create(omit(assetType, [field]));
199
-
200
- return expect(promise).to.be.rejectedWith(
201
- `A ${field} is required to create a new asset type.`
202
- );
203
- });
204
- });
205
- });
206
- });
207
-
208
- describe('delete', function() {
209
- context('when all required information is supplied', function() {
210
- let expectedAssetTypeId;
211
- let promise;
212
-
213
- beforeEach(function() {
214
- expectedAssetTypeId = fixture.build('assetType').id;
215
-
216
- const assetTypes = new AssetTypes(baseSdk, baseRequest, expectedHost);
217
-
218
- promise = assetTypes.delete(expectedAssetTypeId);
219
- });
220
-
221
- it('request to deletes the asset type', function() {
222
- expect(baseRequest.delete).to.be.calledWith(
223
- `${expectedHost}/assets/types/${expectedAssetTypeId}`
224
- );
225
- });
226
-
227
- it('returns a fulfilled promise', function() {
228
- return expect(promise).to.be.fulfilled;
229
- });
230
- });
231
-
232
- context('when there is missing required information', function() {
233
- it('throws an error when the asset type ID is missing', function() {
234
- const assetTypes = new AssetTypes(baseSdk, baseRequest, expectedHost);
235
-
236
- const promise = assetTypes.delete();
237
-
238
- return expect(promise).to.be.rejectedWith(
239
- 'An asset type ID is required for deleting an asset type.'
240
- );
241
- });
242
- });
243
- });
244
-
245
- describe('get', function() {
246
- context('the asset type ID is provided', function() {
247
- let assetTypeFromServerAfterFormat;
248
- let assetTypeFromServerBeforeFormat;
249
- let expectedAssetTypeId;
250
- let promise;
251
- let request;
252
- let toCamelCase;
253
-
254
- beforeEach(function() {
255
- expectedAssetTypeId = fixture.build('assetType').id;
256
- assetTypeFromServerAfterFormat = fixture.build('assetType', {
257
- id: expectedAssetTypeId
258
- });
259
- assetTypeFromServerBeforeFormat = fixture.build(
260
- 'assetType',
261
- assetTypeFromServerAfterFormat,
262
- { fromServer: true }
263
- );
264
-
265
- request = {
266
- ...baseRequest,
267
- get: sinon.stub().resolves(assetTypeFromServerBeforeFormat)
268
- };
269
- toCamelCase = sinon
270
- .stub(objectUtils, 'toCamelCase')
271
- .returns(assetTypeFromServerAfterFormat);
272
-
273
- const assetTypes = new AssetTypes(baseSdk, request, expectedHost);
274
-
275
- promise = assetTypes.get(expectedAssetTypeId);
276
- });
277
-
278
- it('gets the asset type from the server', function() {
279
- expect(request.get).to.be.calledWith(
280
- `${expectedHost}/assets/types/${expectedAssetTypeId}`
281
- );
282
- });
283
-
284
- it('formats the asset type object', function() {
285
- return promise.then(() => {
286
- expect(toCamelCase).to.be.deep.calledWith(
287
- assetTypeFromServerBeforeFormat
288
- );
289
- });
290
- });
291
-
292
- it('returns the requested asset type', function() {
293
- return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
294
- assetTypeFromServerAfterFormat
295
- );
296
- });
297
- });
298
-
299
- context('the asset type ID is not provided', function() {
300
- it('throws an error', function() {
301
- const assetTypes = new AssetTypes(baseSdk, baseRequest, expectedHost);
302
- const promise = assetTypes.get();
303
-
304
- return expect(promise).to.be.rejectedWith(
305
- 'An asset type ID is required for getting information about an asset type.'
306
- );
307
- });
308
- });
309
- });
310
-
311
- describe('getAll', function() {
312
- let assetTypesFromServerAfterFormat;
313
- let assetTypesFromServerBeforeFormat;
314
- let formatPaginatedDataFromServer;
315
- let numberOfAssetTypes;
316
- let paginationOptionsAfterFormat;
317
- let paginationOptionsBeforeFormat;
318
- let promise;
319
- let request;
320
- let toSnakeCase;
321
-
322
- beforeEach(function() {
323
- numberOfAssetTypes = faker.random.number({ min: 1, max: 10 });
324
- assetTypesFromServerAfterFormat = {
325
- _metadata: fixture.build('paginationMetadata'),
326
- records: fixture.buildList('assetType', numberOfAssetTypes)
327
- };
328
- assetTypesFromServerBeforeFormat = {
329
- ...assetTypesFromServerAfterFormat,
330
- records: assetTypesFromServerAfterFormat.records.map((asset) =>
331
- fixture.build('assetType', asset, { fromServer: true })
332
- )
333
- };
334
- paginationOptionsBeforeFormat = {
335
- limit: faker.random.number({ min: 10, max: 1000 }),
336
- offset: faker.random.number({ max: 1000 })
337
- };
338
- paginationOptionsAfterFormat = {
339
- ...paginationOptionsBeforeFormat
340
- };
341
-
342
- formatPaginatedDataFromServer = sinon
343
- .stub(paginationUtils, 'formatPaginatedDataFromServer')
344
- .returns(assetTypesFromServerAfterFormat);
345
- request = {
346
- ...baseRequest,
347
- get: sinon.stub().resolves(assetTypesFromServerBeforeFormat)
348
- };
349
- toSnakeCase = sinon
350
- .stub(objectUtils, 'toSnakeCase')
351
- .returns(paginationOptionsAfterFormat);
352
-
353
- const assetTypes = new AssetTypes(baseSdk, request, expectedHost);
354
-
355
- promise = assetTypes.getAll(paginationOptionsBeforeFormat);
356
- });
357
-
358
- it('formats the pagination options', function() {
359
- expect(toSnakeCase).to.be.calledWith(paginationOptionsBeforeFormat);
360
- });
361
-
362
- it('gets a list of the asset types from the server', function() {
363
- expect(request.get).to.be.calledWith(`${expectedHost}/assets/types`, {
364
- params: paginationOptionsAfterFormat
365
- });
366
- });
367
-
368
- it('formats the asset type object', function() {
369
- return promise.then(() => {
370
- expect(formatPaginatedDataFromServer).to.be.calledWith(
371
- assetTypesFromServerBeforeFormat
372
- );
373
- });
374
- });
375
-
376
- it('returns a list of asset types', function() {
377
- return expect(promise).to.be.fulfilled.and.to.eventually.equal(
378
- assetTypesFromServerAfterFormat
379
- );
380
- });
381
- });
382
-
383
- describe('getAllByOrganizationId', function() {
384
- context('the organization ID is provided', function() {
385
- let assetTypesFromServerAfterFormat;
386
- let assetTypesFromServerBeforeFormat;
387
- let expectedOrganizationId;
388
- let formatPaginatedDataFromServer;
389
- let numberOfAssetTypes;
390
- let paginationOptionsAfterFormat;
391
- let paginationOptionsBeforeFormat;
392
- let promise;
393
- let request;
394
- let toSnakeCase;
395
-
396
- beforeEach(function() {
397
- expectedOrganizationId = fixture.build('organization').id;
398
- numberOfAssetTypes = faker.random.number({ min: 1, max: 10 });
399
- assetTypesFromServerAfterFormat = {
400
- _metadata: fixture.build('paginationMetadata'),
401
- records: fixture.buildList('assetType', numberOfAssetTypes)
402
- };
403
- assetTypesFromServerBeforeFormat = {
404
- ...assetTypesFromServerAfterFormat,
405
- records: assetTypesFromServerAfterFormat.records.map((asset) =>
406
- fixture.build('assetType', asset, { fromServer: true })
407
- )
408
- };
409
- paginationOptionsBeforeFormat = {
410
- limit: faker.random.number({ min: 10, max: 1000 }),
411
- offset: faker.random.number({ max: 1000 })
412
- };
413
- paginationOptionsAfterFormat = {
414
- ...paginationOptionsBeforeFormat
415
- };
416
-
417
- formatPaginatedDataFromServer = sinon
418
- .stub(paginationUtils, 'formatPaginatedDataFromServer')
419
- .returns(assetTypesFromServerAfterFormat);
420
- request = {
421
- ...baseRequest,
422
- get: sinon.stub().resolves(assetTypesFromServerBeforeFormat)
423
- };
424
- toSnakeCase = sinon
425
- .stub(objectUtils, 'toSnakeCase')
426
- .returns(paginationOptionsAfterFormat);
427
-
428
- const assetTypes = new AssetTypes(baseSdk, request, expectedHost);
429
-
430
- promise = assetTypes.getAllByOrganizationId(
431
- expectedOrganizationId,
432
- paginationOptionsBeforeFormat
433
- );
434
- });
435
-
436
- it('formats the pagination options', function() {
437
- expect(toSnakeCase).to.be.calledWith(paginationOptionsBeforeFormat);
438
- });
439
-
440
- it('gets a list of asset types for an organization from the server', function() {
441
- expect(request.get).to.be.calledWith(
442
- `${expectedHost}/organizations/${expectedOrganizationId}/assets/types`,
443
- { params: paginationOptionsAfterFormat }
444
- );
445
- });
446
-
447
- it('formats the asset type object', function() {
448
- return promise.then(() => {
449
- expect(formatPaginatedDataFromServer).to.be.calledWith(
450
- assetTypesFromServerBeforeFormat
451
- );
452
- });
453
- });
454
-
455
- it('returns a list of asset types', function() {
456
- return expect(promise).to.be.fulfilled.and.to.eventually.equal(
457
- assetTypesFromServerAfterFormat
458
- );
459
- });
460
- });
461
-
462
- context('the organization ID is not provided', function() {
463
- it('throws an error', function() {
464
- const assetTypes = new AssetTypes(baseSdk, baseRequest, expectedHost);
465
- const promise = assetTypes.getAllByOrganizationId();
466
-
467
- return expect(promise).to.be.rejectedWith(
468
- "An organization ID is required for getting a list of an organization's asset types."
469
- );
470
- });
471
- });
472
- });
473
-
474
- describe('update', function() {
475
- context('when all required information is available', function() {
476
- let assetTypeToServerAfterFormat;
477
- let assetTypeToServerBeforeFormat;
478
- let promise;
479
- let toSnakeCase;
480
-
481
- beforeEach(function() {
482
- assetTypeToServerAfterFormat = fixture.build('assetType', null, {
483
- fromServer: true
484
- });
485
- assetTypeToServerBeforeFormat = fixture.build('assetType');
486
-
487
- toSnakeCase = sinon
488
- .stub(objectUtils, 'toSnakeCase')
489
- .returns(assetTypeToServerAfterFormat);
490
-
491
- const assetTypes = new AssetTypes(baseSdk, baseRequest, expectedHost);
492
-
493
- promise = assetTypes.update(
494
- assetTypeToServerBeforeFormat.id,
495
- assetTypeToServerBeforeFormat
496
- );
497
- });
498
-
499
- it('formats the data into the right format', function() {
500
- expect(toSnakeCase).to.be.calledWith(assetTypeToServerBeforeFormat, {
501
- excludeKeys: ['id', 'label', 'organizationId']
502
- });
503
- });
504
-
505
- it('updates the asset type', function() {
506
- expect(baseRequest.put).to.be.deep.calledWith(
507
- `${expectedHost}/assets/types/${assetTypeToServerBeforeFormat.id}`
508
- );
509
- });
510
-
511
- it('returns a fulfilled promise', function() {
512
- return expect(promise).to.be.fulfilled;
513
- });
514
- });
515
-
516
- context(
517
- 'when there is missing or malformed required information',
518
- function() {
519
- let assetTypes;
520
-
521
- beforeEach(function() {
522
- assetTypes = new AssetTypes(baseSdk, baseRequest, expectedHost);
523
- });
524
-
525
- it('throws an error when there is no provided asset type ID', function() {
526
- const assetTypeUpdate = fixture.build('assetType');
527
- const promise = assetTypes.update(null, assetTypeUpdate);
528
-
529
- return expect(promise).to.be.rejectedWith(
530
- 'An asset type ID is required to update an asset type.'
531
- );
532
- });
533
-
534
- it('throws an error when there is no updated provided', function() {
535
- const assetTypeUpdate = fixture.build('assetType');
536
- const promise = assetTypes.update(assetTypeUpdate.id);
537
-
538
- return expect(promise).to.be.rejectedWith(
539
- 'An update is required to update an asset type.'
540
- );
541
- });
542
-
543
- it('throws an error when the update is not a well-formed object', function() {
544
- const assetTypeUpdate = fixture.build('assetType');
545
- const promise = assetTypes.update(assetTypeUpdate.id, [
546
- assetTypeUpdate
547
- ]);
548
-
549
- return expect(promise).to.be.rejectedWith(
550
- 'The asset type update must be a well-formed object with the data you wish to update.'
551
- );
552
- });
553
- }
554
- );
555
- });
556
- });