-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
6 changed files
with
267 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import Nano, { Component, defineAsCustomElements } from '../../lib/index.js' | ||
import { initSSR, renderSSR } from '../../lib/ssr.js' | ||
import { wait } from './helpers.js' | ||
|
||
const spy = jest.spyOn(global.console, 'error') | ||
|
||
initSSR() | ||
|
||
test('should render as web components', async () => { | ||
class Test extends Component { | ||
render() { | ||
return <div>test</div> | ||
} | ||
} | ||
defineAsCustomElements(Test, 'nano-test1', []) | ||
|
||
const html = renderSSR( | ||
<div> | ||
<nano-test1></nano-test1> | ||
</div> | ||
) | ||
|
||
expect(html).toBe('<div><div>test</div></div>') | ||
expect(spy).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('should render with props', async () => { | ||
class Test extends Component { | ||
value: string | ||
|
||
constructor(props: { value: string }) { | ||
super(props) | ||
|
||
this.value = props.value | ||
} | ||
|
||
render() { | ||
return <div>test : {this.value}</div> | ||
} | ||
} | ||
defineAsCustomElements(Test, 'nano-test3', ['value'], { mode: 'open' }) | ||
|
||
document.body.innerHTML = '<nano-test3 value="fuga"></nano-test3>' | ||
|
||
await wait() | ||
|
||
const comp = document.querySelector('nano-test3') | ||
expect(comp?.shadowRoot?.innerHTML).toEqual('<div><div>test : fuga</div></div>') | ||
expect(spy).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('should update render result with props change', async () => { | ||
class Test extends Component { | ||
value: string | ||
|
||
constructor(props: { value: string }) { | ||
super(props) | ||
|
||
this.value = props.value | ||
} | ||
|
||
render() { | ||
return <div>test : {this.value}</div> | ||
} | ||
} | ||
defineAsCustomElements(Test, 'nano-test4', ['value'], { mode: 'open' }) | ||
|
||
document.body.innerHTML = '<nano-test4 value="fuga"></nano-test4>' | ||
|
||
await wait() | ||
|
||
const comp = document.querySelector('nano-test4') | ||
expect(comp?.shadowRoot?.innerHTML).toEqual('<div><div>test : fuga</div></div>') | ||
|
||
document.body.innerHTML = '<nano-test4 value="hoge"></nano-test4>' | ||
const compChanged = document.querySelector('nano-test4') | ||
expect(compChanged?.shadowRoot?.innerHTML).toEqual('<div><div>test : hoge</div></div>') | ||
expect(spy).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('should change render result with state change', async () => { | ||
class Test extends Component { | ||
value: number = 0 | ||
|
||
changeValue(newValue: number) { | ||
this.value += newValue | ||
this.update() | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<div>Counter: {this.value}</div> | ||
<button onClick={() => this.changeValue(1)}>Increment</button> | ||
</div> | ||
) | ||
} | ||
} | ||
defineAsCustomElements(Test, 'nano-test5', [], { mode: 'open' }) | ||
|
||
document.body.innerHTML = '<nano-test5></nano-test5>' | ||
|
||
await wait() | ||
|
||
const comp = document.querySelector('nano-test5') | ||
expect(comp?.shadowRoot?.innerHTML).toEqual('<div><div><div>Counter: 0</div><button>Increment</button></div></div>') | ||
|
||
comp?.shadowRoot?.querySelector('button')?.click() | ||
expect(comp?.shadowRoot?.innerHTML).toEqual('<div><div><div>Counter: 1</div><button>Increment</button></div></div>') | ||
expect(spy).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('should keep state with props change', async () => { | ||
class Test extends Component { | ||
stateValue: number = 0 | ||
value: number | ||
|
||
constructor(props: { value: number }) { | ||
super(props) | ||
this.value = props.value | ||
} | ||
|
||
changeValue(newValue: number) { | ||
this.stateValue += newValue | ||
this.update() | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<div>Counter: {this.stateValue}</div> | ||
<div>props: {this.value}</div> | ||
<button onClick={() => this.changeValue(1)}>Increment</button> | ||
</div> | ||
) | ||
} | ||
} | ||
defineAsCustomElements(Test, 'nano-test6', ['value'], { mode: 'open' }) | ||
|
||
document.body.innerHTML = '<nano-test6 value="1"></nano-test6>' | ||
|
||
await wait() | ||
|
||
const comp = document.querySelector('nano-test6') | ||
expect(comp?.shadowRoot?.innerHTML).toEqual( | ||
'<div><div><div>Counter: 0</div><div>props: 1</div><button>Increment</button></div></div>' | ||
) | ||
|
||
comp?.shadowRoot?.querySelector('button')?.click() | ||
expect(comp?.shadowRoot?.innerHTML).toEqual( | ||
'<div><div><div>Counter: 1</div><div>props: 1</div><button>Increment</button></div></div>' | ||
) | ||
// @ts-ignore | ||
comp.attributes.value?.value = 2 | ||
expect(comp?.shadowRoot?.innerHTML).toEqual( | ||
'<div><div><div>Counter: 1</div><div>props: 2</div><button>Increment</button></div></div>' | ||
) | ||
expect(spy).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('should render also with functional component', async () => { | ||
const Test = function ({ value }: { value: string }) { | ||
return <p>{value}</p> | ||
} | ||
|
||
defineAsCustomElements(Test, 'nano-test7', ['value'], { mode: 'open' }) | ||
|
||
document.body.innerHTML = '<nano-test7 value="hoge"></nano-test7>' | ||
|
||
await wait() | ||
|
||
const comp = document.querySelector('nano-test7') | ||
expect(comp?.shadowRoot?.innerHTML).toEqual('<div><p>hoge</p></div>') | ||
// @ts-ignore | ||
comp.attributes.value?.value = 'bar' | ||
expect(comp?.shadowRoot?.innerHTML).toEqual('<div><p>bar</p></div>') | ||
expect(spy).not.toHaveBeenCalled() | ||
}) | ||
|
||
test('should render also with slot', async () => { | ||
class Test extends Component { | ||
header: string | ||
children: any | ||
|
||
constructor(props: { header: string; children: any }) { | ||
super(props) | ||
this.header = props.header | ||
this.children = props.children | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<header>{this.header}</header> | ||
<main>{this.children}</main> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
defineAsCustomElements(Test, 'nano-test8', ['header'], { mode: 'open' }) | ||
|
||
document.body.innerHTML = '<nano-test8 header="nano jsx"><p>hoge</p></nano-test8>' | ||
|
||
await wait() | ||
|
||
const comp = document.querySelector('nano-test8') | ||
expect(comp?.shadowRoot?.innerHTML).toEqual('<div><div><header>nano jsx</header><main><p>hoge</p></main></div></div>') | ||
expect(spy).not.toHaveBeenCalled() | ||
}) |