diff --git a/ets-tests/ets/pages/states/ObservableDateArray.ets b/ets-tests/ets/pages/states/ObservableDateArray.ets index aac877444d5ca254f400f7f4cec981cb7f3b562e..a70dbbd81710f9c566cb328e4ad3d7720302557f 100644 --- a/ets-tests/ets/pages/states/ObservableDateArray.ets +++ b/ets-tests/ets/pages/states/ObservableDateArray.ets @@ -17,16 +17,22 @@ @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 - 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({ 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(() => { 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 a759b7eea3f3f00aab136f5d55303b848242239d..fa2506342b587f4d434b872e22b8ac89d06d2dec 100644 --- a/ets-tests/ets/suites/StateManagement.ets +++ b/ets-tests/ets/suites/StateManagement.ets @@ -103,16 +103,19 @@ 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", () => { + // 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" - + "item 3 = 2023-6-1\n") + + "item 3 = 2021-6-1\n") }) test("Array: fill array with new value", () => { testPageOnChange(control, "ObservableDateArray", 33, @@ -123,18 +126,38 @@ 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("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", [65, 66], "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") + + "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" + + "item 2 = 2021-4-1\n") }) }) suite("State management of the Map data type", () => {