-
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.
fix(core/renderer): improve event handling and add event listener sup…
…port - Refactor EventEmitter and Event decorators for better event management - Add event support for onCamelEventName or on-kebab-case-name - Add tests for event handling and listener behavior
- Loading branch information
Showing
7 changed files
with
163 additions
and
18 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
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,46 @@ | ||
import { Component, DecoElement, EventEmitter, Event, State, Ref, RefType } from '@decoco/core'; | ||
|
||
@Component('emit-event') | ||
export class EmitEvent extends DecoElement { | ||
@Event() emitter: EventEmitter; | ||
|
||
triggerEvent(eventName: string, successTip: string) { | ||
this.emitter!.emit(eventName, successTip); | ||
} | ||
} | ||
|
||
@Component('emit-event-test') | ||
export class EmitEventTest extends DecoElement { | ||
@Ref() emitRef!: RefType<EmitEvent>; | ||
@Ref() emitRef2!: RefType<EmitEvent>; | ||
@Ref() emitRef3!: RefType<EmitEvent>; | ||
@Ref() emitRef4!: RefType<EmitEvent>; | ||
|
||
@State() text = ''; | ||
render() { | ||
const onEvent = (e: CustomEvent) => { | ||
this.text = e.detail; | ||
}; | ||
return ( | ||
<div> | ||
<div className="result">{this.text}</div> | ||
<emit-event ref={this.emitRef} onTestEvent={onEvent}></emit-event> | ||
<emit-event ref={this.emitRef2} onTestEventCapture={onEvent}></emit-event> | ||
<emit-event ref={this.emitRef3} on-test-event={onEvent}></emit-event> | ||
<emit-event ref={this.emitRef4} on-test-event-capture={onEvent}></emit-event> | ||
<button onClick={() => this.emitRef.current!.triggerEvent('testevent', 'onTestEvent ok')}> | ||
onTestEvent | ||
</button> | ||
<button onClick={() => this.emitRef2.current!.triggerEvent('testevent', 'onTestEventCapture ok')}> | ||
onTestEventCapture | ||
</button> | ||
<button onClick={() => this.emitRef3.current!.triggerEvent('test-event', 'test-event ok')}> | ||
on-test-event | ||
</button> | ||
<button onClick={() => this.emitRef4.current!.triggerEvent('test-event', 'test-event-capture ok')}> | ||
on-test-event-capture | ||
</button> | ||
</div> | ||
); | ||
} | ||
} |
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
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,77 @@ | ||
import { test, expect } from '@playwright/test'; | ||
|
||
test('button click triggers event and updates the text', async ({ page }) => { | ||
// 1. 访问页面,假设页面已经包含了 EmitEventTest 组件 | ||
await page.goto('http://localhost:5173/#/renderer/event'); // 替换为你的实际 URL | ||
|
||
// 2. 获取按钮和结果元素 | ||
const button1 = await page.locator('button:has-text("onTestEvent")'); | ||
const button2 = await page.locator('button:has-text("onTestEventCapture")'); | ||
const button3 = await page.locator('button:has-text("on-test-event")'); | ||
const button4 = await page.locator('button:has-text("on-test-event-capture")'); | ||
const result = await page.locator('.result'); | ||
|
||
// 3. 点击 "onTestEvent" 按钮,并验证文本更新 | ||
await button1.click(); | ||
await expect(result).toHaveText('onTestEvent ok'); // 确保文本更新为 "onTestEvent ok" | ||
|
||
// 4. 点击 "onTestEventCapture" 按钮,并验证文本更新 | ||
await button2.click(); | ||
await expect(result).toHaveText('onTestEventCapture ok'); // 确保文本更新为 "onTestEventCapture ok" | ||
|
||
// 5. 点击 "on-test-event" 按钮,并验证文本更新 | ||
await button3.click(); | ||
await expect(result).toHaveText('test-event ok'); // 确保文本更新为 "test-event ok" | ||
|
||
// 6. 点击 "on-test-event-capture" 按钮,并验证文本更新 | ||
await button4.click(); | ||
await expect(result).toHaveText('test-event-capture ok'); // 确保文本更新为 "test-event-capture ok" | ||
}); | ||
|
||
test('event listener should capture event in capture phase', async ({ page }) => { | ||
// 1. 访问页面 | ||
await page.goto('http://localhost:3000'); // 替换为你的实际 URL | ||
|
||
// 2. 获取按钮和结果元素 | ||
const button2 = await page.locator('button:has-text("onTestEventCapture")'); | ||
const result = await page.locator('.result'); | ||
|
||
// 3. 点击 "onTestEventCapture" 按钮,并验证文本更新 | ||
await button2.click(); | ||
await expect(result).toHaveText('onTestEventCapture ok'); // 验证在捕获阶段触发的事件更新文本 | ||
}); | ||
|
||
test('event listener should trigger the correct event', async ({ page }) => { | ||
// 1. 访问页面 | ||
await page.goto('http://localhost:3000'); // 替换为你的实际 URL | ||
|
||
// 2. 获取按钮和结果元素 | ||
const button1 = await page.locator('button:has-text("onTestEvent")'); | ||
const result = await page.locator('.result'); | ||
|
||
// 3. 点击按钮,触发事件,并验证文本更新 | ||
await button1.click(); | ||
await expect(result).toHaveText('onTestEvent ok'); // 验证文本更新为 "onTestEvent ok" | ||
}); | ||
|
||
test('multiple events can be triggered and update text', async ({ page }) => { | ||
// 1. 访问页面 | ||
await page.goto('http://localhost:3000'); // 替换为你的实际 URL | ||
|
||
// 2. 获取按钮和结果元素 | ||
const button1 = await page.locator('button:has-text("onTestEvent")'); | ||
const button2 = await page.locator('button:has-text("onTestEventCapture")'); | ||
const button3 = await page.locator('button:has-text("on-test-event")'); | ||
const button4 = await page.locator('button:has-text("on-test-event-capture")'); | ||
const result = await page.locator('.result'); | ||
|
||
// 3. 顺序点击按钮,并验证文本更新 | ||
await button1.click(); | ||
await expect(result).toHaveText('onTestEvent ok'); // 第一个事件的文本 | ||
await button2.click(); | ||
await expect(result).toHaveText('onTestEventCapture ok'); // 第二个事件的文本 | ||
await button3.click(); | ||
await expect(result).toHaveText('test-event ok'); // 第三个事件的文本 | ||
await button4.click(); | ||
await expect(result).toHaveText('test-event-capture ok'); // 第四个事件的文本 | ||
}); |