From b66e04e2626c3c53a55b36b3902fb4b07a109937 Mon Sep 17 00:00:00 2001 From: Rishabh Kumar Date: Mon, 24 Oct 2022 12:57:02 +0530 Subject: [PATCH 1/2] Remove void return in array from onePropertyDiff --- docs/api/tsOut_model.js.html | 3 ++- ts/model.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/api/tsOut_model.js.html b/docs/api/tsOut_model.js.html index 55cc04f..32975cf 100644 --- a/docs/api/tsOut_model.js.html +++ b/docs/api/tsOut_model.js.html @@ -419,7 +419,8 @@

tsOut/model.js

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 = []; diff --git a/ts/model.ts b/ts/model.ts index a0194c8..e7aea8e 100644 --- a/ts/model.ts +++ b/ts/model.ts @@ -541,7 +541,8 @@ abstract class NohmModel { ): Array> { // 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> = []; for (const [iterationKey] of this.properties) { From 117c1c259df6397c30a6e45ed474ba9360726e05 Mon Sep 17 00:00:00 2001 From: Rishabh Kumar Date: Wed, 26 Oct 2022 18:58:16 +0530 Subject: [PATCH 2/2] Add tests and update return type --- test/typescript.test.ts | 19 +++++++++++++++++++ ts/model.ts | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/test/typescript.test.ts b/test/typescript.test.ts index 912f356..82eab51 100644 --- a/test/typescript.test.ts +++ b/test/typescript.test.ts @@ -391,3 +391,22 @@ test.serial('validation errors', async (t) => { } } }); + +test.serial( + 'return type', + async (t) => { + const testInstance = await nohm.factory('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'); + }, +); \ No newline at end of file diff --git a/ts/model.ts b/ts/model.ts index e7aea8e..3ab9c73 100644 --- a/ts/model.ts +++ b/ts/model.ts @@ -538,7 +538,7 @@ abstract class NohmModel { */ public propertyDiff( key?: keyof TProps, - ): Array> { + ): Array> { // TODO: determine if returning an array is really the best option if (key) { const diff = this.onePropertyDiff(key);