Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/api/tsOut_model.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,8 @@ <h1 class="page-title">tsOut/model.js</h1>
propertyDiff(key) {
// TODO: determine if returning an array is really the best option
if (key) {
return [this.onePropertyDiff(key)];
const diff = this.onePropertyDiff(key);
return diff ? [diff] : [];
}
else {
const diffResult = [];
Expand Down
19 changes: 19 additions & 0 deletions test/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,22 @@ test.serial('validation errors', async (t) => {
}
}
});

test.serial(
'return type',
async (t) => {
const testInstance = await nohm.factory<UserMockup>('UserMockup');
const diffBeforeUpdate = testInstance.propertyDiff('name');

// update the property
testInstance.property('name','testName');
const diffAfterUpdate = testInstance.propertyDiff('name');

t.deepEqual(diffBeforeUpdate, [], 'return without undefined');
t.deepEqual(diffAfterUpdate, [{
after: 'testName',
before: 'defaultName',
key: 'name',
}], 'return with difference in property');
},
);
Comment on lines +395 to +412
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I think this test would not have failed previously, right? The only real change is the return type of an item inside the array. So you'd have to make a test that loops over the diff and explicitly tries to access an item in it.

Something like

diffBeforeUpdate.forEach((diffItem) => { 
  // should never be called, but also shouldn't cause a TS error
  console.log('diff had item', diffItem);
  t.fail('should never reach this');
});

Copy link
Author

@rak3n rak3n Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting, the resultant from the earlier version was returning [undefined] (as the JS func which doesn't return anything is undefined for return), and hence this test would have failed for deepEquals when compared with [] on the previous version. Happy to make change if that's not the case here

5 changes: 3 additions & 2 deletions ts/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,11 @@ abstract class NohmModel<TProps extends IDictionary = IDictionary> {
*/
public propertyDiff(
key?: keyof TProps,
): Array<void | IPropertyDiff<keyof TProps>> {
): Array<IPropertyDiff<keyof TProps>> {
// TODO: determine if returning an array is really the best option
if (key) {
return [this.onePropertyDiff(key)];
const diff = this.onePropertyDiff(key);
return diff ? [diff] : [];
} else {
const diffResult: Array<IPropertyDiff<keyof TProps>> = [];
for (const [iterationKey] of this.properties) {
Expand Down