diff --git a/README.md b/README.md index 9bb78d5..e380ff5 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ To run the app, follow these steps. ## Browser Support -This project is only going to support the latest browsers (Chrome/Firefox/IE11/Edge) and therefore polyfills for MutationObserver and support for IE9/IE10 is being dropped. +This project is only going to support the latest browsers (Chrome/Firefox/Edge). ## Connecting to Webservices in Development @@ -92,10 +92,11 @@ Webdriver for NodeJS is used for the integration tests. This project has been mo The following is a list of test statistics for the project for date -| Date | Commit | Number of Tests | Code Coverage | -| --- | --- | --- |---------------| -| 2022-01-15 | [8ac447f](https://github.com/stamp-web/stamp-web-aurelia/commit/8ac447f580f29d1f0f8dd23e284c6f25448cf1d7) | 83 | 12.05% | -| 2022-01-15 | [081fe3f](https://github.com/stamp-web/stamp-web-aurelia/commit/081fe3f31d5962c10777f4017e2c7a5dbe26e12e) | 86 | 12.20% | +| Date | Commit | Number of Tests | Code Coverage | +| --- |-----------------------------------------------------------------------------------------------------------|-----------------|---------------| +| 2022-01-15 | [8ac447f](https://github.com/stamp-web/stamp-web-aurelia/commit/8ac447f580f29d1f0f8dd23e284c6f25448cf1d7) | 83 | 12.05% | +| 2022-01-15 | [081fe3f](https://github.com/stamp-web/stamp-web-aurelia/commit/081fe3f31d5962c10777f4017e2c7a5dbe26e12e) | 86 | 12.20% | +| 2022-01-15 | [38899a3](https://github.com/stamp-web/stamp-web-aurelia/commit/38899a32d69cd5c62ade7341a83708d4a8e1e726) | 94 | 13.29% | ## Optimizing for Browsers diff --git a/src/services/base-service.js b/src/services/base-service.js index dd26eb8..87897d9 100644 --- a/src/services/base-service.js +++ b/src/services/base-service.js @@ -186,6 +186,7 @@ export class BaseService { if( this.loaded && this.models.length > 0 ) { let m = _.find(this.models, { id: model.id }); if(m) { + this._augmentModel(model, m); _.merge(m, model); // Not sure if this is a good idea or not. this.eventBus.publish(EventNames.updateFinished, { type: this.getCollectionName(), model: m}); @@ -290,6 +291,7 @@ export class BaseService { self._postSave(retModel); let m = _.find(self.models, {id: retModel.id}); if (m) { + this._augmentModel(retModel, m); _.merge(m, retModel); } else { m = retModel; @@ -309,4 +311,29 @@ export class BaseService { }); }); } + + /** + * Augment the model by defining any missing keys as null, unless they are objects in which case an empty object + * will be created. This is for pre-merge processing. Service values will return from the DB without being defined. + * If this value previously existed before the update the model that it is being merged two will have a key and the new + * model will not, skipping the clearing of that field. This occurs for dates, but could show up for other values. + * + * @param model + * @param m + */ + _augmentModel(model, m) { + _.forEach(_.keys(m), k => { + let obj = _.isObject(m[k]); + let arr = _.isArrayLikeObject(m[k]); + if (!_.has(model, k)) { + let num = _.isNumber(m[k]); + let v = arr ? [] : obj ? {} : num ? 0 : null; + _.set(model, k, v); + } else if (arr) { + for (let i = 0; i < m[k].length; i++) { + this._augmentModel(model[k][i], m[k][i]); + } + } + }); + } } diff --git a/test/unit/services/base-service.spec.js b/test/unit/services/base-service.spec.js new file mode 100644 index 0000000..565061b --- /dev/null +++ b/test/unit/services/base-service.spec.js @@ -0,0 +1,94 @@ +/** + Copyright 2022 Jason Drake + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +import {createSpyObj} from 'jest-createspyobj'; +import {BaseService} from 'services/base-service'; + +describe('BaseService service test suite', () => { + + let http = createSpyObj('http', ['configure']); + + describe('_augmentModel', () => { + let svc; + beforeEach(() => { + svc = new BaseService(http, {}); + }); + + it('correctly added null or empty properties missing', () => { + let newModel = { + id: 56 + } + let m = { + id: 56, + condition: 2, + purchased: '2022-01-12' + }; + svc._augmentModel(newModel, m); + expect(newModel.purchased).toBeNull(); + expect(newModel.condition).toBe(0); + }); + + it('correctly adds missing arrays', () => { + let newModel = { + id: 56 + } + let m = { + id: 56, + catalogueNumbers: [{ + id: 47 + }] + }; + svc._augmentModel(newModel, m); + expect(newModel.catalogueNumbers.length).toBe(0); + }); + + it('correctly adds missing objects', () => { + let newModel = { + id: 56 + } + let m = { + id: 56, + activeCatalogueNumber: { + id: 43 + } + }; + svc._augmentModel(newModel, m); + expect(newModel.activeCatalogueNumber).toStrictEqual({}); + }); + + it('adds missing values in child objects', () => { + let newModel = { + id: 56, + stampOwnerships: [{ + id: 43 + }] + } + let m = { + id: 56, + stampOwnerships: [{ + id: 43, + pricePaid: '2021-12-31', + condition: 2 + }] + }; + svc._augmentModel(newModel, m); + expect(newModel.stampOwnerships.length).toBe(1); + expect(newModel.stampOwnerships[0].id).toBe(43); + expect(newModel.stampOwnerships[0].condition).toBe(0); + expect(newModel.stampOwnerships[0].pricePaid).toBe(null); + }); + + }); +});