Skip to content

Commit

Permalink
updated: pure components should't be automatically removed from DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Dec 22, 2020
1 parent 987a5ff commit e232d50
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"coveralls": "^3.1.0",
"eslint": "^7.14.0",
"eslint": "^7.16.0",
"eslint-config-riot": "^3.0.0",
"esm": "^3.2.25",
"jsdom": "^16.4.0",
"jsdom-global": "3.0.2",
"mocha": "^8.2.1",
"nyc": "^15.1.0",
"rollup": "^2.33.3",
"rollup": "^2.35.1",
"rollup-plugin-alias": "^2.2.0",
"rollup-plugin-node-resolve": "^5.2.0",
"sinon": "^9.2.1",
"sinon": "^9.2.2",
"sinon-chai": "^3.5.0",
"typescript": "^4.1.2"
"typescript": "^4.1.3"
},
"dependencies": {
"@riotjs/util": "^2.0.0"
Expand Down
4 changes: 4 additions & 0 deletions src/template.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cleanNode, clearChildren, removeChild } from '@riotjs/util/dom'
import {IS_PURE_SYMBOL} from '@riotjs/util/constants'
import createBinding from './binding'
import createDOMTree from './util/create-DOM-tree'
import injectDOM from './util/inject-DOM'
Expand Down Expand Up @@ -121,6 +122,9 @@ export const TemplateChunk = Object.freeze({
this.bindings.forEach(b => b.unmount(scope, parentScope, mustRemoveRoot))

switch (true) {
// pure components should handle the DOM unmount updates by themselves
case this.el[IS_PURE_SYMBOL]:
break
// <template> tags should be treated a bit differently
// we need to clear their children only if it's explicitly required by the caller
// via mustRemoveRoot !== null
Expand Down
19 changes: 18 additions & 1 deletion test/core.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { bindingTypes, expressionTypes, template } from '../src'
import {IS_PURE_SYMBOL} from '@riotjs/util/constants'

describe('core specs', () => {
it('The riot DOM bindings public methods get properly exported', () => {
Expand Down Expand Up @@ -47,6 +48,22 @@ describe('core specs', () => {
expect(() => tag.unmount({}, {}, true)).to.not.throw()
})

it('The pure components should handle the unmount DOM updates by themselves', () => {
const el = template('hello')
const target = document.createElement('div')

target[IS_PURE_SYMBOL] = true

document.body.appendChild(target)

el.mount(target)
el.unmount({}, {}, true)

expect(target.parentNode).to.be.ok
target.parentNode.removeChild(target)
expect(target.parentNode).to.be.not.ok
})

it('The template.unmount can be able to remove the root node (node appended to the dom)', () => {
const el = template('hello')
const target = document.createElement('div')
Expand Down Expand Up @@ -302,4 +319,4 @@ describe('core specs', () => {

el.unmount()
})
})
})

0 comments on commit e232d50

Please sign in to comment.