Skip to content

Commit c17f067

Browse files
committed
Fix the issue where cleared dates are being restored
Dates that were cleared are coming back as a previous date.
1 parent 38899a3 commit c17f067

File tree

3 files changed

+127
-5
lines changed

3 files changed

+127
-5
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ To run the app, follow these steps.
5555

5656
## Browser Support
5757

58-
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.
58+
This project is only going to support the latest browsers (Chrome/Firefox/Edge).
5959

6060

6161
## Connecting to Webservices in Development
@@ -92,10 +92,11 @@ Webdriver for NodeJS is used for the integration tests. This project has been mo
9292

9393
The following is a list of test statistics for the project for date
9494

95-
| Date | Commit | Number of Tests | Code Coverage |
96-
| --- | --- | --- |---------------|
97-
| 2022-01-15 | [8ac447f](https://github.com/stamp-web/stamp-web-aurelia/commit/8ac447f580f29d1f0f8dd23e284c6f25448cf1d7) | 83 | 12.05% |
98-
| 2022-01-15 | [081fe3f](https://github.com/stamp-web/stamp-web-aurelia/commit/081fe3f31d5962c10777f4017e2c7a5dbe26e12e) | 86 | 12.20% |
95+
| Date | Commit | Number of Tests | Code Coverage |
96+
| --- |-----------------------------------------------------------------------------------------------------------|-----------------|---------------|
97+
| 2022-01-15 | [8ac447f](https://github.com/stamp-web/stamp-web-aurelia/commit/8ac447f580f29d1f0f8dd23e284c6f25448cf1d7) | 83 | 12.05% |
98+
| 2022-01-15 | [081fe3f](https://github.com/stamp-web/stamp-web-aurelia/commit/081fe3f31d5962c10777f4017e2c7a5dbe26e12e) | 86 | 12.20% |
99+
| 2022-01-15 | [38899a3](https://github.com/stamp-web/stamp-web-aurelia/commit/38899a32d69cd5c62ade7341a83708d4a8e1e726) | 94 | 13.29% |
99100

100101
## Optimizing for Browsers
101102

src/services/base-service.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ export class BaseService {
186186
if( this.loaded && this.models.length > 0 ) {
187187
let m = _.find(this.models, { id: model.id });
188188
if(m) {
189+
this._augmentModel(model, m);
189190
_.merge(m, model);
190191
// Not sure if this is a good idea or not.
191192
this.eventBus.publish(EventNames.updateFinished, { type: this.getCollectionName(), model: m});
@@ -290,6 +291,7 @@ export class BaseService {
290291
self._postSave(retModel);
291292
let m = _.find(self.models, {id: retModel.id});
292293
if (m) {
294+
this._augmentModel(retModel, m);
293295
_.merge(m, retModel);
294296
} else {
295297
m = retModel;
@@ -309,4 +311,29 @@ export class BaseService {
309311
});
310312
});
311313
}
314+
315+
/**
316+
* Augment the model by defining any missing keys as null, unless they are objects in which case an empty object
317+
* will be created. This is for pre-merge processing. Service values will return from the DB without being defined.
318+
* If this value previously existed before the update the model that it is being merged two will have a key and the new
319+
* model will not, skipping the clearing of that field. This occurs for dates, but could show up for other values.
320+
*
321+
* @param model
322+
* @param m
323+
*/
324+
_augmentModel(model, m) {
325+
_.forEach(_.keys(m), k => {
326+
let obj = _.isObject(m[k]);
327+
let arr = _.isArrayLikeObject(m[k]);
328+
if (!_.has(model, k)) {
329+
let num = _.isNumber(m[k]);
330+
let v = arr ? [] : obj ? {} : num ? 0 : null;
331+
_.set(model, k, v);
332+
} else if (arr) {
333+
for (let i = 0; i < m[k].length; i++) {
334+
this._augmentModel(model[k][i], m[k][i]);
335+
}
336+
}
337+
});
338+
}
312339
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
Copyright 2022 Jason Drake
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
import {createSpyObj} from 'jest-createspyobj';
17+
import {BaseService} from 'services/base-service';
18+
19+
describe('BaseService service test suite', () => {
20+
21+
let http = createSpyObj('http', ['configure']);
22+
23+
describe('_augmentModel', () => {
24+
let svc;
25+
beforeEach(() => {
26+
svc = new BaseService(http, {});
27+
});
28+
29+
it('correctly added null or empty properties missing', () => {
30+
let newModel = {
31+
id: 56
32+
}
33+
let m = {
34+
id: 56,
35+
condition: 2,
36+
purchased: '2022-01-12'
37+
};
38+
svc._augmentModel(newModel, m);
39+
expect(newModel.purchased).toBeNull();
40+
expect(newModel.condition).toBe(0);
41+
});
42+
43+
it('correctly adds missing arrays', () => {
44+
let newModel = {
45+
id: 56
46+
}
47+
let m = {
48+
id: 56,
49+
catalogueNumbers: [{
50+
id: 47
51+
}]
52+
};
53+
svc._augmentModel(newModel, m);
54+
expect(newModel.catalogueNumbers.length).toBe(0);
55+
});
56+
57+
it('correctly adds missing objects', () => {
58+
let newModel = {
59+
id: 56
60+
}
61+
let m = {
62+
id: 56,
63+
activeCatalogueNumber: {
64+
id: 43
65+
}
66+
};
67+
svc._augmentModel(newModel, m);
68+
expect(newModel.activeCatalogueNumber).toStrictEqual({});
69+
});
70+
71+
it('adds missing values in child objects', () => {
72+
let newModel = {
73+
id: 56,
74+
stampOwnerships: [{
75+
id: 43
76+
}]
77+
}
78+
let m = {
79+
id: 56,
80+
stampOwnerships: [{
81+
id: 43,
82+
pricePaid: '2021-12-31',
83+
condition: 2
84+
}]
85+
};
86+
svc._augmentModel(newModel, m);
87+
expect(newModel.stampOwnerships.length).toBe(1);
88+
expect(newModel.stampOwnerships[0].id).toBe(43);
89+
expect(newModel.stampOwnerships[0].condition).toBe(0);
90+
expect(newModel.stampOwnerships[0].pricePaid).toBe(null);
91+
});
92+
93+
});
94+
});

0 commit comments

Comments
 (0)