1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16exports.source = `
17import { ClassB as ClassA } from './test/pages/import@Observed'
18@Component
19struct ViewA {
20  label: string = 'ViewA1'
21  @ObjectLink a: ClassA
22
23  build() {
24    Row() {
25      Button('ViewA'+ JSON.stringify(this.label) + 'this.a.c='+JSON.stringify(this.a.c))
26        .onClick(() => {
27          this.a.c += 1
28        })
29    }.margin({ top: 10 })
30  }
31}
32
33@Entry
34@Component
35struct ViewB {
36  @State arrA: ClassA[] = [new ClassA(0), new ClassA(0)]
37
38  build() {
39    Column() {
40      ForEach(this.arrA, (item) => {
41        ViewA({ label: JSON.stringify(item.id), a: item })
42      }, (item) => item.id.toString())
43      ViewA({ label: JSON.stringify(this.arrA[0]), a: this.arrA[0] })
44      ViewA({ label: JSON.stringify(this.arrA[this.arrA.length - 1]), a: this.arrA[this.arrA.length - 1] })
45
46      Button('ViewB: reset array')
47        .margin({ top: 10 })
48        .onClick(() => {
49          this.arrA = [new ClassA(0), new ClassA(0)]
50        })
51      Button('ViewB: push')
52        .margin({ top: 10 })
53        .onClick(() => {
54          this.arrA.push(new ClassA(0))
55        })
56      Button('ViewB: shift')
57        .margin({ top: 10 })
58        .onClick(() => {
59          this.arrA.shift()
60        })
61    }.width('100%')
62  }
63}
64`
65
66exports.expectResult =
67`"use strict";
68Object.defineProperty(exports, "__esModule", { value: true });
69let __generate__Id = 0;
70function generateId() {
71    return "import@Observed_" + ++__generate__Id;
72}
73const import_Observed_1 = require("./test/pages/import@Observed");
74class ViewA extends View {
75    constructor(compilerAssignedUniqueChildId, parent, params, localStorage) {
76        super(compilerAssignedUniqueChildId, parent, localStorage);
77        this.label = 'ViewA1';
78        this.__a = new SynchedPropertyNesedObject(params.a, this, "a");
79        this.updateWithValueParams(params);
80    }
81    updateWithValueParams(params) {
82        if (params.label !== undefined) {
83            this.label = params.label;
84        }
85        this.__a.set(params.a);
86    }
87    aboutToBeDeleted() {
88        this.__a.aboutToBeDeleted();
89        SubscriberManager.Get().delete(this.id());
90    }
91    get a() {
92        return this.__a.get();
93    }
94    render() {
95        Row.create();
96        Row.margin({ top: 10 });
97        Button.createWithLabel('ViewA' + JSON.stringify(this.label) + 'this.a.c=' + JSON.stringify(this.a.c));
98        Button.onClick(() => {
99            this.a.c += 1;
100        });
101        Button.pop();
102        Row.pop();
103    }
104}
105class ViewB extends View {
106    constructor(compilerAssignedUniqueChildId, parent, params, localStorage) {
107        super(compilerAssignedUniqueChildId, parent, localStorage);
108        this.__arrA = new ObservedPropertyObject([new import_Observed_1.ClassB(0), new import_Observed_1.ClassB(0)], this, "arrA");
109        this.updateWithValueParams(params);
110    }
111    updateWithValueParams(params) {
112        if (params.arrA !== undefined) {
113            this.arrA = params.arrA;
114        }
115    }
116    aboutToBeDeleted() {
117        this.__arrA.aboutToBeDeleted();
118        SubscriberManager.Get().delete(this.id());
119    }
120    get arrA() {
121        return this.__arrA.get();
122    }
123    set arrA(newValue) {
124        this.__arrA.set(newValue);
125    }
126    render() {
127        Column.create();
128        Column.width('100%');
129        ForEach.create("3", this, ObservedObject.GetRawObject(this.arrA), (item) => {
130            let earlierCreatedChild_2 = (this && this.findChildById) ? this.findChildById("2") : undefined;
131            if (earlierCreatedChild_2 == undefined) {
132                View.create(new ViewA("2", this, { label: JSON.stringify(item.id), a: item }));
133            }
134            else {
135                earlierCreatedChild_2.updateWithValueParams({
136                    label: JSON.stringify(item.id), a: item
137                });
138                View.create(earlierCreatedChild_2);
139            }
140        }, (item) => item.id.toString());
141        ForEach.pop();
142        let earlierCreatedChild_4 = (this && this.findChildById) ? this.findChildById("4") : undefined;
143        if (earlierCreatedChild_4 == undefined) {
144            View.create(new ViewA("4", this, { label: JSON.stringify(this.arrA[0]), a: this.arrA[0] }));
145        }
146        else {
147            earlierCreatedChild_4.updateWithValueParams({
148                label: JSON.stringify(this.arrA[0]), a: this.arrA[0]
149            });
150            View.create(earlierCreatedChild_4);
151        }
152        let earlierCreatedChild_5 = (this && this.findChildById) ? this.findChildById("5") : undefined;
153        if (earlierCreatedChild_5 == undefined) {
154            View.create(new ViewA("5", this, { label: JSON.stringify(this.arrA[this.arrA.length - 1]), a: this.arrA[this.arrA.length - 1] }));
155        }
156        else {
157            earlierCreatedChild_5.updateWithValueParams({
158                label: JSON.stringify(this.arrA[this.arrA.length - 1]), a: this.arrA[this.arrA.length - 1]
159            });
160            View.create(earlierCreatedChild_5);
161        }
162        Button.createWithLabel('ViewB: reset array');
163        Button.margin({ top: 10 });
164        Button.onClick(() => {
165            this.arrA = [new import_Observed_1.ClassB(0), new import_Observed_1.ClassB(0)];
166        });
167        Button.pop();
168        Button.createWithLabel('ViewB: push');
169        Button.margin({ top: 10 });
170        Button.onClick(() => {
171            this.arrA.push(new import_Observed_1.ClassB(0));
172        });
173        Button.pop();
174        Button.createWithLabel('ViewB: shift');
175        Button.margin({ top: 10 });
176        Button.onClick(() => {
177            this.arrA.shift();
178        });
179        Button.pop();
180        Column.pop();
181    }
182}
183loadDocument(new ViewB("1", undefined, {}));
184`
185