Skip to content

Commit

Permalink
closes #2984
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Aug 26, 2023
1 parent a2dc911 commit da1dd9d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/core/mocked-template-interface.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { PURE_COMPONENT_API } from './pure-component-api.js'
import { noop } from '@riotjs/util'
import {
noop,
removeChild,
cleanNode,
UNMOUNT_METHOD_KEY,
MOUNT_METHOD_KEY,
UPDATE_METHOD_KEY,
} from '@riotjs/util'

// Components without template use a mocked template interface with some basic functionalities to
// guarantee consistent rendering behaviour see https://github.com/riot/riot/issues/2984
export const MOCKED_TEMPLATE_INTERFACE = {
...PURE_COMPONENT_API,
[MOUNT_METHOD_KEY](el) {
this.el = el
},
[UPDATE_METHOD_KEY]: noop,
[UNMOUNT_METHOD_KEY](_, __, mustRemoveRoot = false) {
if (mustRemoveRoot) removeChild(this.el)
else if (!mustRemoveRoot) cleanNode(this.el)
},
clone: noop,
createDOM: noop,
}
17 changes: 17 additions & 0 deletions test/components/raw-component.riot
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<raw>
<script>
export default {
onMounted() {
this.updateContent()
},
onUpdated() {
this.updateContent()
},
updateContent() {
if (this.props.html) {
this.root.innerHTML = this.props.html
}
}
}
</script>
</raw>
32 changes: 32 additions & 0 deletions test/specs/lifecycle-events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CommentsAndExpressions from '../components/comments-and-expressions.riot'
import ConditionalSelectOption from '../components/conditional-select-option.riot'
import EachCustomChildrenComponents from '../components/each-custom-children-components.riot'
import ExpressionParts from '../components/expression-parts.riot'
import RawComponent from '../components/raw-component.riot'
import InvalidPureCssComponent from '../components/invalid-pure-css-component.riot'
import InvalidPureHtmlComponent from '../components/invalid-pure-html-component.riot'
import NativeAttributes from '../components/native-attributes.riot'
Expand Down Expand Up @@ -49,6 +50,19 @@ describe('lifecycle events', () => {
expect(element.parentNode).to.be.not.ok
})

it('unmounting raw components should not preserve the root tag', () => {
const component = riot.component(RawComponent)
const element = document.createElement('div')
document.body.appendChild(element)

const tag = component(element)

expect(element.parentNode).to.be.ok

tag.unmount()
expect(element.parentNode).to.be.not.ok
})

it('unmounting components can preserve the root tag', () => {
const component = riot.component(SimpleComponent)
const element = document.createElement('div')
Expand All @@ -63,6 +77,24 @@ describe('lifecycle events', () => {
document.body.removeChild(element)
})

it('unmounting raw components can preserve the root tag', () => {
const component = riot.component(RawComponent)
const element = document.createElement('div')
document.body.appendChild(element)

const tag = component(element, {
html: '<p>hello</p>',
})

expect(element.parentNode).to.be.ok
expect(element.querySelector('p')).to.be.ok
tag.unmount(true)
expect(element.parentNode).to.be.ok
expect(element.querySelector('p')).to.be.not.ok

document.body.removeChild(element)
})

it('the shouldUpdate method can block all the components updates', () => {
const updatedSpy = spy()
const MyComponent = {
Expand Down

0 comments on commit da1dd9d

Please sign in to comment.