Skip to content

Commit

Permalink
Optimize catch
Browse files Browse the repository at this point in the history
  • Loading branch information
yoriiis committed Sep 24, 2024
1 parent 02b578f commit 0f7938d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ export default class App {
this.initComponentInCache()
}

try {
this.getComponentView().then((componentView) => {
this.getComponentView()
.then((componentView) => {
if (componentView && this.currentRoute) {
if (!this.currentRoute.interfaceType) {
this.currentRoute.interfaceType =
Expand All @@ -273,9 +273,9 @@ export default class App {
this.currentRoute.component.afterRender()
}
})
} catch (error) {
console.warn('getComponentView::promise rejected')
}
.catch((error) => {
console.warn('getComponentView::promise rejected', error)
})
}
}

Expand Down
42 changes: 33 additions & 9 deletions tests/unit/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,33 @@ describe('App', () => {
expect(app.currentRoute.component.afterRender).toHaveBeenCalled()
})

it('should call the createComponent function with a promise returning undefined (route has changed)', async () => {
app.currentRoute = {
component: {
afterRender: jest.fn()
},
interfaceType: null,
isComponentClass: true,
isComponentClassReady: false
}

app.initComponentInCache = jest.fn()
app.getComponentView = jest.fn().mockResolvedValue(undefined)
app.getInterfaceTypeFromView = jest.fn()
app.transformLinksInStringComponent = jest.fn()
app.target.appendChild = jest.fn()
console.warn = jest.fn()

await app.createComponent()

expect(app.initComponentInCache).toHaveBeenCalled()
expect(app.getComponentView).toHaveBeenCalled()
expect(app.getInterfaceTypeFromView).not.toHaveBeenCalled()
expect(app.transformLinksInStringComponent).not.toHaveBeenCalled()
expect(app.target.appendChild).not.toHaveBeenCalled()
expect(app.currentRoute.component.afterRender).not.toHaveBeenCalled()
})

it('should call the createComponent function with a promise rejected', async () => {
app.currentRoute = {
component: {
Expand All @@ -756,10 +783,12 @@ describe('App', () => {
}

app.initComponentInCache = jest.fn()
app.getComponentView = jest.fn()
app.getComponentView = jest.fn().mockRejectedValue(new Error('Promise rejection error'))

app.getInterfaceTypeFromView = jest.fn()
app.transformLinksInStringComponent = jest.fn()
app.target.appendChild = jest.fn()
app.test = jest.fn()
console.warn = jest.fn()

await app.createComponent()
Expand All @@ -770,7 +799,6 @@ describe('App', () => {
expect(app.transformLinksInStringComponent).not.toHaveBeenCalled()
expect(app.target.appendChild).not.toHaveBeenCalled()
expect(app.currentRoute.component.afterRender).not.toHaveBeenCalled()
expect(console.warn).toHaveBeenCalledWith('getComponentView::promise rejected')
})
})

Expand Down Expand Up @@ -857,15 +885,11 @@ describe('App', () => {
expect(result).toStrictEqual(<div>Component</div>)
})

it('should call the getComponentView function with a component class with before render as a promise', async () => {
it('should call the getComponentView function with a component class with before render a promise', async () => {
app.currentRoute = {
component: {
afterRender: jest.fn(),
beforeRender: jest
.fn()
.mockImplementation(
() => new Promise((resolve) => setTimeout(resolve, 2000))
),
beforeRender: jest.fn().mockResolvedValue(),
render: jest.fn()
},
isComponentClass: true
Expand All @@ -881,7 +905,7 @@ describe('App', () => {
expect(result).toStrictEqual(<div>Component</div>)
})

it('should call the getComponentView function with a component class with before render as a promise and route has changed', async () => {
it('should call the getComponentView function with a component class with before render a promise and route has changed', async () => {
const beforeRenderMock = jest.fn().mockResolvedValue()

app.currentRoute = {
Expand Down

0 comments on commit 0f7938d

Please sign in to comment.