From 677c88fdc922c93e0d5cf47d002587061eeb0a84 Mon Sep 17 00:00:00 2001 From: kirillberezin Date: Mon, 2 Jun 2025 15:10:57 +0300 Subject: [PATCH 1/4] add tests for array Signed-off-by: kirillberezin --- ets-tests/ets/suites/StateManagement.ets | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/ets-tests/ets/suites/StateManagement.ets b/ets-tests/ets/suites/StateManagement.ets index a759b7eea3..82ffb265a9 100644 --- a/ets-tests/ets/suites/StateManagement.ets +++ b/ets-tests/ets/suites/StateManagement.ets @@ -103,16 +103,15 @@ function stateManagementTests(control: AppControl) { + "single item = 2022-5-1\n" + "item 2 = 2022-5-1\n") }) - // skip test as it makes more component updates than expected - // expected: update of only pushed component - // actual: sibling component to ForEach statement is updated as well - // TODO: After calling "push" data[1] element changes - needs to be fixed. - test.expectFailure("Description of the problem", "Array: push new value", () => { + // type of decorated value is Array, + // so any reference should be processed, including array elements + test("Array: push new value", () => { testPageOnChange(control, "ObservableDateArray", 22, "single item = 2021-4-1\n" + "item 1 = 2020-3-1\n" + "item 2 = 2021-4-1\n" - + "item 3 = 2023-6-1\n") + + "single item = 2021-4-1\n" + + "item 3 = 2021-6-1\n") }) test("Array: fill array with new value", () => { testPageOnChange(control, "ObservableDateArray", 33, @@ -136,6 +135,16 @@ function stateManagementTests(control: AppControl) { + "single item = 2021-9-1\n" + "item 2 = 2021-9-1\n") }) + test("Array: remove observed element from array", () => { + testPageOnChange(control, "ObservableDateArray", [22, 66], + "single item = 2021-4-1\n" + + "item 1 = 2020-3-1\n" + + "item 2 = 2021-4-1\n" + + "single item = 2021-4-1\n" + + "item 3 = 2021-6-1\n" + + "single item = 2021-6-1\n" + + "item 2 = 2021-6-1\n") + }) }) suite("State management of the Map data type", () => { // skip test as it makes more component updates than expected -- Gitee From 813d820550a63dc5d2d9a4d471744019cbe70e91 Mon Sep 17 00:00:00 2001 From: kirillberezin Date: Mon, 2 Jun 2025 15:26:03 +0300 Subject: [PATCH 2/4] add lost configuration Signed-off-by: kirillberezin --- ets-tests/ets/pages/states/ObservableDateArray.ets | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ets-tests/ets/pages/states/ObservableDateArray.ets b/ets-tests/ets/pages/states/ObservableDateArray.ets index aac877444d..df2091d9f1 100644 --- a/ets-tests/ets/pages/states/ObservableDateArray.ets +++ b/ets-tests/ets/pages/states/ObservableDateArray.ets @@ -24,9 +24,11 @@ struct ObservableDateArray { TestComponent({}) .log(`item ${index! + 1} = ${item.getFullYear()}-${item.getMonth() + 1}-${item.getDate()}`) }) + TestComponent({}).log(`single item2 = ${this.data[1].getFullYear()}-${this.data[1].getMonth() + 1}-${this.data[1].getDate()}`) TestComponent({ id: 11 }).onChange(() => { this.data[1] = new Date(2022, 4, 1) }) - TestComponent({ id: 22 }).onChange(() => { this.data.push(new Date(2023, 5, 1)) }) + TestComponent({ id: 22 }).onChange(() => { console.log('insert new date value') ; this.data.push(new Date(2021, 5, 1)) }) TestComponent({ id: 33 }).onChange(() => { this.data.fill(new Date(2025, 1, 1)) }) - TestComponent({ id: 44 }).onChange(() => { this.data[1].setMonth(8) }) + TestComponent({ id: 44 }).onChange(() => { console.log('update value') ; this.data[1].setMonth(8) }) + TestComponent({ id: 66 }).onChange(() => { console.log('delete observed value') ; this.data.splice(1,1)}) } } -- Gitee From 6ff3b41c9a79397656f373827e1ed7cd37b45726 Mon Sep 17 00:00:00 2001 From: kirillberezin Date: Tue, 3 Jun 2025 15:29:14 +0300 Subject: [PATCH 3/4] add more tests Signed-off-by: kirillberezin --- .../ets/pages/states/ObservableDateArray.ets | 12 ++++---- ets-tests/ets/suites/StateManagement.ets | 28 ++++++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/ets-tests/ets/pages/states/ObservableDateArray.ets b/ets-tests/ets/pages/states/ObservableDateArray.ets index df2091d9f1..0db0f8364d 100644 --- a/ets-tests/ets/pages/states/ObservableDateArray.ets +++ b/ets-tests/ets/pages/states/ObservableDateArray.ets @@ -19,16 +19,18 @@ struct ObservableDateArray { @State data: Array = Array.of(new Date(2020, 2, 1), new Date(2021, 3, 1)) build() { // check updates after changing array item attributes - TestComponent({}).log(`single item = ${this.data[1].getFullYear()}-${this.data[1].getMonth() + 1}-${this.data[1].getDate()}`) + if (this.data.length > 0) { // guard against the absenece of the observed element + TestComponent({}).log(`single item = ${this.data[1].getFullYear()}-${this.data[1].getMonth() + 1}-${this.data[1].getDate()}`) + } ForEach(this.data, (item: Date, index?: int)=>{ TestComponent({}) .log(`item ${index! + 1} = ${item.getFullYear()}-${item.getMonth() + 1}-${item.getDate()}`) }) - TestComponent({}).log(`single item2 = ${this.data[1].getFullYear()}-${this.data[1].getMonth() + 1}-${this.data[1].getDate()}`) TestComponent({ id: 11 }).onChange(() => { this.data[1] = new Date(2022, 4, 1) }) - TestComponent({ id: 22 }).onChange(() => { console.log('insert new date value') ; this.data.push(new Date(2021, 5, 1)) }) + TestComponent({ id: 22 }).onChange(() => { this.data.push(new Date(2021, 5, 1)) }) TestComponent({ id: 33 }).onChange(() => { this.data.fill(new Date(2025, 1, 1)) }) - TestComponent({ id: 44 }).onChange(() => { console.log('update value') ; this.data[1].setMonth(8) }) - TestComponent({ id: 66 }).onChange(() => { console.log('delete observed value') ; this.data.splice(1,1)}) + TestComponent({ id: 44 }).onChange(() => { this.data[1].setMonth(8) }) + TestComponent({ id: 66 }).onChange(() => { this.data.splice(1,1)}) + TestComponent({ id: 77 }).onChange(() => { this.data.splice(0, this.data.length) }) } } diff --git a/ets-tests/ets/suites/StateManagement.ets b/ets-tests/ets/suites/StateManagement.ets index 82ffb265a9..d61674fd21 100644 --- a/ets-tests/ets/suites/StateManagement.ets +++ b/ets-tests/ets/suites/StateManagement.ets @@ -122,20 +122,22 @@ function stateManagementTests(control: AppControl) { + "item 1 = 2025-2-1\n" + "item 2 = 2025-2-1\n") }) - // skip test as it makes less component updates than expected - // expected: update of all changed components: one inside ForEach statement - // and sibling component logged modified array item - // actual: only sibling component is updated, similar component under ForEach statement is not updated - // TODO: After calling "setMonth" on data[1], ForEach does not detect a change - needs to be fixed. - test.expectFailure("Description of the problem", "Array: change month of the second date inside an array", () => { + // skip test as it makes more component updates than expected + // expected: see no logged updates both for ForEach statement + // and for sibling component + // actual: log of sibling component update is deteted + // TODO: After calling "setMonth" on data[1] no change should be detected + // for details see https://gitcode.com/openharmony/docs/blob/master/en/application-dev/quick-start/arkts-state.md + // section 'Observed changes' : 'When the decorated variable is of the array type, the addition, + // deletion, and updates of array items can be observed ... The value assignment of array items can be observed. ... + // The property value assignment in the array items cannot be observed.' + test.expectFailure("Description of the problem", "Array: change month of the second element of the Date's array and see no chnages for observed items", () => { testPageOnChange(control, "ObservableDateArray", 44, "single item = 2021-4-1\n" + "item 1 = 2020-3-1\n" - + "item 2 = 2021-4-1\n" - + "single item = 2021-9-1\n" - + "item 2 = 2021-9-1\n") + + "item 2 = 2021-4-1\n") }) - test("Array: remove observed element from array", () => { + test("Array: observe the change of the element after push and delete (splice)", () => { testPageOnChange(control, "ObservableDateArray", [22, 66], "single item = 2021-4-1\n" + "item 1 = 2020-3-1\n" @@ -145,6 +147,12 @@ function stateManagementTests(control: AppControl) { + "single item = 2021-6-1\n" + "item 2 = 2021-6-1\n") }) + test("Array: clear array and try observe second element with condition", () => { + testPageOnChange(control, "ObservableDateArray", [77], + "single item = 2021-4-1\n" + + "item 1 = 2020-3-1\n" + + "item 2 = 2021-4-1\n") + }) }) suite("State management of the Map data type", () => { // skip test as it makes more component updates than expected -- Gitee From e07e261e278c2794358410b13193b0ad6f47effc Mon Sep 17 00:00:00 2001 From: kirillberezin Date: Thu, 5 Jun 2025 08:40:58 +0300 Subject: [PATCH 4/4] change test sequence of the item shift test Signed-off-by: kirillberezin --- .../ets/pages/states/ObservableDateArray.ets | 2 ++ ets-tests/ets/suites/StateManagement.ets | 30 +++++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/ets-tests/ets/pages/states/ObservableDateArray.ets b/ets-tests/ets/pages/states/ObservableDateArray.ets index 0db0f8364d..a70dbbd817 100644 --- a/ets-tests/ets/pages/states/ObservableDateArray.ets +++ b/ets-tests/ets/pages/states/ObservableDateArray.ets @@ -17,6 +17,7 @@ @Component struct ObservableDateArray { @State data: Array = Array.of(new Date(2020, 2, 1), new Date(2021, 3, 1)) + temp_array: Array = Array.of(new Date(2022, 2, 4), new Date(2022, 3, 5), new Date(2022, 4,6)) build() { // check updates after changing array item attributes if (this.data.length > 0) { // guard against the absenece of the observed element @@ -30,6 +31,7 @@ struct ObservableDateArray { TestComponent({ id: 22 }).onChange(() => { this.data.push(new Date(2021, 5, 1)) }) TestComponent({ id: 33 }).onChange(() => { this.data.fill(new Date(2025, 1, 1)) }) TestComponent({ id: 44 }).onChange(() => { this.data[1].setMonth(8) }) + TestComponent({ id: 65 }).onChange(() => { this.data = this.temp_array}) TestComponent({ id: 66 }).onChange(() => { this.data.splice(1,1)}) TestComponent({ id: 77 }).onChange(() => { this.data.splice(0, this.data.length) }) } diff --git a/ets-tests/ets/suites/StateManagement.ets b/ets-tests/ets/suites/StateManagement.ets index d61674fd21..fa2506342b 100644 --- a/ets-tests/ets/suites/StateManagement.ets +++ b/ets-tests/ets/suites/StateManagement.ets @@ -103,14 +103,18 @@ function stateManagementTests(control: AppControl) { + "single item = 2022-5-1\n" + "item 2 = 2022-5-1\n") }) - // type of decorated value is Array, - // so any reference should be processed, including array elements - test("Array: push new value", () => { + // We expect that push a new element results in update of ForEach part and we should not see + // any updates of sibling elements (we presume that there is no condictional updates on position of pushed element). + // On the other hand if run a similar test on DevEco we can see updates on all sibling + // elements and a new log item in For Each part. + // Documentation is not clear about which way is correct. + // A possible explanation is : @State decorator presents a contract on watching array as an observable + // item and all references to array will be processed. + test.expectFailure("See less elements than expected", "Array: push new value", () => { testPageOnChange(control, "ObservableDateArray", 22, "single item = 2021-4-1\n" + "item 1 = 2020-3-1\n" + "item 2 = 2021-4-1\n" - + "single item = 2021-4-1\n" + "item 3 = 2021-6-1\n") }) test("Array: fill array with new value", () => { @@ -131,23 +135,25 @@ function stateManagementTests(control: AppControl) { // section 'Observed changes' : 'When the decorated variable is of the array type, the addition, // deletion, and updates of array items can be observed ... The value assignment of array items can be observed. ... // The property value assignment in the array items cannot be observed.' - test.expectFailure("Description of the problem", "Array: change month of the second element of the Date's array and see no chnages for observed items", () => { + test.expectFailure("We see changes of observable items", "Array: change month of the second element of the Date's array", () => { testPageOnChange(control, "ObservableDateArray", 44, "single item = 2021-4-1\n" + "item 1 = 2020-3-1\n" + "item 2 = 2021-4-1\n") }) test("Array: observe the change of the element after push and delete (splice)", () => { - testPageOnChange(control, "ObservableDateArray", [22, 66], + testPageOnChange(control, "ObservableDateArray", [65, 66], "single item = 2021-4-1\n" + "item 1 = 2020-3-1\n" + "item 2 = 2021-4-1\n" - + "single item = 2021-4-1\n" - + "item 3 = 2021-6-1\n" - + "single item = 2021-6-1\n" - + "item 2 = 2021-6-1\n") - }) - test("Array: clear array and try observe second element with condition", () => { + + "single item = 2022-4-5\n" + + "item 1 = 2022-3-4\n" + + "item 2 = 2022-4-5\n" + + "item 3 = 2022-5-6\n" + + "single item = 2022-5-6\n" + + "item 2 = 2022-5-6\n") + }) + test("Array: clear array and try to observe second element with condition", () => { testPageOnChange(control, "ObservableDateArray", [77], "single item = 2021-4-1\n" + "item 1 = 2020-3-1\n" -- Gitee