Skip to content

Commit

Permalink
Merge pull request #6 from costrojs/fix/replace-children
Browse files Browse the repository at this point in the history
Add fallback for replaceChildren support
  • Loading branch information
yoriiis authored Jan 19, 2023
2 parents ee4651b + ad3a54e commit 4be89eb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## 1.0.6

### Fixes

- Add fallback for replaceChildren support ([#6](https://github.com/costrojs/costro/pull/6))

## 1.0.5

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "costro",
"version": "1.0.5",
"version": "1.0.6",
"description": "Build web applications with Components, Store and Router in 3KB",
"keywords": [
"costro",
Expand Down
7 changes: 6 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,12 @@ export default class App {
destroyComponent() {
if (this.previousRoute) {
this.previousRoute.isComponentClass && this.previousRoute.component.beforeDestroy()
this.target.replaceChildren()

if (typeof Element.prototype.replaceChildren === 'function') {
this.target.replaceChildren()
} else {
this.target.innerHTML = ''
}
this.previousRoute.isComponentClass && this.previousRoute.component.afterDestroy()
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,24 @@ describe('App', () => {
expect(app.target.replaceChildren).toHaveBeenCalled()
expect(app.previousRoute.component.afterDestroy).toHaveBeenCalled()
})

it('should call the destroyComponent function without replaceChildren support', () => {
app.previousRoute = {
component: {
afterDestroy: jest.fn(),
beforeDestroy: jest.fn()
},
isComponentClass: true
}

Element.prototype.replaceChildren = undefined

app.destroyComponent()

expect(app.previousRoute.component.beforeDestroy).toHaveBeenCalled()
expect(app.target.innerHTML).toStrictEqual('')
expect(app.previousRoute.component.afterDestroy).toHaveBeenCalled()
})
})

describe('createComponent', () => {
Expand Down

0 comments on commit 4be89eb

Please sign in to comment.