-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: 开发文档 test: 测试用例
- Loading branch information
Showing
41 changed files
with
579 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
**RootComponent下的computed字段** | ||
|
||
```ts | ||
import { DefineComponent, type DetailedType, RootComponent } from "annil"; | ||
import { observable } from "mobx"; | ||
|
||
type User = { name: string; age: number }; | ||
|
||
const storeUser = observable<User>({ | ||
name: "zhao", | ||
age: 20, | ||
}); | ||
|
||
const rootComponent = RootComponent()({ | ||
properties: { | ||
user: Object as DetailedType<User>, | ||
userOptional: { | ||
type: Object as DetailedType<User>, | ||
value: { name: "zhao", age: 20 }, | ||
}, | ||
}, | ||
data: { | ||
Duser: { name: "zhao", age: 20 }, | ||
}, | ||
store: { | ||
Sage: () => storeUser.age, | ||
}, | ||
|
||
computed: { | ||
// 1. 引用properties字段 | ||
age() { | ||
return this.data.user?.age || 0; | ||
}, | ||
// 2. 引用data字段 | ||
Dname() { | ||
return this.data.Duser.name; | ||
}, | ||
// 3. 引用store字段 | ||
SagePlus() { | ||
return this.data.Sage + 1; | ||
}, | ||
// 5. 引用其他计算属性 | ||
refOtherComputedFields() { | ||
return this.data.age + this.data.SagePlus; | ||
}, | ||
}, | ||
}); | ||
const computed = DefineComponent({ | ||
name: "computed", | ||
rootComponent, | ||
}); | ||
|
||
export type $Computed = typeof computed; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
### store字段 可引入基于mobx的全局状态(响应式数据)。 | ||
|
||
```ts | ||
import { DefineComponent, type DetailedType, RootComponent } from "annil"; | ||
import { observable } from "mobx"; | ||
|
||
type User = { name: string; age: number }; | ||
|
||
const storeUser = observable<User>({ | ||
name: "zhao", | ||
age: 20, | ||
changeName(name: string) { | ||
this.name = name; | ||
}, | ||
changeAge(age: number) { | ||
this.age = age; | ||
}, | ||
}); | ||
type DemoComp = { properties: { demo_age: number } }; | ||
const rootComponent = SubComopnent<{}>()({ | ||
store: { | ||
demo_age: () => storeUser.age, | ||
}, | ||
lifetimes: { | ||
attached() { | ||
console.log(this.data.demo_age); // 20 | ||
storeUser.changeAge(30); | ||
console.log(this.data.demo_age); // 30 | ||
}, | ||
}, | ||
}); | ||
const rootComponent = RootComponent()({ | ||
store: { | ||
name: () => storeUser.name, | ||
}, | ||
lifetimes: { | ||
attached() { | ||
console.log(this.data.name); // 'zhao' | ||
storeUser.changeName("li"); | ||
console.log(this.data.name); // 'li' | ||
}, | ||
}, | ||
}); | ||
|
||
const store = DefineComponent({ | ||
name: "store", | ||
rootComponent, | ||
}); | ||
|
||
export type $Store = typeof store; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
### watch字段 | ||
|
||
可定义所有数据字段(properties,data,store,computed中定义的),数据改变时(深度相等比较)运行定义函数,参数包含旧值,可监控多个字段或子字段。 | ||
|
||
```ts | ||
import { DefineComponent, type DetailedType, RootComponent } from "annil"; | ||
import { observable } from "mobx"; | ||
|
||
type User = { name: string; age: number }; | ||
|
||
const storeUser = observable<User>({ | ||
name: "zhao", | ||
age: 20, | ||
changeName(name: string) { | ||
this.name = name; | ||
}, | ||
}); | ||
|
||
const rootComponent = RootComponent()({ | ||
properties: { | ||
user: Object as DetailedType<User>, // 默认null | ||
}, | ||
data: { | ||
Duser: { name: "zhao", age: 20 }, | ||
}, | ||
store: { | ||
name: () => storeUser.name, | ||
}, | ||
computed: { | ||
age() { | ||
return this.data.user?.age || 0; | ||
}, | ||
}, | ||
watch: { | ||
user(newValue, oldValue) { | ||
console.log(newValue, oldValue); // {name:'li',age:30}, null | ||
}, | ||
"Duser.age"(newValue, oldValue) { | ||
console.log(newValue, oldValue); // 30, 20 | ||
}, | ||
name(newValue, oldValue) { | ||
console.log(newValue, oldValue); // "li", "zhao" | ||
}, | ||
age(newValue, oldValue) { | ||
console.log(newValue, oldValue); // 30, 0 | ||
}, | ||
}, | ||
lifetimes: { | ||
attached() { | ||
this.setData({ | ||
"Duser.age": 30, | ||
// 模拟properteis.user传入新值 | ||
user: { name: "li", age: 30 }, | ||
}); | ||
storeUser.changeName("li"); | ||
}, | ||
}, | ||
}); | ||
|
||
const watch = DefineComponent({ | ||
name: "watch", | ||
rootComponent, | ||
}); | ||
|
||
export type $Watch = typeof computed; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"component": true | ||
} |
17 changes: 17 additions & 0 deletions
17
jest/InternalFieldProtection/InternalFieldProtection.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import simulate from "miniprogram-simulate"; | ||
import path from "path"; | ||
|
||
describe("内部字段保护", () => { | ||
test("methods中包含了disposer字段--报错", () => { | ||
try { | ||
const id = simulate.load(path.resolve(__dirname, "InternalFieldProtection")); | ||
const comp = simulate.render(id); | ||
const parent = document.createElement("parent-wrapper"); | ||
|
||
comp.attach(parent); | ||
} catch (error) { | ||
// @ts-ignore | ||
expect(error.message).toBe("disposer已被内部字段占用"); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { DefineComponent, RootComponent } from "../../src"; | ||
|
||
const rootComponent = RootComponent()({ | ||
methods: { | ||
disposer() { | ||
// ... | ||
}, | ||
}, | ||
}); | ||
const compA = DefineComponent({ | ||
name: "/compA", | ||
rootComponent, | ||
}); | ||
export type $CompA = typeof compA; |
Empty file.
3 changes: 3 additions & 0 deletions
3
jest/deleteVirtualHostWhenIsPage/deleteVirtualHostWhenIsPage.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"component": true | ||
} |
15 changes: 15 additions & 0 deletions
15
jest/deleteVirtualHostWhenIsPage/deleteVirtualHostWhenIsPage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import simulate from "miniprogram-simulate"; | ||
import path from "path"; | ||
export const tempObj = { result: false }; | ||
|
||
describe("页面virtualHost字段会被删除", () => { | ||
const id = simulate.load(path.resolve(__dirname, "deleteVirtualHostWhenIsPage")); | ||
const comp = simulate.render(id); | ||
const parent = document.createElement("parent-wrapper"); | ||
|
||
comp.attach(parent); | ||
|
||
test("页面virtualHost字段会被删除", () => { | ||
expect(tempObj.result).toBeTruthy(); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
jest/deleteVirtualHostWhenIsPage/deleteVirtualHostWhenIsPage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { DefineComponent, RootComponent } from "../../src"; | ||
import { tempObj } from "./deleteVirtualHostWhenIsPage.test"; | ||
|
||
const rootComponent = RootComponent()({ | ||
isPage: true, | ||
|
||
options: { | ||
virtualHost: true, // 模拟注入的virtualHost字段 | ||
}, | ||
lifetimes: { | ||
beforeCreate(options) { | ||
tempObj.result = options.options!.virtualHost ?? true; | ||
}, | ||
created() { | ||
// 模拟页面给 is和route假值。因为isPageCheck是根据is和route来的。 | ||
this.is = this.route = "pages/index/index"; | ||
}, | ||
}, | ||
}); | ||
const index = DefineComponent({ | ||
path: "/pages/index/index", | ||
rootComponent, | ||
// subComponents:[] | ||
}); | ||
export type $Index = typeof index; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"component": true, | ||
"usingComponents": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import simulate from "miniprogram-simulate"; | ||
import path from "path"; | ||
|
||
describe("构建组件时isPage:false是正确的", () => { | ||
const id = simulate.load(path.resolve(__dirname, "IsComponent")); | ||
const comp = simulate.render(id); | ||
const parent = document.createElement("parent-wrapper"); | ||
const onTap = jest.spyOn(console, "error"); | ||
|
||
test("不报错", () => { | ||
comp.attach(parent); | ||
|
||
// 没有报错行为 | ||
expect(onTap).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { DefineComponent, RootComponent } from "../../../src"; | ||
|
||
const rootComponent = RootComponent()({ | ||
isPage: false, | ||
data: { | ||
result: true, | ||
}, | ||
lifetimes: { | ||
created() { | ||
// 模拟组件给 is 一个路径。因为isPageCheck是根据is和route来的。 | ||
this.is = "components/compA/compA"; | ||
}, | ||
}, | ||
}); | ||
const compA = DefineComponent({ | ||
name: "compA", | ||
rootComponent, | ||
// subComponents:[] | ||
}); | ||
export type $CompA = typeof compA; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<view class="events" data-id="events" bind:tap="onTap">{{id}}</view> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"component": true, | ||
"usingComponents": {} | ||
} |
16 changes: 16 additions & 0 deletions
16
jest/isPageCheck/IsComponentError/IsComponentError.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import simulate from "miniprogram-simulate"; | ||
import path from "path"; | ||
|
||
describe("构建组件时isPage:true是错误的", () => { | ||
const id = simulate.load(path.resolve(__dirname, "IsComponentError")); | ||
const comp = simulate.render(id); | ||
const parent = document.createElement("parent-wrapper"); | ||
const onTap = jest.spyOn(console, "error"); | ||
|
||
test("报错", () => { | ||
comp.attach(parent); | ||
|
||
// 报错1次 `组件 components/compA/compA 中 RootComponent构建组件时,可不写isPage字段或值为 false` | ||
expect(onTap).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { DefineComponent, RootComponent } from "../../../src"; | ||
|
||
const rootComponent = RootComponent()({ | ||
isPage: true, // 故意写错 1 | ||
data: { | ||
result: true, | ||
}, | ||
lifetimes: { | ||
created() { | ||
// 模拟组件给 is 一个路径。因为isPageCheck是根据is和route来的。 | ||
this.is = "components/compA/compA"; | ||
}, | ||
}, | ||
}); | ||
const compA = DefineComponent({ | ||
path: "/compA", // 故意写错 2 | ||
rootComponent, | ||
// subComponents:[] | ||
}); | ||
export type $CompA = typeof compA; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<view class="events" data-id="events" bind:tap="onTap">{{id}}</view> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"component": true, | ||
"usingComponents": {} | ||
} |
Oops, something went wrong.