-
Notifications
You must be signed in to change notification settings - Fork 781
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some e2e tests for computed values for
@Watch()
s
- Loading branch information
1 parent
58d9ada
commit f538849
Showing
6 changed files
with
121 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
2 changes: 1 addition & 1 deletion
2
test/karma/test-app/computed-properties-prop-decorator/karma.spec.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
2 changes: 1 addition & 1 deletion
2
test/karma/test-app/computed-properties-state-decorator/karma.spec.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
48 changes: 48 additions & 0 deletions
48
test/karma/test-app/computed-properties-watch-decorator/cmp.tsx
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,48 @@ | ||
import { Component, h, Prop, Watch } from '@stencil/core'; | ||
|
||
enum Foo { | ||
// names are explicitly different to ensure we aren't | ||
// just resolving the declaration name. | ||
BAR = 'first', | ||
} | ||
|
||
const MyProp = 'last'; | ||
|
||
@Component({ | ||
tag: 'computed-properties-watch-decorator', | ||
}) | ||
export class ComputedPropertiesWatchDecorator { | ||
@Prop() [Foo.BAR] = 'no'; | ||
|
||
@Prop() [MyProp] = 'content'; | ||
|
||
firstNameCalledWith: any; | ||
lastNameCalledWith: any; | ||
|
||
@Watch(Foo.BAR) | ||
onFirstNameChange(newVal: string, oldVal: string, attrName: string) { | ||
this.firstNameCalledWith = { | ||
newVal, | ||
oldVal, | ||
attrName, | ||
}; | ||
} | ||
|
||
@Watch(MyProp) | ||
onLastNameChange(newVal: string, oldVal: string, attrName: string) { | ||
this.lastNameCalledWith = { | ||
newVal, | ||
oldVal, | ||
attrName, | ||
}; | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<p>First name called with: {this.firstNameCalledWith ? JSON.stringify(this.firstNameCalledWith) : 'not yet'}</p> | ||
<p>Last name called with: {this.lastNameCalledWith ? JSON.stringify(this.lastNameCalledWith) : 'not yet'}</p> | ||
</div> | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/karma/test-app/computed-properties-watch-decorator/index.html
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 @@ | ||
<!doctype html> | ||
<meta charset="utf8" /> | ||
<script src="./build/testapp.esm.js" type="module"></script> | ||
<script src="./build/testapp.js" nomodule></script> | ||
|
||
<computed-properties-watch-decorator></computed-properties-watch-decorator> | ||
|
||
<button type="button">Trigger watch callbacks</button> | ||
|
||
<script> | ||
document.querySelector('button').addEventListener('click', () => { | ||
const cmp = document.querySelector('computed-properties-watch-decorator'); | ||
cmp.setAttribute('first', 'Bob'); | ||
cmp.setAttribute('last', 'Builder'); | ||
}); | ||
</script> |
38 changes: 38 additions & 0 deletions
38
test/karma/test-app/computed-properties-watch-decorator/karma.spec.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,38 @@ | ||
import { setupDomTests, waitForChanges } from '../util'; | ||
|
||
describe('computed-properties-watch-decorator', function () { | ||
const { setupDom, tearDownDom } = setupDomTests(document); | ||
let app: HTMLElement; | ||
|
||
beforeEach(async () => { | ||
app = await setupDom('/computed-properties-watch-decorator/index.html'); | ||
}); | ||
afterEach(tearDownDom); | ||
|
||
it('triggers the watch callback when the associated prop changes', async () => { | ||
const el = app.querySelector('computed-properties-watch-decorator'); | ||
expect(el.innerText).toBe('First name called with: not yet\n\nLast name called with: not yet'); | ||
|
||
const button = app.querySelector('button'); | ||
expect(button).toBeDefined(); | ||
|
||
button.click(); | ||
await waitForChanges(); | ||
|
||
const firstNameCalledWith = { | ||
newVal: 'Bob', | ||
oldVal: 'no', | ||
attrName: 'first', | ||
}; | ||
const lastNameCalledWith = { | ||
newVal: 'Builder', | ||
oldVal: 'content', | ||
attrName: 'last', | ||
}; | ||
expect(el.innerText).toBe( | ||
`First name called with: ${JSON.stringify(firstNameCalledWith)}\n\nLast name called with: ${JSON.stringify( | ||
lastNameCalledWith, | ||
)}`, | ||
); | ||
}); | ||
}); |