This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add beforeProperties lifecycle hook (#663)
* add beforeProperties lifecycle hook * doc
- Loading branch information
Showing
6 changed files
with
108 additions
and
2 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,16 @@ | ||
import { handleDecorator } from './../WidgetBase'; | ||
import { BeforeProperties } from './../interfaces'; | ||
|
||
/** | ||
* Decorator that adds the function passed of target method to be run | ||
* in the `beforeProperties` lifecycle. | ||
*/ | ||
export function beforeProperties(method: BeforeProperties): (target: any) => void; | ||
export function beforeProperties(): (target: any, propertyKey: string) => void; | ||
export function beforeProperties(method?: BeforeProperties) { | ||
return handleDecorator((target, propertyKey) => { | ||
target.addDecorator('beforeProperties', propertyKey ? target[propertyKey] : method); | ||
}); | ||
} | ||
|
||
export default beforeProperties; |
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 @@ | ||
import './beforeProperties'; |
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,69 @@ | ||
import * as registerSuite from 'intern!object'; | ||
import * as assert from 'intern/chai!assert'; | ||
|
||
import { beforeProperties } from './../../../src/decorators/beforeProperties'; | ||
import { WidgetBase } from './../../../src/WidgetBase'; | ||
import { WidgetProperties } from './../../../src/interfaces'; | ||
|
||
registerSuite({ | ||
name: 'decorators/beforeProperties', | ||
beforeProperties() { | ||
function before(properties: WidgetProperties): WidgetProperties { | ||
return { key: 'foo' }; | ||
} | ||
|
||
@beforeProperties(before) | ||
class TestWidget extends WidgetBase<any> {} | ||
const widget = new TestWidget(); | ||
widget.__setProperties__({}); | ||
assert.strictEqual(widget.properties.key, 'foo'); | ||
}, | ||
'multiple beforeProperties decorators'() { | ||
function beforeOne(properties: WidgetProperties): WidgetProperties { | ||
return { key: 'foo' }; | ||
} | ||
function beforeTwo(properties: any): any { | ||
return { other: 'bar' }; | ||
} | ||
|
||
@beforeProperties(beforeOne) | ||
@beforeProperties(beforeTwo) | ||
class TestWidget extends WidgetBase<any> {} | ||
const widget = new TestWidget(); | ||
widget.__setProperties__({}); | ||
assert.strictEqual(widget.properties.key, 'foo'); | ||
assert.strictEqual(widget.properties.other, 'bar'); | ||
}, | ||
'beforeProperties on class method'() { | ||
class TestWidget extends WidgetBase<any> { | ||
@beforeProperties() | ||
before(properties: WidgetProperties): WidgetProperties { | ||
return { key: 'foo' }; | ||
} | ||
} | ||
const widget = new TestWidget(); | ||
widget.__setProperties__({}); | ||
assert.strictEqual(widget.properties.key, 'foo'); | ||
|
||
}, | ||
'programmatic beforeProperties'() { | ||
function beforeOne(properties: WidgetProperties): WidgetProperties { | ||
return { key: 'foo' }; | ||
} | ||
function beforeTwo(properties: any): any { | ||
return { other: 'bar' }; | ||
} | ||
|
||
class TestWidget extends WidgetBase<any> { | ||
constructor() { | ||
super(); | ||
beforeProperties(beforeOne)(this); | ||
beforeProperties(beforeTwo)(this); | ||
} | ||
} | ||
const widget = new TestWidget(); | ||
widget.__setProperties__({}); | ||
assert.strictEqual(widget.properties.key, 'foo'); | ||
assert.strictEqual(widget.properties.other, 'bar'); | ||
} | ||
}); |