Skip to content

Commit

Permalink
Add test: child components have access to the parent scope throughout…
Browse files Browse the repository at this point in the history
… their entire lifecycle

Took 54 minutes
  • Loading branch information
kachurun authored and GianlucaGuarini committed May 15, 2021
1 parent 65e61de commit be5851a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/components/parent-context-child.riot
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<parent-context-child>
<script>
import {PARENT_KEY_SYMBOL} from '@riotjs/util/constants'
export default {
callParent(from) {
this[PARENT_KEY_SYMBOL].test.add(from);
},
onBeforeMount() {
this.callParent('onBeforeMount')
},
onMounted() {
this.callParent('onMounted')
},
onBeforeUpdate() {
this.callParent('onBeforeUpdate')
},
onUpdated() {
this.callParent('onUpdated')
},
onBeforeUnmount() {
this.callParent('onBeforeUnmount')
},
onUnmounted() {
this.callParent('onUnmounted')
}
}
</script>
</parent-context-child>
15 changes: 15 additions & 0 deletions test/components/parent-context.riot
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<parent-context>
<parent-context-child></parent-context-child>

<script>
import parentContextChild from './parent-context-child.riot'
export default {
components: {
parentContextChild,
},
test: new Set()
}
</script>
</parent-context>
11 changes: 11 additions & 0 deletions test/specs/lifecycle-events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import InvalidPureCssComponent from '../components/invalid-pure-css-component.ri
import InvalidPureHtmlComponent from '../components/invalid-pure-html-component.riot'
import NativeAttributes from '../components/native-attributes.riot'
import NativeInlineEvents from '../components/native-inline-events.riot'
import ParentContext from '../components/parent-context.riot'
import ParentValueProp from '../components/parent-value-prop.riot'
import PureComponent from '../components/pure-component.riot'
import PureObject from '../components/pure-object.riot'
Expand Down Expand Up @@ -243,4 +244,14 @@ describe('lifecycle events', () => {

component.unmount()
})

it('child components have access to the parent scope throughout their entire lifecycle', () => {
const element = document.createElement('parent-context')
const component = riot.component(ParentContext)(element)

component.update()
component.unmount()

expect([...component.test]).to.eql(['onBeforeMount', 'onMounted', 'onBeforeUpdate', 'onUpdated', 'onBeforeUnmount', 'onUnmounted'])
})
})

0 comments on commit be5851a

Please sign in to comment.