-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
116 additions
and
3 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
34 changes: 34 additions & 0 deletions
34
packages/integration-karma/test/reactivity/setters/index.spec.js
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,34 @@ | ||
import { createElement } from 'test-utils'; | ||
|
||
import Parent from 'x/parent'; | ||
|
||
describe('Reactivity for setters', () => { | ||
|
||
it('should not alter rendering if they are not invoked', () => { | ||
let elm = createElement('x-test', { is: Parent }); | ||
document.body.appendChild(elm); | ||
expect(elm.getRenderingCounter()).toBe(1); | ||
expect(elm.shadowRoot.querySelector('x-child.first').getRenderingCounter()).toBe(1); | ||
}); | ||
|
||
it('should react to changes in parent by calling the setter on the child once', () => { | ||
let elm = createElement('x-test', { is: Parent }); | ||
document.body.appendChild(elm); | ||
elm.addNewItem(4); | ||
return Promise.resolve().then(() => { | ||
expect(elm.getRenderingCounter()).toBe(1); | ||
expect(elm.shadowRoot.querySelector('x-child.second').getRenderingCounter()).toBe(2); | ||
expect(elm.shadowRoot.querySelector('x-child.second').shadowRoot.querySelectorAll('li').length).toBe(4); | ||
}); | ||
}); | ||
|
||
it('should ignore string value passed to setters', () => { | ||
let elm = createElement('x-test', { is: Parent }); | ||
document.body.appendChild(elm); | ||
elm.setCity('sanfrancisco'); | ||
return Promise.resolve().then(() => { | ||
expect(elm.getRenderingCounter()).toBe(2); | ||
expect(elm.shadowRoot.querySelector('x-child.third').getRenderingCounter()).toBe(2); | ||
}); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/integration-karma/test/reactivity/setters/x/child/child.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,10 @@ | ||
<template> | ||
<ul> | ||
<template for:each={normalizedItems} for:item="item"> | ||
<li key={item.key}>{item.label}</li> | ||
</template> | ||
</ul> | ||
<p> | ||
City: {normalizedCity} | ||
</p> | ||
</template> |
38 changes: 38 additions & 0 deletions
38
packages/integration-karma/test/reactivity/setters/x/child/child.js
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 { LightningElement, api, track } from 'lwc'; | ||
|
||
export default class Test extends LightningElement { | ||
counter = 0; | ||
@track normalizedItems = []; | ||
|
||
@api | ||
get items() { | ||
return this.normalizedItems; | ||
} | ||
set items(items) { | ||
this.originalItems = items; | ||
this.normalizedItems = items.map((item)=> { | ||
const normalizedItem = { | ||
key : `id-${item}`, | ||
label : item | ||
}; | ||
|
||
return normalizedItem; | ||
}); | ||
} | ||
|
||
@track normalizedCity = ''; | ||
@api | ||
get city() { | ||
return this.normalizedCity; | ||
} | ||
set city(name) { | ||
this.normalizedCity = name.trim(); | ||
} | ||
|
||
@api getRenderingCounter() { | ||
return this.counter; | ||
} | ||
renderedCallback() { | ||
this.counter++; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/integration-karma/test/reactivity/setters/x/parent/parent.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,5 @@ | ||
<template> | ||
<x-child class="first"></x-child> | ||
<x-child class="second" items={items}></x-child> | ||
<x-child class="third" city={city}></x-child> | ||
</template> |
19 changes: 19 additions & 0 deletions
19
packages/integration-karma/test/reactivity/setters/x/parent/parent.js
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,19 @@ | ||
import { LightningElement, api, track } from 'lwc'; | ||
|
||
export default class Test extends LightningElement { | ||
counter = 0; | ||
@track items = [1,2,3]; | ||
@track city = 'miami'; | ||
@api addNewItem(item) { | ||
this.items.push(item); | ||
} | ||
@api getRenderingCounter() { | ||
return this.counter; | ||
} | ||
@api setCity(v) { | ||
return this.city = v; | ||
} | ||
renderedCallback() { | ||
this.counter++; | ||
} | ||
} |