-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
259 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { SanComponent } from 'san' | ||
|
||
export function handleError (e: Error, instance: SanComponent<{}>, info: string) { | ||
let current: SanComponent<{}> | undefined = instance | ||
while (current) { | ||
if (typeof current.error === 'function') { | ||
current.error(e, instance, info) | ||
return | ||
} | ||
current = current.parentComponent | ||
} | ||
|
||
throw e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { _ } from './underscore' | ||
export { createResolver } from './resolver' | ||
export { SanSSRData } from './san-ssr-data' | ||
export { handleError } from './handle-error' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { SanProject } from '../dist/index' | ||
import san from 'san' | ||
|
||
it('lifecycle hook: inited', function () { | ||
const spy = jest.fn() | ||
const Child = san.defineComponent({ | ||
template: '<h1>test</h1>', | ||
inited: function () { | ||
throw new Error('error') | ||
} | ||
}) | ||
const MyComponent = san.defineComponent({ | ||
template: '<div><x-child /></div>', | ||
components: { | ||
'x-child': Child | ||
}, | ||
error: spy | ||
}) | ||
|
||
const project = new SanProject() | ||
const renderer = project.compileToRenderer(MyComponent) | ||
|
||
renderer({}) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
|
||
const args = spy.mock.calls[0] | ||
expect(args[2]).toBe('hook:inited') | ||
expect(args[1] instanceof Child).toBe(true) | ||
expect(args[0] instanceof Error).toBe(true) | ||
expect(args[0].message).toBe('error') | ||
}) | ||
|
||
it('initData', function () { | ||
const spy = jest.fn() | ||
const Child = san.defineComponent({ | ||
template: '<h1>test</h1>', | ||
initData: function () { | ||
throw new Error('error') | ||
} | ||
}) | ||
const MyComponent = san.defineComponent({ | ||
template: '<div><x-child /></div>', | ||
components: { | ||
'x-child': Child | ||
}, | ||
error: spy | ||
}) | ||
|
||
const project = new SanProject() | ||
const renderer = project.compileToRenderer(MyComponent) | ||
|
||
renderer({}) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
|
||
const args = spy.mock.calls[0] | ||
expect(args[2]).toBe('initData') | ||
expect(args[1] instanceof Child).toBe(true) | ||
expect(args[0] instanceof Error).toBe(true) | ||
expect(args[0].message).toBe('error') | ||
}) | ||
|
||
it('computed', function () { | ||
const spy = jest.fn() | ||
const Child = san.defineComponent({ | ||
template: '<h1>{{ message }}</h1>', | ||
computed: { | ||
message: function () { | ||
throw new Error('error') | ||
} | ||
} | ||
}) | ||
const MyComponent = san.defineComponent({ | ||
template: '<div><x-child /></div>', | ||
components: { | ||
'x-child': Child | ||
}, | ||
error: spy | ||
}) | ||
|
||
const project = new SanProject() | ||
const renderer = project.compileToRenderer(MyComponent) | ||
|
||
renderer({}) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
|
||
const args = spy.mock.calls[0] | ||
expect(args[2]).toBe('computed:message') | ||
expect(args[1] instanceof Child).toBe(true) | ||
expect(args[0] instanceof Error).toBe(true) | ||
expect(args[0].message).toBe('error') | ||
}) | ||
|
||
it('filter', function () { | ||
const spy = jest.fn() | ||
const Child = san.defineComponent({ | ||
template: '<h1>{{ msg | add }}</h1>', | ||
filters: { | ||
add: function () { | ||
throw new Error('error') | ||
} | ||
}, | ||
initData: function () { | ||
return { | ||
msg: 'test' | ||
} | ||
} | ||
}) | ||
const MyComponent = san.defineComponent({ | ||
template: '<div><x-child /></div>', | ||
components: { | ||
'x-child': Child | ||
}, | ||
error: spy | ||
}) | ||
|
||
const project = new SanProject() | ||
const renderer = project.compileToRenderer(MyComponent) | ||
|
||
renderer({}) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
|
||
const args = spy.mock.calls[0] | ||
expect(args[2]).toBe('filter:add') | ||
expect(args[1] instanceof Child).toBe(true) | ||
expect(args[0] instanceof Error).toBe(true) | ||
expect(args[0].message).toBe('error') | ||
}) | ||
|
||
it('slot children', function () { | ||
const spy = jest.fn() | ||
const slotChild = san.defineComponent({ | ||
template: '<span>test</span>', | ||
inited: function () { | ||
throw new Error('error') | ||
} | ||
}) | ||
const Child = san.defineComponent({ | ||
template: '<h1><slot /></h1>' | ||
}) | ||
const MyComponent = san.defineComponent({ | ||
template: '<div><x-child><x-slot-child /></x-child></div>', | ||
components: { | ||
'x-child': Child, | ||
'x-slot-child': slotChild | ||
}, | ||
error: spy | ||
}) | ||
|
||
const project = new SanProject() | ||
const renderer = project.compileToRenderer(MyComponent) | ||
|
||
renderer({}) | ||
|
||
expect(spy).toHaveBeenCalled() | ||
|
||
const args = spy.mock.calls[0] | ||
expect(args[2]).toBe('hook:inited') | ||
expect(args[1] instanceof slotChild).toBe(true) | ||
expect(args[0] instanceof Error).toBe(true) | ||
expect(args[0].message).toBe('error') | ||
}) |
Oops, something went wrong.