Skip to content

Commit 4be89eb

Browse files
authored
Merge pull request #6 from costrojs/fix/replace-children
Add fallback for replaceChildren support
2 parents ee4651b + ad3a54e commit 4be89eb

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 1.0.6
4+
5+
### Fixes
6+
7+
- Add fallback for replaceChildren support ([#6](https://github.com/costrojs/costro/pull/6))
8+
39
## 1.0.5
410

511
### Fixes

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "costro",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Build web applications with Components, Store and Router in 3KB",
55
"keywords": [
66
"costro",

src/app.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,12 @@ export default class App {
237237
destroyComponent() {
238238
if (this.previousRoute) {
239239
this.previousRoute.isComponentClass && this.previousRoute.component.beforeDestroy()
240-
this.target.replaceChildren()
240+
241+
if (typeof Element.prototype.replaceChildren === 'function') {
242+
this.target.replaceChildren()
243+
} else {
244+
this.target.innerHTML = ''
245+
}
241246
this.previousRoute.isComponentClass && this.previousRoute.component.afterDestroy()
242247
}
243248
}

tests/unit/app.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,24 @@ describe('App', () => {
657657
expect(app.target.replaceChildren).toHaveBeenCalled()
658658
expect(app.previousRoute.component.afterDestroy).toHaveBeenCalled()
659659
})
660+
661+
it('should call the destroyComponent function without replaceChildren support', () => {
662+
app.previousRoute = {
663+
component: {
664+
afterDestroy: jest.fn(),
665+
beforeDestroy: jest.fn()
666+
},
667+
isComponentClass: true
668+
}
669+
670+
Element.prototype.replaceChildren = undefined
671+
672+
app.destroyComponent()
673+
674+
expect(app.previousRoute.component.beforeDestroy).toHaveBeenCalled()
675+
expect(app.target.innerHTML).toStrictEqual('')
676+
expect(app.previousRoute.component.afterDestroy).toHaveBeenCalled()
677+
})
660678
})
661679

662680
describe('createComponent', () => {

0 commit comments

Comments
 (0)