2019-07-14 22:18:00 +02:00
import { Component , Env } from "../../src/component/component" ;
import { QWeb } from "../../src/qweb/qweb" ;
2019-09-12 09:45:32 +02:00
import { xml } from "../../src/tags" ;
2019-09-26 21:56:01 +02:00
import { useState , useRef } from "../../src/hooks" ;
2019-07-14 22:18:00 +02:00
import { EventBus } from "../../src/core/event_bus" ;
2019-02-06 13:26:21 +01:00
import {
2019-02-07 12:10:44 +01:00
makeDeferred ,
2019-02-06 13:26:21 +01:00
makeTestFixture ,
2019-05-17 15:52:16 +02:00
makeTestEnv ,
2019-02-06 13:26:21 +01:00
nextMicroTick ,
2019-02-07 12:10:44 +01:00
nextTick ,
2019-06-13 16:27:12 +02:00
normalize ,
editInput
2019-07-14 22:18:00 +02:00
} from "../helpers" ;
2019-01-16 13:19:58 +01:00
2019-01-25 15:28:21 +01:00
//------------------------------------------------------------------------------
// Setup and helpers
//------------------------------------------------------------------------------
// We create before each test:
// - fixture: a div, appended to the DOM, intended to be the target of dom
// manipulations. Note that it is removed after each test.
2019-06-20 09:42:33 +02:00
// - env: a WEnv, necessary to create new components
2019-01-23 14:28:41 +01:00
2019-01-25 15:28:21 +01:00
let fixture : HTMLElement ;
2019-03-14 15:53:31 +01:00
let env : Env ;
2019-01-23 11:12:54 +01:00
2019-01-25 15:28:21 +01:00
beforeEach ( ( ) = > {
2019-01-29 10:46:01 +01:00
fixture = makeTestFixture ( ) ;
2019-05-17 15:52:16 +02:00
env = makeTestEnv ( ) ;
2019-06-20 09:42:33 +02:00
env . qweb . addTemplate ( "WidgetA" , ` <div>Hello<t t-component="b"/></div> ` ) ;
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "WidgetB" , ` <div>world</div> ` ) ;
2019-10-31 13:31:10 +01:00
Component . env = env ;
2019-01-25 15:28:21 +01:00
} ) ;
afterEach ( ( ) = > {
fixture . remove ( ) ;
} ) ;
2019-01-16 13:19:58 +01:00
2020-02-05 21:30:14 +01:00
function children ( w : Component ) : Component [ ] {
2019-04-13 14:14:34 +02:00
const childrenMap = w . __owl__ . children ;
2019-01-27 13:50:53 +01:00
return Object . keys ( childrenMap ) . map ( id = > childrenMap [ id ] ) ;
}
2019-06-20 09:42:33 +02:00
// Test components
2019-01-25 15:44:32 +01:00
2020-02-05 21:30:14 +01:00
class WidgetB extends Component { }
2019-09-10 22:37:08 +02:00
2020-02-05 21:30:14 +01:00
class WidgetA extends Component {
2019-09-10 22:37:08 +02:00
static components = { b : WidgetB } ;
2019-01-27 13:50:53 +01:00
}
2019-01-25 15:28:21 +01:00
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
2019-01-16 13:19:58 +01:00
describe ( "basic widget properties" , ( ) = > {
2019-11-01 07:58:25 +01:00
test ( "props is set on root components" , async ( ) = > {
2020-02-05 21:30:14 +01:00
const widget = new Component ( null , { } ) ;
2019-11-01 07:58:25 +01:00
expect ( widget . props ) . toEqual ( { } ) ;
2019-03-22 09:28:56 +01:00
} ) ;
2019-01-16 13:19:58 +01:00
test ( "has no el after creation" , async ( ) = > {
2020-02-05 21:30:14 +01:00
const widget = new Component ( ) ;
2019-01-16 13:19:58 +01:00
expect ( widget . el ) . toBe ( null ) ;
} ) ;
test ( "can be mounted" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2019-10-25 15:45:56 +02:00
static template = xml ` <div>content</div> ` ;
}
2019-10-14 15:09:17 +02:00
const widget = new SomeWidget ( ) ;
2019-10-25 15:45:56 +02:00
widget . mount ( fixture ) ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>content</div>" ) ;
2019-01-16 13:19:58 +01:00
} ) ;
2019-11-22 14:49:37 +01:00
test ( "can be mounted on a documentFragment" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2019-11-22 14:49:37 +01:00
static template = xml ` <div>content</div> ` ;
}
const widget = new SomeWidget ( ) ;
await widget . mount ( document . createDocumentFragment ( ) ) ;
expect ( fixture . innerHTML ) . toBe ( "" ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>content</div>" ) ;
} ) ;
2019-11-02 19:45:28 +01:00
test ( "display a nice message if mounted on a non existing node" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2019-11-02 19:45:28 +01:00
static template = xml ` <div>content</div> ` ;
}
const widget = new SomeWidget ( ) ;
let error ;
try {
await widget . mount ( null as any ) ;
} catch ( e ) {
error = e ;
}
expect ( error ) . toBeDefined ( ) ;
expect ( error . message ) . toBe (
"Component 'SomeWidget' cannot be mounted: the target is not a valid DOM node.\nMaybe the DOM is not ready yet? (in that case, you can use owl.utils.whenReady)"
) ;
} ) ;
2019-11-14 15:33:45 +01:00
test ( "display an error message if result of rendering is empty" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2019-11-14 15:33:45 +01:00
static template = xml ` <t/> ` ;
}
const widget = new SomeWidget ( ) ;
let error ;
try {
await widget . mount ( fixture ) ;
} catch ( e ) {
error = e ;
}
expect ( error ) . toBeDefined ( ) ;
expect ( error . message ) . toBe ( "Rendering 'SomeWidget' did not return anything" ) ;
} ) ;
2019-06-14 23:33:45 +02:00
test ( "crashes if it cannot find a template" , async ( ) = > {
expect . assertions ( 1 ) ;
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component { }
2019-06-14 23:33:45 +02:00
try {
2019-10-14 15:09:17 +02:00
new SomeWidget ( ) ;
2019-06-14 23:33:45 +02:00
} catch ( e ) {
2019-06-28 10:32:03 +02:00
expect ( e . message ) . toBe ( 'Could not find template for component "SomeWidget"' ) ;
2019-06-14 23:33:45 +02:00
}
} ) ;
2019-01-16 13:19:58 +01:00
test ( "can be clicked on and updated" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Counter extends Component {
2019-12-11 11:36:59 +01:00
static template = xml `
<div><t t-esc="state.counter"/><button t-on-click="state.counter++">Inc</button></div> ` ;
state = useState ( {
counter : 0
} ) ;
}
2019-10-14 15:09:17 +02:00
const counter = new Counter ( ) ;
2019-10-25 15:45:56 +02:00
counter . mount ( fixture ) ;
await nextTick ( ) ;
2019-05-06 13:45:50 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div>0<button>Inc</button></div>" ) ;
const button = ( < HTMLElement > counter . el ) . getElementsByTagName ( "button" ) [ 0 ] ;
2019-10-25 15:45:56 +02:00
button . click ( ) ;
2019-05-06 13:45:50 +02:00
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>1<button>Inc</button></div>" ) ;
} ) ;
2019-12-19 14:20:16 +01:00
test ( "can handle empty props" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-19 14:20:16 +01:00
static template = xml ` <span><t t-esc="props.val"/></span> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-19 14:20:16 +01:00
static template = xml ` <div><Child val=""/></div> ` ;
static components = { Child } ;
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
expect ( env . qweb . templates [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-12-19 21:54:35 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div><span></span></div>" ) ;
2019-12-19 14:20:16 +01:00
} ) ;
2019-05-06 13:45:50 +02:00
test ( "cannot be clicked on and updated if not in DOM" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Counter extends Component {
2019-12-11 11:36:59 +01:00
static template = xml `
<div><t t-esc="state.counter"/><button t-on-click="state.counter++">Inc</button></div> ` ;
state = useState ( {
counter : 0
} ) ;
}
2019-10-14 15:09:17 +02:00
const counter = new Counter ( ) ;
2019-01-16 13:19:58 +01:00
const target = document . createElement ( "div" ) ;
await counter . mount ( target ) ;
expect ( target . innerHTML ) . toBe ( "<div>0<button>Inc</button></div>" ) ;
2019-01-25 15:28:21 +01:00
const button = ( < HTMLElement > counter . el ) . getElementsByTagName ( "button" ) [ 0 ] ;
2019-10-25 15:45:56 +02:00
button . click ( ) ;
2019-04-17 10:07:31 +02:00
await nextTick ( ) ;
2019-05-06 13:45:50 +02:00
expect ( target . innerHTML ) . toBe ( "<div>0<button>Inc</button></div>" ) ;
2019-12-04 21:29:34 +01:00
expect ( counter . state . counter ) . toBe ( 0 ) ;
2019-01-16 13:19:58 +01:00
} ) ;
2019-01-21 14:38:44 +01:00
test ( "widget style and classname" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class StyledWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<div style="font-weight:bold;" class="some-class">world</div>
` ;
}
2019-10-14 15:09:17 +02:00
const widget = new StyledWidget ( ) ;
2019-01-25 15:28:21 +01:00
await widget . mount ( fixture ) ;
2019-06-28 10:32:03 +02:00
expect ( fixture . innerHTML ) . toBe ( ` <div style="font-weight:bold;" class="some-class">world</div> ` ) ;
2019-01-21 14:38:44 +01:00
} ) ;
2019-01-24 13:48:31 +01:00
2019-04-17 10:07:31 +02:00
test ( "changing state before first render does not trigger a render" , async ( ) = > {
2019-10-25 15:45:56 +02:00
const steps : string [ ] = [ ] ;
2020-02-05 21:30:14 +01:00
class TestW extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-09-24 14:06:53 +02:00
state = useState ( { drinks : 1 } ) ;
2019-01-24 13:48:31 +01:00
async willStart() {
2019-04-17 10:07:31 +02:00
this . state . drinks ++ ;
2019-01-24 13:48:31 +01:00
}
2019-09-19 11:42:00 +02:00
async __render ( f ) {
2019-10-25 15:45:56 +02:00
steps . push ( "__render" ) ;
2019-09-19 11:42:00 +02:00
return super . __render ( f ) ;
2019-01-24 13:48:31 +01:00
}
2019-10-25 15:45:56 +02:00
mounted() {
steps . push ( "mounted" ) ;
}
patched() {
steps . push ( "patched" ) ;
}
2019-01-24 13:48:31 +01:00
}
2019-10-14 15:09:17 +02:00
const widget = new TestW ( ) ;
2019-01-25 15:28:21 +01:00
await widget . mount ( fixture ) ;
2019-10-25 15:45:56 +02:00
expect ( steps ) . toEqual ( [ "__render" , "mounted" ] ) ;
} ) ;
test ( "changing state before first render does not trigger a render (with parent)" , async ( ) = > {
const steps : string [ ] = [ ] ;
2020-02-05 21:30:14 +01:00
class TestW extends Component {
2019-10-25 15:45:56 +02:00
static template = xml ` <span>W</span> ` ;
state = useState ( { drinks : 1 } ) ;
async willStart() {
this . state . drinks ++ ;
}
async __render ( f ) {
steps . push ( "__render" ) ;
return super . __render ( f ) ;
}
mounted() {
steps . push ( "mounted" ) ;
}
patched() {
steps . push ( "patched" ) ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-10-25 15:45:56 +02:00
state = useState ( { flag : false } ) ;
static components = { TestW } ;
static template = xml ` <div><TestW t-if="state.flag"/></div> ` ;
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-10-25 15:45:56 +02:00
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div></div>" ) ;
expect ( steps ) . toEqual ( [ ] ) ;
parent . state . flag = true ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>W</span></div>" ) ;
expect ( steps ) . toEqual ( [ "__render" , "mounted" ] ) ;
2019-01-24 13:48:31 +01:00
} ) ;
2019-01-26 20:33:23 +01:00
2019-10-24 21:20:52 +02:00
test ( "render method wait until rendering is done" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class TestW extends Component {
2019-10-24 21:20:52 +02:00
static template = xml ` <div><t t-esc="state.drinks"/></div> ` ;
state = { drinks : 1 } ;
}
2019-10-14 15:09:17 +02:00
const widget = new TestW ( ) ;
2019-10-24 21:20:52 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>1</div>" ) ;
widget . state . drinks = 2 ;
const renderPromise = widget . render ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>1</div>" ) ;
await renderPromise ;
expect ( fixture . innerHTML ) . toBe ( "<div>2</div>" ) ;
} ) ;
2019-01-27 15:12:35 +01:00
test ( "keeps a reference to env" , async ( ) = > {
2020-02-05 21:30:14 +01:00
const widget = new Component ( ) ;
2019-01-26 20:33:23 +01:00
expect ( widget . env ) . toBe ( env ) ;
} ) ;
2019-01-27 15:12:35 +01:00
test ( "do not remove previously rendered dom if not necessary" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
const widget = new SomeComponent ( ) ;
2019-01-27 15:12:35 +01:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( ` <div></div> ` ) ;
widget . el ! . appendChild ( document . createElement ( "span" ) ) ;
expect ( fixture . innerHTML ) . toBe ( ` <div><span></span></div> ` ) ;
2019-10-25 15:45:56 +02:00
widget . render ( ) ; // FIXME?
2019-01-27 15:12:35 +01:00
expect ( fixture . innerHTML ) . toBe ( ` <div><span></span></div> ` ) ;
} ) ;
2019-09-02 15:27:37 +02:00
test ( "reconciliation alg is not confused in some specific situation" , async ( ) = > {
// in this test, we set t-key to 4 because it was in conflict with the
// template id corresponding to the first child.
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <span>child</span> ` ;
}
2019-09-02 15:27:37 +02:00
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<div>
<Child />
<Child t-key="4"/>
</div>
` ;
2019-09-10 22:37:08 +02:00
static components = { Child } ;
2019-09-02 15:27:37 +02:00
}
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-09-02 15:27:37 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>child</span><span>child</span></div>" ) ;
} ) ;
2019-10-16 14:51:53 +02:00
test ( "reconciliation alg works for t-foreach in t-foreach" , async ( ) = > {
const warn = console . warn ;
console . warn = ( ) = > { } ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-10-16 14:51:53 +02:00
static template = xml ` <div><t t-esc="props.blip"/></div> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-10-16 14:51:53 +02:00
static template = xml `
<div>
<t t-foreach="state.s" t-as="section">
<t t-foreach="section.blips" t-as="blip">
<Child blip="blip"/>
</t>
</t>
</div> ` ;
static components = { Child } ;
state = { s : [ { blips : [ "a1" , "a2" ] } , { blips : [ "b1" ] } ] } ;
}
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-10-16 14:51:53 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div>a1</div><div>a2</div><div>b1</div></div>" ) ;
expect ( env . qweb . templates [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
console . warn = warn ;
} ) ;
2019-12-16 16:50:21 +01:00
test ( "reconciliation alg works for t-foreach in t-foreach, 2" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-16 16:50:21 +01:00
static template = xml ` <div><t t-esc="props.row + '_' + props.col"/></div> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-16 16:50:21 +01:00
static template = xml `
<div>
<p t-foreach="state.rows" t-as="row" t-key="row">
<p t-foreach="state.cols" t-as="col" t-key="col">
<Child row="row" col="col"/>
</p>
</p>
</div> ` ;
static components = { Child } ;
state = useState ( { rows : [ 1 , 2 ] , cols : [ "a" , "b" ] } ) ;
}
const widget = new Parent ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe (
"<div><p><p><div>1_a</div></p><p><div>1_b</div></p></p><p><p><div>2_a</div></p><p><div>2_b</div></p></p></div>"
) ;
widget . state . rows = [ 2 , 1 ] ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe (
"<div><p><p><div>2_a</div></p><p><div>2_b</div></p></p><p><p><div>1_a</div></p><p><div>1_b</div></p></p></div>"
) ;
} ) ;
2019-10-16 14:51:53 +02:00
test ( "same t-keys in two different places" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-10-16 14:51:53 +02:00
static template = xml ` <span><t t-esc="props.blip"/></span> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-10-16 14:51:53 +02:00
static template = xml `
<div>
<div><Child t-key="1" blip="'1'"/></div>
<div><Child t-key="1" blip="'2'"/></div>
</div> ` ;
static components = { Child } ;
}
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-10-16 14:51:53 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div><span>1</span></div><div><span>2</span></div></div>" ) ;
2019-12-16 16:50:21 +01:00
expect ( env . qweb . templates [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-10-16 14:51:53 +02:00
} ) ;
2019-11-27 07:24:33 +01:00
test ( "t-key on a component with t-if, and a sibling component" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-11-27 07:24:33 +01:00
static template = xml ` <span>child</span> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-11-27 07:24:33 +01:00
static template = xml `
<div>
<Child t-if="false" t-key="'str'"/>
<Child/>
</div> ` ;
static components = { Child } ;
}
const widget = new Parent ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>child</span></div>" ) ;
expect ( env . qweb . templates [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-01-16 13:19:58 +01:00
} ) ;
2019-01-16 23:03:51 +01:00
describe ( "lifecycle hooks" , ( ) = > {
test ( "willStart hook is called" , async ( ) = > {
let willstart = false ;
2020-02-05 21:30:14 +01:00
class HookWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-16 23:03:51 +01:00
async willStart() {
willstart = true ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new HookWidget ( ) ;
2019-01-25 15:28:21 +01:00
await widget . mount ( fixture ) ;
2019-01-16 23:03:51 +01:00
expect ( willstart ) . toBe ( true ) ;
} ) ;
test ( "mounted hook is not called if not in DOM" , async ( ) = > {
let mounted = false ;
2020-02-05 21:30:14 +01:00
class HookWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-16 23:03:51 +01:00
async mounted() {
mounted = true ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new HookWidget ( ) ;
2019-01-16 23:03:51 +01:00
const target = document . createElement ( "div" ) ;
await widget . mount ( target ) ;
expect ( mounted ) . toBe ( false ) ;
} ) ;
2019-01-21 14:54:13 +01:00
test ( "mounted hook is called if mounted in DOM" , async ( ) = > {
let mounted = false ;
2020-02-05 21:30:14 +01:00
class HookWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-21 14:54:13 +01:00
async mounted() {
mounted = true ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new HookWidget ( ) ;
2019-01-25 15:28:21 +01:00
await widget . mount ( fixture ) ;
2019-01-21 14:54:13 +01:00
expect ( mounted ) . toBe ( true ) ;
} ) ;
2019-01-21 16:26:51 +01:00
2019-01-25 11:12:20 +01:00
test ( "willStart hook is called on subwidget" , async ( ) = > {
let ok = false ;
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-25 11:12:20 +01:00
async willStart() {
ok = true ;
}
}
2019-09-10 22:37:08 +02:00
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <div><t t-component="child"/></div> ` ;
2019-09-10 22:37:08 +02:00
static components = { child : ChildWidget } ;
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-01-25 15:28:21 +01:00
await widget . mount ( fixture ) ;
2019-01-25 11:12:20 +01:00
expect ( ok ) . toBe ( true ) ;
} ) ;
2019-06-20 09:42:33 +02:00
test ( "mounted hook is called on subcomponents, in proper order" , async ( ) = > {
2019-04-26 16:20:50 +02:00
const steps : any [ ] = [ ] ;
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-25 11:01:43 +01:00
mounted() {
expect ( document . body . contains ( this . el ) ) . toBe ( true ) ;
2019-04-26 16:20:50 +02:00
steps . push ( "child:mounted" ) ;
2019-01-21 16:26:51 +01:00
}
}
2019-09-10 22:37:08 +02:00
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-19 11:42:00 +02:00
static template = xml ` <div><ChildWidget /></div> ` ;
2019-09-12 09:45:32 +02:00
static components = { ChildWidget } ;
2019-09-10 22:37:08 +02:00
mounted() {
steps . push ( "parent:mounted" ) ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-01-25 15:28:21 +01:00
await widget . mount ( fixture ) ;
2019-04-26 16:20:50 +02:00
expect ( steps ) . toEqual ( [ "child:mounted" , "parent:mounted" ] ) ;
2019-01-21 16:26:51 +01:00
} ) ;
2019-01-25 11:12:20 +01:00
2019-06-20 09:42:33 +02:00
test ( "mounted hook is called on subsubcomponents, in proper order" , async ( ) = > {
2019-04-26 17:32:02 +02:00
const steps : any [ ] = [ ] ;
2020-02-05 21:30:14 +01:00
class ChildChildWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-04-26 17:32:02 +02:00
mounted() {
2019-09-10 22:37:08 +02:00
steps . push ( "childchild:mounted" ) ;
2019-04-26 17:32:02 +02:00
}
willUnmount() {
2019-09-10 22:37:08 +02:00
steps . push ( "childchild:willUnmount" ) ;
2019-04-26 17:32:02 +02:00
}
}
2019-09-10 22:37:08 +02:00
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <div><t t-component="childchild"/></div> ` ;
2019-09-10 22:37:08 +02:00
static components = { childchild : ChildChildWidget } ;
2019-04-26 17:32:02 +02:00
mounted() {
steps . push ( "child:mounted" ) ;
}
willUnmount() {
steps . push ( "child:willUnmount" ) ;
}
}
2019-09-10 22:37:08 +02:00
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <div><t t-if="state.flag"><t t-component="child"/></t></div> ` ;
2019-09-10 22:37:08 +02:00
static components = { child : ChildWidget } ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : false } ) ;
2019-04-26 17:32:02 +02:00
mounted() {
2019-09-10 22:37:08 +02:00
steps . push ( "parent:mounted" ) ;
2019-04-26 17:32:02 +02:00
}
willUnmount() {
2019-09-10 22:37:08 +02:00
steps . push ( "parent:willUnmount" ) ;
2019-04-26 17:32:02 +02:00
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-04-26 17:32:02 +02:00
await widget . mount ( fixture ) ;
expect ( steps ) . toEqual ( [ "parent:mounted" ] ) ;
widget . state . flag = true ;
await nextTick ( ) ;
widget . destroy ( ) ;
expect ( steps ) . toEqual ( [
"parent:mounted" ,
"childchild:mounted" ,
"child:mounted" ,
"parent:willUnmount" ,
"child:willUnmount" ,
"childchild:willUnmount"
] ) ;
} ) ;
2019-06-20 09:42:33 +02:00
test ( "willPatch, patched hook are called on subsubcomponents, in proper order" , async ( ) = > {
2019-04-27 19:20:29 +02:00
const steps : any [ ] = [ ] ;
2019-09-10 22:37:08 +02:00
env . qweb . addTemplate ( "ChildWidget" , ` <div><t t-component="childchild" n="props.n"/></div> ` ) ;
env . qweb . addTemplate ( "ChildChildWidget" , ` <div><t t-esc="props.n"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class ChildChildWidget extends Component {
2019-04-27 19:20:29 +02:00
willPatch() {
2019-09-10 22:37:08 +02:00
steps . push ( "childchild:willPatch" ) ;
2019-04-27 19:20:29 +02:00
}
patched() {
2019-09-10 22:37:08 +02:00
steps . push ( "childchild:patched" ) ;
2019-04-27 19:20:29 +02:00
}
}
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { childchild : ChildChildWidget } ;
2019-04-27 19:20:29 +02:00
willPatch() {
steps . push ( "child:willPatch" ) ;
}
patched() {
steps . push ( "child:patched" ) ;
}
}
2019-05-15 11:15:08 +02:00
2019-09-10 22:37:08 +02:00
env . qweb . addTemplate ( "ParentWidget" , ` <div><t t-component="child" n="state.n"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : ChildWidget } ;
2019-09-24 14:06:53 +02:00
state = useState ( { n : 1 } ) ;
2019-04-27 19:20:29 +02:00
willPatch() {
2019-09-10 22:37:08 +02:00
steps . push ( "parent:willPatch" ) ;
2019-04-27 19:20:29 +02:00
}
patched() {
2019-09-10 22:37:08 +02:00
steps . push ( "parent:patched" ) ;
2019-04-27 19:20:29 +02:00
}
}
2019-09-10 22:37:08 +02:00
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-04-27 19:20:29 +02:00
await widget . mount ( fixture ) ;
expect ( steps ) . toEqual ( [ ] ) ;
widget . state . n = 2 ;
await nextTick ( ) ;
widget . destroy ( ) ;
expect ( steps ) . toEqual ( [
"parent:willPatch" ,
"child:willPatch" ,
"childchild:willPatch" ,
"childchild:patched" ,
"child:patched" ,
"parent:patched"
] ) ;
} ) ;
2019-01-25 11:12:20 +01:00
test ( "willStart, mounted on subwidget rendered after main is mounted in some other position" , async ( ) = > {
2019-04-26 16:20:50 +02:00
const steps : string [ ] = [ ] ;
2019-02-03 09:42:35 +01:00
// the t-else part in the template is important. This is
// necessary to have a situation that could confuse the vdom
// patching algorithm
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate (
"ParentWidget" ,
`
2019-01-25 15:28:21 +01:00
<div>
<t t-if="state.ok">
2019-06-20 09:42:33 +02:00
<t t-component="child"/>
2019-01-25 15:28:21 +01:00
</t>
2019-12-11 11:01:54 +01:00
<t t-else="">
2019-01-25 15:28:21 +01:00
<div/>
</t>
2019-05-15 11:15:08 +02:00
</div> `
) ;
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-25 11:12:20 +01:00
async willStart() {
2019-04-26 16:20:50 +02:00
steps . push ( "child:willStart" ) ;
2019-01-25 11:12:20 +01:00
}
mounted() {
2019-04-26 16:20:50 +02:00
steps . push ( "child:mounted" ) ;
2019-01-25 11:12:20 +01:00
}
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-24 14:06:53 +02:00
state = useState ( { ok : false } ) ;
2019-09-10 22:37:08 +02:00
static components = { child : ChildWidget } ;
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-01-25 15:28:21 +01:00
await widget . mount ( fixture ) ;
2019-04-26 16:20:50 +02:00
expect ( steps ) . toEqual ( [ ] ) ;
2019-04-16 22:59:54 +02:00
widget . state . ok = true ;
await nextTick ( ) ;
2019-04-26 16:20:50 +02:00
expect ( steps ) . toEqual ( [ "child:willStart" , "child:mounted" ] ) ;
2019-01-25 11:12:20 +01:00
} ) ;
2019-01-24 22:17:06 +01:00
2019-10-25 15:45:56 +02:00
test ( "mounted hook is correctly called on subcomponents created in mounted hook" , async ( ) = > {
2019-01-24 22:17:06 +01:00
// the issue here is that the parent widget creates in the
// mounted hook a new widget, which means that it modifies
// in place its list of children. But this list of children is currently
// being visited, so the mount action of the parent could cause a mount
// action of the new child widget, even though it is not ready yet.
expect . assertions ( 1 ) ;
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-24 22:17:06 +01:00
mounted() {
const child = new ChildWidget ( this ) ;
child . mount ( this . el ! ) ;
}
}
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-24 22:17:06 +01:00
mounted() {
expect ( this . el ) . toBeTruthy ( ) ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-10-25 15:45:56 +02:00
await widget . mount ( fixture ) ; // wait for ParentWidget
await nextTick ( ) ; // wait for ChildWidget
2019-01-24 22:17:06 +01:00
} ) ;
2019-01-27 15:40:10 +01:00
2019-06-20 09:42:33 +02:00
test ( "components are unmounted and destroyed if no longer in DOM" , async ( ) = > {
2019-01-27 15:40:10 +01:00
let steps : string [ ] = [ ] ;
2019-09-12 09:45:32 +02:00
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-27 15:40:10 +01:00
constructor ( parent ) {
super ( parent ) ;
steps . push ( "init" ) ;
}
async willStart() {
steps . push ( "willstart" ) ;
}
mounted() {
steps . push ( "mounted" ) ;
}
willUnmount() {
steps . push ( "willunmount" ) ;
}
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<div>
<t t-if="state.ok"><ChildWidget /></t>
</div>
` ;
static components = { ChildWidget } ;
2019-09-24 14:06:53 +02:00
state = useState ( { ok : true } ) ;
2019-09-10 22:37:08 +02:00
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-01-27 15:40:10 +01:00
await widget . mount ( fixture ) ;
expect ( steps ) . toEqual ( [ "init" , "willstart" , "mounted" ] ) ;
2019-04-17 10:07:31 +02:00
widget . state . ok = false ;
await nextTick ( ) ;
2019-04-10 09:35:20 +02:00
expect ( steps ) . toEqual ( [ "init" , "willstart" , "mounted" , "willunmount" ] ) ;
2019-01-27 15:40:10 +01:00
} ) ;
2019-01-27 21:03:13 +01:00
2019-06-20 09:42:33 +02:00
test ( "components are unmounted and destroyed if no longer in DOM, even after updateprops" , async ( ) = > {
2019-04-09 15:24:15 +02:00
let childUnmounted = false ;
2019-09-12 09:45:32 +02:00
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <span><t t-esc="props.n"/></span> ` ;
2019-04-09 15:24:15 +02:00
willUnmount() {
childUnmounted = true ;
}
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<div>
<div t-if="state.flag">
<ChildWidget n="state.n"/>
</div>
</div>
` ;
2019-09-10 22:37:08 +02:00
static components = { ChildWidget } ;
2019-09-24 14:06:53 +02:00
state = useState ( { n : 0 , flag : true } ) ;
2019-04-09 15:24:15 +02:00
increment() {
2019-04-17 10:07:31 +02:00
this . state . n += 1 ;
2019-04-09 15:24:15 +02:00
}
toggleSubWidget() {
2019-04-17 10:07:31 +02:00
this . state . flag = ! this . state . flag ;
2019-04-09 15:24:15 +02:00
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-04-09 15:24:15 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div><span>0</span></div></div>" ) ;
widget . increment ( ) ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div><span>1</span></div></div>" ) ;
widget . toggleSubWidget ( ) ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div></div>" ) ;
expect ( childUnmounted ) . toBe ( true ) ;
} ) ;
2019-01-27 21:03:13 +01:00
test ( "hooks are called in proper order in widget creation/destruction" , async ( ) = > {
let steps : string [ ] = [ ] ;
2019-09-12 09:45:32 +02:00
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-27 21:03:13 +01:00
constructor ( parent ) {
super ( parent ) ;
2019-09-10 22:37:08 +02:00
steps . push ( "c init" ) ;
2019-01-27 21:03:13 +01:00
}
async willStart() {
2019-09-10 22:37:08 +02:00
steps . push ( "c willstart" ) ;
2019-01-27 21:03:13 +01:00
}
mounted() {
2019-09-10 22:37:08 +02:00
steps . push ( "c mounted" ) ;
2019-01-27 21:03:13 +01:00
}
willUnmount() {
2019-09-10 22:37:08 +02:00
steps . push ( "c willunmount" ) ;
2019-01-27 21:03:13 +01:00
}
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <div><t t-component="child"/></div> ` ;
2019-09-10 22:37:08 +02:00
static components = { child : ChildWidget } ;
2019-10-14 15:09:17 +02:00
constructor ( parent ? ) {
2019-01-27 21:03:13 +01:00
super ( parent ) ;
2019-09-10 22:37:08 +02:00
steps . push ( "p init" ) ;
2019-01-27 21:03:13 +01:00
}
async willStart() {
2019-09-10 22:37:08 +02:00
steps . push ( "p willstart" ) ;
2019-01-27 21:03:13 +01:00
}
mounted() {
2019-09-10 22:37:08 +02:00
steps . push ( "p mounted" ) ;
2019-01-27 21:03:13 +01:00
}
willUnmount() {
2019-09-10 22:37:08 +02:00
steps . push ( "p willunmount" ) ;
2019-01-27 21:03:13 +01:00
}
}
2019-09-10 22:37:08 +02:00
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-01-27 21:03:13 +01:00
await widget . mount ( fixture ) ;
widget . destroy ( ) ;
expect ( steps ) . toEqual ( [
"p init" ,
"p willstart" ,
"c init" ,
"c willstart" ,
"c mounted" ,
2019-04-26 16:20:50 +02:00
"p mounted" ,
"p willunmount" ,
"c willunmount"
2019-01-27 21:03:13 +01:00
] ) ;
} ) ;
2019-01-28 16:16:22 +01:00
2019-04-09 13:51:04 +02:00
test ( "willUpdateProps hook is called" , async ( ) = > {
let def = makeDeferred ( ) ;
2019-06-22 14:44:09 +02:00
env . qweb . addTemplate ( "Parent" , '<span><Child n="state.n"/></span>' ) ;
2020-02-05 21:30:14 +01:00
class HookWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <span><t t-esc="props.n"/></span> ` ;
2019-04-09 13:51:04 +02:00
willUpdateProps ( nextProps ) {
expect ( nextProps . n ) . toBe ( 2 ) ;
return def ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-24 14:06:53 +02:00
state = useState ( { n : 1 } ) ;
2019-09-10 22:37:08 +02:00
static components = { Child : HookWidget } ;
}
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-04-09 13:51:04 +02:00
await widget . mount ( fixture ) ;
2019-04-27 19:20:29 +02:00
expect ( fixture . innerHTML ) . toBe ( "<span><span>1</span></span>" ) ;
widget . state . n = 2 ;
2019-04-09 13:51:04 +02:00
await nextTick ( ) ;
2019-04-27 19:20:29 +02:00
expect ( fixture . innerHTML ) . toBe ( "<span><span>1</span></span>" ) ;
2019-04-09 13:51:04 +02:00
def . resolve ( ) ;
await nextTick ( ) ;
2019-04-27 19:20:29 +02:00
expect ( fixture . innerHTML ) . toBe ( "<span><span>2</span></span>" ) ;
2019-04-09 13:51:04 +02:00
} ) ;
2019-04-17 10:07:31 +02:00
test ( "patched hook is called after updating State" , async ( ) = > {
2019-04-01 15:42:24 +02:00
let n = 0 ;
2020-02-05 21:30:14 +01:00
class TestWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-09-24 14:06:53 +02:00
state = useState ( { a : 1 } ) ;
2019-04-01 15:42:24 +02:00
2019-04-09 10:37:53 +02:00
patched() {
2019-04-01 15:42:24 +02:00
n ++ ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new TestWidget ( ) ;
2019-04-01 15:42:24 +02:00
await widget . mount ( fixture ) ;
expect ( n ) . toBe ( 0 ) ;
2019-04-17 10:07:31 +02:00
widget . state . a = 1 ; // empty update, should do nothing
await nextTick ( ) ;
2019-04-01 15:42:24 +02:00
expect ( n ) . toBe ( 0 ) ;
2019-04-17 10:07:31 +02:00
widget . state . a = 3 ;
await nextTick ( ) ;
2019-04-01 15:42:24 +02:00
expect ( n ) . toBe ( 1 ) ;
} ) ;
2019-04-09 10:37:53 +02:00
test ( "patched hook is called after updateProps" , async ( ) = > {
2019-04-01 15:42:24 +02:00
let n = 0 ;
2020-02-05 21:30:14 +01:00
class TestWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-04-09 10:37:53 +02:00
patched() {
2019-04-01 15:42:24 +02:00
n ++ ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <div><Child a="state.a"/></div> ` ;
2019-09-24 14:06:53 +02:00
state = useState ( { a : 1 } ) ;
2019-09-10 22:37:08 +02:00
static components = { Child : TestWidget } ;
}
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-04-01 15:42:24 +02:00
await widget . mount ( fixture ) ;
expect ( n ) . toBe ( 0 ) ;
2019-04-27 19:20:29 +02:00
widget . state . a = 2 ;
await nextTick ( ) ;
2019-04-01 15:42:24 +02:00
expect ( n ) . toBe ( 1 ) ;
} ) ;
2019-01-28 16:16:22 +01:00
test ( "shouldUpdate hook prevent rerendering" , async ( ) = > {
let shouldUpdate = false ;
2020-02-05 21:30:14 +01:00
class TestWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <div><t t-esc="props.val"/></div> ` ;
2019-01-28 16:16:22 +01:00
shouldUpdate() {
return shouldUpdate ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <div><Child val="state.val"/></div> ` ;
2019-09-10 22:37:08 +02:00
static components = { Child : TestWidget } ;
2019-09-24 14:06:53 +02:00
state = useState ( { val : 42 } ) ;
2019-09-10 22:37:08 +02:00
}
2019-09-12 09:45:32 +02:00
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-01-28 16:16:22 +01:00
await widget . mount ( fixture ) ;
2019-04-27 19:20:29 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div><div>42</div></div>" ) ;
widget . state . val = 123 ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div>42</div></div>" ) ;
2019-01-28 16:16:22 +01:00
shouldUpdate = true ;
2019-04-27 19:20:29 +02:00
widget . state . val = 666 ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div>666</div></div>" ) ;
2019-01-28 16:16:22 +01:00
} ) ;
2019-03-29 12:50:45 +01:00
test ( "sub widget (inside sub node): hooks are correctly called" , async ( ) = > {
let created = false ;
let mounted = false ;
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-03-29 12:50:45 +01:00
constructor ( parent , props ) {
super ( parent , props ) ;
created = true ;
}
mounted() {
mounted = true ;
}
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<div>
<t t-if="state.flag">
<div><t t-component="child"/></div>
</t>
</div>
` ;
2019-09-10 22:37:08 +02:00
static components = { child : ChildWidget } ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : false } ) ;
2019-09-10 22:37:08 +02:00
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-03-29 12:50:45 +01:00
await widget . mount ( fixture ) ;
expect ( created ) . toBe ( false ) ;
expect ( mounted ) . toBe ( false ) ;
2019-04-17 10:07:31 +02:00
widget . state . flag = true ;
await nextTick ( ) ;
2019-03-29 12:50:45 +01:00
expect ( mounted ) . toBe ( true ) ;
expect ( created ) . toBe ( true ) ;
} ) ;
2019-04-05 16:30:35 +02:00
2019-04-09 10:37:53 +02:00
test ( "willPatch/patched hook" , async ( ) = > {
2019-04-05 16:30:35 +02:00
const steps : string [ ] = [ ] ;
2019-09-12 09:45:32 +02:00
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-09-10 22:37:08 +02:00
willPatch() {
steps . push ( "child:willPatch" ) ;
}
patched() {
steps . push ( "child:patched" ) ;
}
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<div>
<t t-component="child" v="state.n"/>
</div>
` ;
2019-09-10 22:37:08 +02:00
static components = { child : ChildWidget } ;
2019-09-24 14:06:53 +02:00
state = useState ( { n : 1 } ) ;
2019-04-05 16:30:35 +02:00
willPatch() {
steps . push ( "parent:willPatch" ) ;
}
2019-09-29 16:47:06 +02:00
patched() {
2019-04-09 10:37:53 +02:00
steps . push ( "parent:patched" ) ;
2019-04-05 16:30:35 +02:00
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-04-05 16:30:35 +02:00
await widget . mount ( fixture ) ;
expect ( steps ) . toEqual ( [ ] ) ;
2019-04-16 22:59:54 +02:00
widget . state . n = 2 ;
await nextTick ( ) ;
2019-04-05 16:30:35 +02:00
// Not sure about this order. If you disagree, feel free to open an issue...
expect ( steps ) . toEqual ( [
2019-04-27 19:20:29 +02:00
"parent:willPatch" ,
2019-04-05 16:30:35 +02:00
"child:willPatch" ,
2019-04-09 10:37:53 +02:00
"child:patched" ,
"parent:patched"
2019-04-05 16:30:35 +02:00
] ) ;
} ) ;
2019-01-16 23:03:51 +01:00
} ) ;
describe ( "destroy method" , ( ) = > {
test ( "destroy remove the widget from the DOM" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
const widget = new SomeComponent ( ) ;
2019-01-25 15:28:21 +01:00
await widget . mount ( fixture ) ;
2019-01-16 23:03:51 +01:00
expect ( document . contains ( widget . el ) ) . toBe ( true ) ;
widget . destroy ( ) ;
expect ( document . contains ( widget . el ) ) . toBe ( false ) ;
2019-04-13 14:14:34 +02:00
expect ( widget . __owl__ . isMounted ) . toBe ( false ) ;
expect ( widget . __owl__ . isDestroyed ) . toBe ( true ) ;
2019-01-16 23:03:51 +01:00
} ) ;
2019-01-26 18:20:49 +01:00
2019-01-27 21:03:13 +01:00
test ( "destroying a parent also destroys its children" , async ( ) = > {
2019-10-14 15:09:17 +02:00
const parent = new WidgetA ( ) ;
2019-01-27 21:03:13 +01:00
await parent . mount ( fixture ) ;
const child = children ( parent ) [ 0 ] ;
2019-04-13 14:14:34 +02:00
expect ( child . __owl__ . isDestroyed ) . toBe ( false ) ;
2019-01-27 21:03:13 +01:00
parent . destroy ( ) ;
2019-04-13 14:14:34 +02:00
expect ( child . __owl__ . isDestroyed ) . toBe ( true ) ;
2019-01-27 21:03:13 +01:00
} ) ;
2019-01-27 13:50:53 +01:00
test ( "destroy remove the parent/children link" , async ( ) = > {
2019-10-14 15:09:17 +02:00
const parent = new WidgetA ( ) ;
2019-01-27 13:50:53 +01:00
await parent . mount ( fixture ) ;
const child = children ( parent ) [ 0 ] ;
2019-04-13 14:14:34 +02:00
expect ( child . __owl__ . parent ) . toBe ( parent ) ;
2019-01-27 13:50:53 +01:00
expect ( children ( parent ) . length ) . toBe ( 1 ) ;
child . destroy ( ) ;
2019-04-13 14:14:34 +02:00
expect ( child . __owl__ . parent ) . toBe ( null ) ;
2019-01-27 13:50:53 +01:00
expect ( children ( parent ) . length ) . toBe ( 0 ) ;
} ) ;
2019-01-27 21:50:22 +01:00
test ( "destroying a widget before willStart is done" , async ( ) = > {
2019-02-07 12:10:44 +01:00
let def = makeDeferred ( ) ;
2019-01-27 21:50:22 +01:00
let isRendered = false ;
2020-02-05 21:30:14 +01:00
class DelayedWidget extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
2019-01-27 21:50:22 +01:00
willStart() {
2019-02-07 12:10:44 +01:00
return def ;
2019-01-27 21:50:22 +01:00
}
}
expect ( fixture . innerHTML ) . toBe ( "" ) ;
2019-10-14 15:09:17 +02:00
const widget = new DelayedWidget ( ) ;
2019-01-27 21:50:22 +01:00
widget . mount ( fixture ) ;
2019-04-13 14:14:34 +02:00
expect ( widget . __owl__ . isMounted ) . toBe ( false ) ;
expect ( widget . __owl__ . isDestroyed ) . toBe ( false ) ;
2019-01-27 21:50:22 +01:00
widget . destroy ( ) ;
2019-04-13 14:14:34 +02:00
expect ( widget . __owl__ . isMounted ) . toBe ( false ) ;
expect ( widget . __owl__ . isDestroyed ) . toBe ( true ) ;
2019-02-07 12:10:44 +01:00
def . resolve ( ) ;
await nextTick ( ) ;
2019-01-27 21:50:22 +01:00
2019-04-13 14:14:34 +02:00
expect ( widget . __owl__ . isMounted ) . toBe ( false ) ;
expect ( widget . __owl__ . isDestroyed ) . toBe ( true ) ;
2019-04-26 16:20:50 +02:00
expect ( widget . __owl__ . vnode ) . toBe ( undefined ) ;
2019-01-27 21:50:22 +01:00
expect ( fixture . innerHTML ) . toBe ( "" ) ;
expect ( isRendered ) . toBe ( false ) ;
} ) ;
2019-09-01 21:24:54 +02:00
test ( "destroying a widget before being mounted" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class GrandChild extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<span>
<t t-esc="props.val.val"/>
</span>
` ;
}
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<span>
<GrandChild t-if="state.flag" val="something"/>
<button t-on-click="doSomething">click</button>
</span>
` ;
2019-09-10 22:37:08 +02:00
static components = { GrandChild } ;
2019-09-24 14:06:53 +02:00
state = useState ( { val : 33 , flag : false } ) ;
2019-09-01 21:24:54 +02:00
doSomething() {
this . state . val = 12 ;
this . state . flag = true ;
this . trigger ( "some-event" ) ;
}
get something() {
return { val : this.state.val } ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<div t-on-some-event="doStuff">
<Child />
</div>
` ;
2019-09-10 22:37:08 +02:00
static components = { Child } ;
2019-09-24 14:06:53 +02:00
state = useState ( { p : 1 } ) ;
2019-09-10 22:37:08 +02:00
doStuff() {
this . state . p = 2 ;
}
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-09-01 21:24:54 +02:00
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span><button>click</button></span></div>" ) ;
fixture . querySelector ( "button" ) ! . click ( ) ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span><span>12</span><button>click</button></span></div>" ) ;
} ) ;
2019-11-20 11:08:11 +01:00
test ( "destroying a widget before being mounted (2)" , async ( ) = > {
const steps : string [ ] = [ ] ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-11-20 11:08:11 +01:00
static template = xml ` <span></span> ` ;
willStart() {
steps . push ( "willStart" ) ;
2019-11-20 13:19:14 +01:00
return makeDeferred ( ) ;
2019-11-20 11:08:11 +01:00
}
__destroy ( parent ) {
steps . push ( "__destroy" ) ;
super . __destroy ( parent ) ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-11-20 11:08:11 +01:00
static template = xml ` <div><Child t-if="state.flag"/></div> ` ;
static components = { Child } ;
state = useState ( { flag : true } ) ;
}
const parent = new Parent ( ) ;
const prom = parent . mount ( fixture ) ;
await nextTick ( ) ; // wait for Child to be instantiated
parent . state . flag = false ;
await prom ;
expect ( fixture . innerHTML ) . toBe ( "<div></div>" ) ;
expect ( steps ) . toEqual ( [ "willStart" , "__destroy" ] ) ;
} ) ;
2019-01-16 23:03:51 +01:00
} ) ;
2019-01-20 14:46:36 +01:00
describe ( "composition" , ( ) = > {
2019-01-21 15:46:29 +01:00
test ( "a widget with a sub widget" , async ( ) = > {
2019-10-14 15:09:17 +02:00
const widget = new WidgetA ( ) ;
2019-01-25 15:28:21 +01:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>Hello<div>world</div></div>" ) ;
2019-04-13 14:14:34 +02:00
expect ( children ( widget ) [ 0 ] . __owl__ . parent ) . toBe ( widget ) ;
2019-01-20 14:46:36 +01:00
} ) ;
2019-01-21 15:46:29 +01:00
2019-06-20 09:42:33 +02:00
test ( "can use components from the global registry" , async ( ) = > {
2019-09-12 10:45:40 +02:00
QWeb . registerComponent ( "WidgetB" , WidgetB ) ;
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "ParentWidget" , ` <div><t t-component="WidgetB"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component { }
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-05-23 16:14:52 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div>world</div></div>" ) ;
2019-06-20 09:42:33 +02:00
delete QWeb . components [ "WidgetB" ] ;
2019-05-23 16:14:52 +02:00
} ) ;
2019-09-21 09:28:56 +02:00
test ( "can use dynamic components (the class) if given" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class A extends Component {
2019-09-21 09:28:56 +02:00
static template = xml ` <span>child a</span> ` ;
}
2020-02-05 21:30:14 +01:00
class B extends Component {
2019-09-21 09:28:56 +02:00
static template = xml ` <span>child b</span> ` ;
}
2020-02-05 21:30:14 +01:00
class App extends Component {
2019-09-21 09:28:56 +02:00
static template = xml ` <t t-component="myComponent" t-key="state.child"/> ` ;
2019-09-24 14:06:53 +02:00
state = useState ( {
2019-09-21 09:28:56 +02:00
child : "a"
2019-09-24 14:06:53 +02:00
} ) ;
2019-09-21 09:28:56 +02:00
get myComponent() {
return this . state . child === "a" ? A : B ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new App ( ) ;
2019-09-21 09:28:56 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>child a</span>" ) ;
widget . state . child = "b" ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>child b</span>" ) ;
} ) ;
2019-10-25 15:45:56 +02:00
test ( "can use dynamic components (the class) if given (with different root tagname)" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class A extends Component {
2019-10-25 15:45:56 +02:00
static template = xml ` <span>child a</span> ` ;
}
2020-02-05 21:30:14 +01:00
class B extends Component {
2019-10-25 15:45:56 +02:00
static template = xml ` <div>child b</div> ` ;
}
2020-02-05 21:30:14 +01:00
class App extends Component {
2019-10-25 15:45:56 +02:00
static template = xml ` <t t-component="myComponent" t-key="state.child"/> ` ;
state = useState ( {
child : "a"
} ) ;
get myComponent() {
return this . state . child === "a" ? A : B ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new App ( ) ;
2019-10-25 15:45:56 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>child a</span>" ) ;
widget . state . child = "b" ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>child b</div>" ) ;
} ) ;
2019-05-23 16:14:52 +02:00
test ( "don't fallback to global registry if widget defined locally" , async ( ) = > {
2019-09-12 10:45:40 +02:00
QWeb . registerComponent ( "WidgetB" , WidgetB ) ; // should not use this widget
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "ParentWidget" , ` <div><t t-component="WidgetB"/></div> ` ) ;
2019-05-23 16:14:52 +02:00
env . qweb . addTemplate ( "AnotherWidgetB" , ` <span>Belgium</span> ` ) ;
2020-02-05 21:30:14 +01:00
class AnotherWidgetB extends Component { }
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { WidgetB : AnotherWidgetB } ;
2019-05-23 16:14:52 +02:00
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-05-23 16:14:52 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>Belgium</span></div>" ) ;
2019-06-20 09:42:33 +02:00
delete QWeb . components [ "WidgetB" ] ;
2019-05-23 16:14:52 +02:00
} ) ;
2019-06-20 09:42:33 +02:00
test ( "can define components in template without t-component" , async ( ) = > {
2019-06-18 09:24:52 +02:00
env . qweb . addTemplates ( `
<templates>
<div t-name="P"><C a="1"/></div>
<span t-name="C"><t t-esc="props.a"/></span>
</templates> ` ) ;
2020-02-05 21:30:14 +01:00
class C extends Component { }
class P extends Component {
2019-09-10 22:37:08 +02:00
static components = { C } ;
2019-06-18 09:24:52 +02:00
}
2019-10-14 15:09:17 +02:00
const parent = new P ( ) ;
2019-06-18 09:24:52 +02:00
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>1</span></div>" ) ;
} ) ;
2019-07-09 14:33:56 +02:00
test ( "display a nice error if it cannot find component" , async ( ) = > {
const consoleError = console . error ;
console . error = jest . fn ( ) ;
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component { }
2019-06-22 14:44:09 +02:00
env . qweb . addTemplate ( "Parent" , ` <div><SomeMispelledWidget /></div> ` ) ;
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-11 11:36:59 +01:00
static components = { SomeWidget : SomeComponent } ;
2019-05-15 17:17:39 +02:00
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-10-26 16:44:12 +02:00
let error ;
try {
await parent . mount ( fixture ) ;
} catch ( e ) {
error = e ;
}
expect ( error ) . toBeDefined ( ) ;
expect ( error . message ) . toBe ( 'Cannot find the definition of component "SomeMispelledWidget"' ) ;
expect ( console . error ) . toBeCalledTimes ( 0 ) ;
2019-07-09 14:33:56 +02:00
console . error = consoleError ;
2019-05-15 17:17:39 +02:00
} ) ;
2019-01-25 15:44:32 +01:00
test ( "modifying a sub widget" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Counter extends Component {
2019-12-11 11:36:59 +01:00
static template = xml `
<div><t t-esc="state.counter"/><button t-on-click="state.counter++">Inc</button></div> ` ;
state = useState ( {
counter : 0
} ) ;
}
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "ParentWidget" , ` <div><t t-component="Counter"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { Counter } ;
2019-01-25 15:44:32 +01:00
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-01-25 15:44:32 +01:00
await widget . mount ( fixture ) ;
2019-06-28 10:32:03 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div><div>0<button>Inc</button></div></div>" ) ;
2019-01-25 15:44:32 +01:00
const button = fixture . getElementsByTagName ( "button" ) [ 0 ] ;
await button . click ( ) ;
2019-04-17 10:07:31 +02:00
await nextTick ( ) ;
2019-06-28 10:32:03 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div><div>1<button>Inc</button></div></div>" ) ;
2019-01-25 15:44:32 +01:00
} ) ;
2019-01-26 08:40:09 +01:00
2019-05-04 22:46:58 +02:00
test ( "refs in a loop" , async ( ) = > {
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate (
"ParentWidget" ,
` <div>
2019-05-04 22:46:58 +02:00
<t t-foreach="state.items" t-as="item">
2019-06-20 09:42:33 +02:00
<t t-component="Child" t-ref="{{item}}" t-key="item"/>
2019-05-04 22:46:58 +02:00
</t>
2019-05-15 11:15:08 +02:00
</div> `
) ;
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-12-11 11:36:59 +01:00
static components = { Child : SomeComponent } ;
2019-09-26 21:56:01 +02:00
elem1 = useRef ( "1" ) ;
elem2 = useRef ( "2" ) ;
elem3 = useRef ( "3" ) ;
elem4 = useRef ( "4" ) ;
state = useState ( { items : [ 1 , 2 , 3 ] } ) ;
2019-05-04 22:46:58 +02:00
}
2019-10-14 15:09:17 +02:00
const parent = new ParentWidget ( ) ;
2019-05-04 22:46:58 +02:00
await parent . mount ( fixture ) ;
2019-09-26 21:56:01 +02:00
expect ( parent . elem1 . comp ) . toBeDefined ( ) ;
expect ( parent . elem2 . comp ) . toBeDefined ( ) ;
expect ( parent . elem3 . comp ) . toBeDefined ( ) ;
expect ( parent . elem4 . comp ) . toBeNull ( ) ;
2019-05-04 22:46:58 +02:00
} ) ;
2020-02-17 13:07:57 +01:00
test ( "t-ref on a node, and t-on-click" , async ( ) = > {
let c ;
let v = false ;
class Child extends Component {
static template = xml ` <div t-ref="nibor">nibor</div> ` ;
nibor = useRef ( "nibor" ) ;
mounted() {
c = this ;
}
}
class Parent extends Component {
static template = xml ` <div><Child t-on-click="doSomething"/></div> ` ;
static components = { Child } ;
doSomething() {
v = true ;
}
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div>nibor</div></div>" ) ;
expect ( c . nibor . el ) . toBeInstanceOf ( HTMLElement ) ;
expect ( v ) . toBe ( false ) ;
c . nibor . el . dispatchEvent ( new Event ( "click" ) ) ;
expect ( v ) . toBe ( true ) ;
expect ( QWeb . TEMPLATES [ Child . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
expect ( QWeb . TEMPLATES [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-01-26 08:40:09 +01:00
test ( "parent's elm for a children === children's elm, even after rerender" , async ( ) = > {
2019-10-14 15:09:17 +02:00
const widget = new WidgetA ( ) ;
2019-01-26 08:40:09 +01:00
await widget . mount ( fixture ) ;
2019-04-13 14:14:34 +02:00
expect ( ( < any > widget . __owl__ . vnode ! . children ! [ 1 ] ) . elm ) . toBe (
( < any > children ( widget ) [ 0 ] . __owl__ . vnode ) . elm
2019-01-26 08:40:09 +01:00
) ;
2019-01-27 13:50:53 +01:00
await children ( widget ) [ 0 ] . render ( ) ;
2019-04-13 14:14:34 +02:00
expect ( ( < any > widget . __owl__ . vnode ! . children ! [ 1 ] ) . elm ) . toBe (
( < any > children ( widget ) [ 0 ] . __owl__ . vnode ) . elm
2019-01-26 08:40:09 +01:00
) ;
} ) ;
2019-01-26 20:33:23 +01:00
2019-06-20 09:42:33 +02:00
test ( "parent env is propagated to child components" , async ( ) = > {
2019-10-14 15:09:17 +02:00
const widget = new WidgetA ( ) ;
2019-01-26 20:33:23 +01:00
await widget . mount ( fixture ) ;
2019-01-27 13:50:53 +01:00
expect ( children ( widget ) [ 0 ] . env ) . toBe ( env ) ;
2019-01-26 20:33:23 +01:00
} ) ;
2019-01-27 22:16:00 +01:00
test ( "rerendering a widget with a sub widget" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Counter extends Component {
2019-12-11 11:36:59 +01:00
static template = xml `
<div><t t-esc="state.counter"/><button t-on-click="state.counter++">Inc</button></div> ` ;
state = useState ( {
counter : 0
} ) ;
}
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "ParentWidget" , ` <div><t t-component="Counter"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { Counter } ;
2019-01-27 22:16:00 +01:00
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-01-27 22:16:00 +01:00
await widget . mount ( fixture ) ;
const button = fixture . getElementsByTagName ( "button" ) [ 0 ] ;
await button . click ( ) ;
2019-04-17 10:07:31 +02:00
await nextTick ( ) ;
2019-06-28 10:32:03 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div><div>1<button>Inc</button></div></div>" ) ;
2019-01-27 22:16:00 +01:00
await widget . render ( ) ;
2019-06-28 10:32:03 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div><div>1<button>Inc</button></div></div>" ) ;
2019-01-27 22:16:00 +01:00
} ) ;
2019-01-28 15:51:20 +01:00
2019-06-20 09:42:33 +02:00
test ( "sub components are destroyed if no longer in dom, then recreated" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Counter extends Component {
2019-12-11 11:36:59 +01:00
static template = xml `
<div><t t-esc="state.counter"/><button t-on-click="state.counter++">Inc</button></div> ` ;
state = useState ( {
counter : 0
} ) ;
}
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "ParentWidget" , ` <div><t t-if="state.ok"><Counter /></t></div> ` ) ;
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-24 14:06:53 +02:00
state = useState ( { ok : true } ) ;
2019-09-10 22:37:08 +02:00
static components = { Counter } ;
2019-01-28 15:51:20 +01:00
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-01-28 15:51:20 +01:00
await widget . mount ( fixture ) ;
const button = fixture . getElementsByTagName ( "button" ) [ 0 ] ;
await button . click ( ) ;
2019-04-17 10:07:31 +02:00
await nextTick ( ) ;
2019-06-28 10:32:03 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div><div>1<button>Inc</button></div></div>" ) ;
2019-04-17 10:07:31 +02:00
widget . state . ok = false ;
await nextTick ( ) ;
2019-01-28 15:51:20 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div></div>" ) ;
2019-04-17 10:07:31 +02:00
widget . state . ok = true ;
await nextTick ( ) ;
2019-06-28 10:32:03 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div><div>0<button>Inc</button></div></div>" ) ;
2019-01-28 15:51:20 +01:00
} ) ;
2019-01-29 15:19:02 +01:00
2019-06-20 09:42:33 +02:00
test ( "sub components rendered in a loop" , async ( ) = > {
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "ChildWidget" , ` <span><t t-esc="props.n"/></span> ` ) ;
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component { }
2019-02-03 09:42:35 +01:00
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate (
"Parent" ,
`
2019-01-29 15:19:02 +01:00
<div>
<t t-foreach="state.numbers" t-as="number">
2019-06-20 09:42:33 +02:00
<t t-component="ChildWidget" t-key="number" n="number"/>
2019-01-29 15:19:02 +01:00
</t>
2019-05-15 11:15:08 +02:00
</div> `
) ;
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-24 14:06:53 +02:00
state = useState ( {
2019-01-29 15:19:02 +01:00
numbers : [ 1 , 2 , 3 ]
2019-09-24 14:06:53 +02:00
} ) ;
2019-09-10 22:37:08 +02:00
static components = { ChildWidget } ;
2019-01-29 15:19:02 +01:00
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-01-29 15:19:02 +01:00
await parent . mount ( fixture ) ;
expect ( normalize ( fixture . innerHTML ) ) . toBe (
normalize ( `
<div>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
` )
) ;
} ) ;
2019-01-30 11:29:54 +01:00
2019-06-20 09:42:33 +02:00
test ( "sub components with some state rendered in a loop" , async ( ) = > {
2019-01-30 11:29:54 +01:00
let n = 1 ;
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "ChildWidget" , ` <span><t t-esc="state.n"/></span> ` ) ;
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-09-24 14:06:53 +02:00
state : any ;
2019-01-30 11:29:54 +01:00
constructor ( parent ) {
super ( parent ) ;
2019-09-24 14:06:53 +02:00
this . state = useState ( { n } ) ;
2019-01-30 11:29:54 +01:00
n ++ ;
}
}
2019-02-02 11:41:19 +01:00
2019-03-13 14:10:15 +01:00
env . qweb . addTemplate (
"parent" ,
` <div>
2019-01-30 11:29:54 +01:00
<t t-foreach="state.numbers" t-as="number">
2019-06-20 09:42:33 +02:00
<t t-component="ChildWidget" t-key="number"/>
2019-01-30 11:29:54 +01:00
</t>
2019-03-13 14:10:15 +01:00
</div> `
) ;
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-12 09:48:17 +02:00
static template = "parent" ;
2019-09-24 14:06:53 +02:00
state = useState ( {
2019-01-30 11:29:54 +01:00
numbers : [ 1 , 2 , 3 ]
2019-09-24 14:06:53 +02:00
} ) ;
2019-09-10 22:37:08 +02:00
static components = { ChildWidget } ;
2019-01-30 11:29:54 +01:00
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-01-30 11:29:54 +01:00
await parent . mount ( fixture ) ;
2019-04-17 10:07:31 +02:00
parent . state . numbers = [ 1 , 3 ] ;
await nextTick ( ) ;
2019-01-30 11:29:54 +01:00
expect ( normalize ( fixture . innerHTML ) ) . toBe (
normalize ( `
<div>
<span>1</span>
<span>3</span>
</div>
` )
) ;
2019-05-02 09:47:33 +02:00
expect ( env . qweb . templates . parent . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-01-30 11:29:54 +01:00
} ) ;
2019-02-15 14:35:46 +01:00
2019-06-20 09:42:33 +02:00
test ( "sub components between t-ifs" , async ( ) = > {
2019-02-15 14:35:46 +01:00
// this confuses the patching algorithm...
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-10-25 15:45:56 +02:00
static template = xml ` <span>child</span> ` ;
}
2019-05-15 11:15:08 +02:00
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-10-25 15:45:56 +02:00
static template = xml `
<div>
<h1 t-if="state.flag">hey</h1>
2019-12-11 11:01:54 +01:00
<h2 t-else="">noo</h2>
2019-10-25 15:45:56 +02:00
<span><ChildWidget/></span>
<t t-if="state.flag"><span>test</span></t>
</div> ` ;
2019-09-10 22:37:08 +02:00
static components = { ChildWidget } ;
2019-10-25 15:45:56 +02:00
state = useState ( { flag : false } ) ;
2019-02-15 14:35:46 +01:00
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-02-15 14:35:46 +01:00
await parent . mount ( fixture ) ;
const child = children ( parent ) [ 0 ] ;
2019-10-25 15:45:56 +02:00
expect ( normalize ( fixture . innerHTML ) ) . toBe (
normalize ( `
<div>
<h2>noo</h2>
<span><span>child</span></span>
</div>
` )
) ;
2019-04-17 10:07:31 +02:00
parent . state . flag = true ;
await nextTick ( ) ;
2019-02-15 14:35:46 +01:00
expect ( children ( parent ) [ 0 ] ) . toBe ( child ) ;
2019-04-13 14:14:34 +02:00
expect ( child . __owl__ . isDestroyed ) . toBe ( false ) ;
2019-02-15 14:35:46 +01:00
expect ( normalize ( fixture . innerHTML ) ) . toBe (
normalize ( `
<div>
<h1>hey</h1>
<span><span>child</span></span>
<span>test</span>
</div>
` )
) ;
} ) ;
2019-05-28 14:30:08 +02:00
2019-09-03 09:32:38 +02:00
test ( "list of sub components inside other nodes" , async ( ) = > {
// this confuses the patching algorithm...
env . qweb . addTemplate ( "ChildWidget" , ` <span>child</span> ` ) ;
env . qweb . addTemplates ( `
<templates>
<div t-name="Parent">
<div t-foreach="state.blips" t-as="blip" t-key="blip.id">
<SubWidget />
</div>
</div>
<span t-name="SubWidget">asdf</span>
</templates> ` ) ;
2020-02-05 21:30:14 +01:00
class SubWidget extends Component { }
class Parent extends Component {
2019-09-10 22:37:08 +02:00
static components = { SubWidget } ;
2019-11-26 22:35:11 +01:00
state = useState ( {
blips : [
{ a : "a" , id : 1 } ,
{ b : "b" , id : 2 } ,
{ c : "c" , id : 4 }
]
} ) ;
2019-09-03 09:32:38 +02:00
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-09-03 09:32:38 +02:00
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe (
"<div><div><span>asdf</span></div><div><span>asdf</span></div><div><span>asdf</span></div></div>"
) ;
parent . state . blips . splice ( 0 , 1 ) ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe (
"<div><div><span>asdf</span></div><div><span>asdf</span></div></div>"
) ;
} ) ;
2019-09-03 16:38:24 +02:00
test ( "list of two sub components inside other nodes" , async ( ) = > {
// this confuses the patching algorithm...
env . qweb . addTemplate ( "ChildWidget" , ` <span>child</span> ` ) ;
env . qweb . addTemplates ( `
<templates>
<div t-name="Parent">
<div t-foreach="state.blips" t-as="blip" t-key="blip.id">
<SubWidget />
<SubWidget />
</div>
</div>
<span t-name="SubWidget">asdf</span>
</templates> ` ) ;
2020-02-05 21:30:14 +01:00
class SubWidget extends Component { }
class Parent extends Component {
2019-09-10 22:37:08 +02:00
static components = { SubWidget } ;
2019-09-24 14:06:53 +02:00
state = useState ( { blips : [ { a : "a" , id : 1 } ] } ) ;
2019-09-03 16:38:24 +02:00
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-09-03 16:38:24 +02:00
await parent . mount ( fixture ) ;
2019-09-04 22:48:37 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div><div><span>asdf</span><span>asdf</span></div></div>" ) ;
2019-09-03 16:38:24 +02:00
} ) ;
2019-06-20 09:42:33 +02:00
test ( "t-component with dynamic value" , async ( ) = > {
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "ParentWidget" , ` <div><t t-component="{{state.widget}}"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { WidgetB } ;
2019-09-24 14:06:53 +02:00
state = useState ( { widget : "WidgetB" } ) ;
2019-05-28 14:30:08 +02:00
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-05-28 14:30:08 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div>world</div></div>" ) ;
expect ( env . qweb . templates . ParentWidget . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-06-20 09:42:33 +02:00
test ( "t-component with dynamic value 2" , async ( ) = > {
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "ParentWidget" , ` <div><t t-component="Widget{{state.widget}}"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { WidgetB } ;
2019-09-24 14:06:53 +02:00
state = useState ( { widget : "B" } ) ;
2019-05-28 14:30:08 +02:00
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-05-28 14:30:08 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div>world</div></div>" ) ;
expect ( env . qweb . templates . ParentWidget . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-10-24 21:20:52 +02:00
2019-11-21 15:17:25 +01:00
test ( "t-component not on a <t> node" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-11-21 15:17:25 +01:00
static template = xml ` <span>1</span> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-11-21 15:17:25 +01:00
static components = { Child } ;
static template = xml ` <div><div t-component="Child"/></div> ` ;
}
const parent = new Parent ( ) ;
let error ;
try {
await parent . mount ( fixture ) ;
} catch ( e ) {
error = e ;
}
expect ( error ) . toBeDefined ( ) ;
2019-11-22 09:40:51 +01:00
expect ( error . message ) . toBe (
` Directive 't-component' can only be used on <t> nodes (used on a <div>) `
) ;
2019-11-21 15:17:25 +01:00
} ) ;
2019-10-24 21:20:52 +02:00
test ( "sub components, loops, and shouldUpdate" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2019-10-24 21:20:52 +02:00
static template = xml ` <span><t t-esc="props.val"/></span> ` ;
shouldUpdate ( nextProps ) {
if ( nextProps . val === 12 ) {
return false ;
}
return true ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-10-24 21:20:52 +02:00
static template = xml `
<div>
<t t-foreach="state.records" t-as="record">
<ChildWidget t-key="record.id" val="record.val"/>
</t>
</div> ` ;
state = useState ( {
2019-11-26 22:35:11 +01:00
records : [
{ id : 1 , val : 1 } ,
{ id : 2 , val : 2 } ,
{ id : 3 , val : 3 }
]
2019-10-24 21:20:52 +02:00
} ) ;
static components = { ChildWidget } ;
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-10-24 21:20:52 +02:00
await parent . mount ( fixture ) ;
expect ( normalize ( fixture . innerHTML ) ) . toBe (
"<div><span>1</span><span>2</span><span>3</span></div>"
) ;
parent . state . records [ 0 ] . val = 11 ;
parent . state . records [ 1 ] . val = 12 ;
parent . state . records [ 2 ] . val = 13 ;
await nextTick ( ) ;
expect ( normalize ( fixture . innerHTML ) ) . toBe (
"<div><span>11</span><span>2</span><span>13</span></div>"
) ;
} ) ;
2019-11-21 11:23:05 +01:00
test ( "three level of components with collapsing root nodes" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class GrandChild extends Component {
2019-11-21 11:23:05 +01:00
static template = xml ` <div>2</div> ` ;
}
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-11-21 11:23:05 +01:00
static components = { GrandChild } ;
static template = xml ` <GrandChild/> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-11-21 11:23:05 +01:00
static components = { Child } ;
static template = xml ` <Child></Child> ` ;
}
const app = new Parent ( ) ;
await app . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>2</div>" ) ;
} ) ;
2019-01-20 14:46:36 +01:00
} ) ;
2019-01-28 12:03:59 +01:00
2019-06-03 10:10:41 +02:00
describe ( "props evaluation " , ( ) = > {
2019-01-28 12:03:59 +01:00
test ( "explicit object prop" , async ( ) = > {
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "Parent" , ` <div><t t-component="child" value="state.val"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-01-28 12:03:59 +01:00
state : { someval : number } ;
constructor ( parent : Parent , props : { value : number } ) {
super ( parent ) ;
2019-09-24 14:06:53 +02:00
this . state = useState ( { someval : props.value } ) ;
2019-01-28 12:03:59 +01:00
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-09-24 14:06:53 +02:00
state = useState ( { val : 42 } ) ;
2019-09-10 22:37:08 +02:00
}
env . qweb . addTemplate ( "Child" , ` <span><t t-esc="state.someval"/></span> ` ) ;
2019-01-28 12:03:59 +01:00
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-01-28 12:03:59 +01:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>42</span></div>" ) ;
} ) ;
2019-03-14 15:53:31 +01:00
test ( "accept ES6-like syntax for props (with getters)" , async ( ) = > {
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "Child" , ` <span><t t-esc="props.greetings"/></span> ` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component { }
2019-03-14 15:53:31 +01:00
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "Parent" , ` <div><t t-component="child" greetings="greetings"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-03-14 15:53:31 +01:00
get greetings() {
2019-10-30 14:57:24 +01:00
const name = "aaron" ;
return ` hello ${ name } ` ;
2019-03-14 15:53:31 +01:00
}
}
2019-10-30 14:57:24 +01:00
const widget = new Parent ( ) ;
2019-03-14 15:53:31 +01:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>hello aaron</span></div>" ) ;
} ) ;
2019-03-15 10:11:50 +01:00
2019-06-03 10:10:41 +02:00
test ( "t-set works " , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component { }
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate (
"Parent" ,
`
2019-03-15 10:11:50 +01:00
<div>
<t t-set="val" t-value="42"/>
2019-06-20 09:42:33 +02:00
<t t-component="child" val="val"/>
2019-05-15 11:15:08 +02:00
</div> `
) ;
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-03-15 10:11:50 +01:00
}
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate (
"Child" ,
`
2019-03-15 10:11:50 +01:00
<span>
<t t-esc="props.val"/>
2019-05-15 11:15:08 +02:00
</span> `
) ;
2019-03-15 10:11:50 +01:00
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-03-15 10:11:50 +01:00
await widget . mount ( fixture ) ;
expect ( normalize ( fixture . innerHTML ) ) . toBe ( "<div><span>42</span></div>" ) ;
} ) ;
2019-01-28 12:03:59 +01:00
} ) ;
2019-01-30 13:33:37 +01:00
2019-06-20 09:42:33 +02:00
describe ( "other directives with t-component" , ( ) = > {
2019-02-02 18:17:21 +01:00
test ( "t-on works as expected" , async ( ) = > {
2019-05-31 14:03:34 +02:00
expect . assertions ( 4 ) ;
env . qweb . addTemplates ( `
<templates>
2019-06-20 09:42:33 +02:00
<div t-name="ParentWidget"><t t-component="child" t-on-custom-event="someMethod"/></div>
2019-05-31 14:03:34 +02:00
</templates>
` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-05-31 14:03:34 +02:00
n = 0 ;
someMethod ( ev ) {
expect ( ev . detail ) . toBe ( 43 ) ;
this . n ++ ;
2019-02-02 18:17:21 +01:00
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-02-02 18:17:21 +01:00
await widget . mount ( fixture ) ;
let child = children ( widget ) [ 0 ] ;
2019-05-31 14:03:34 +02:00
expect ( widget . n ) . toBe ( 0 ) ;
child . trigger ( "custom-event" , 43 ) ;
expect ( widget . n ) . toBe ( 1 ) ;
2019-02-02 18:17:21 +01:00
child . destroy ( ) ;
2019-05-31 14:03:34 +02:00
child . trigger ( "custom-event" , 43 ) ;
expect ( widget . n ) . toBe ( 1 ) ;
2019-02-02 18:17:21 +01:00
} ) ;
2019-03-04 09:29:27 +01:00
2019-12-02 15:47:49 +01:00
test ( "t-on works, even if flush is called many times" , async ( ) = > {
let flag = false ;
let mounted = false ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-02 15:47:49 +01:00
static template = xml ` <span>child</span> ` ;
mounted() {
mounted = true ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-02 15:47:49 +01:00
static template = xml ` <div><Child t-on-click="doSomething"/></div> ` ;
static components = { Child } ;
doSomething() {
flag = true ;
}
}
const parent = new Parent ( ) ;
parent . mount ( fixture ) ;
while ( ! mounted ) {
await nextMicroTick ( ) ;
Component . scheduler . flush ( ) ;
}
expect ( fixture . innerHTML ) . toBe ( "<div><span>child</span></div>" ) ;
expect ( flag ) . toBe ( false ) ;
fixture . querySelector ( "span" ) ! . click ( ) ;
expect ( flag ) . toBe ( true ) ;
} ) ;
2019-12-12 13:17:51 +01:00
test ( "t-on with inline statement" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-12 13:17:51 +01:00
static template = xml ` <span>child</span> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-12 13:17:51 +01:00
static template = xml ` <div><Child t-on-click="state.n = state.n + 1"/></div> ` ;
static components = { Child } ;
2019-12-12 11:21:49 +01:00
state = { n : 3 } ;
2019-12-12 13:17:51 +01:00
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
expect ( parent . state . n ) . toBe ( 3 ) ;
fixture . querySelector ( "span" ) ! . click ( ) ;
expect ( parent . state . n ) . toBe ( 4 ) ;
} ) ;
2019-06-03 15:12:21 +02:00
test ( "t-on with handler bound to argument" , async ( ) = > {
expect . assertions ( 3 ) ;
env . qweb . addTemplates ( `
<templates>
2019-06-20 09:42:33 +02:00
<div t-name="ParentWidget"><t t-component="child" t-on-ev="onEv(3)"/></div>
2019-06-03 15:12:21 +02:00
</templates>
` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-06-03 15:12:21 +02:00
onEv ( n , ev ) {
expect ( n ) . toBe ( 3 ) ;
expect ( ev . detail ) . toBe ( 43 ) ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-06-03 15:12:21 +02:00
await widget . mount ( fixture ) ;
let child = children ( widget ) [ 0 ] ;
child . trigger ( "ev" , 43 ) ;
expect ( env . qweb . templates . ParentWidget . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-on with handler bound to object" , async ( ) = > {
expect . assertions ( 3 ) ;
env . qweb . addTemplates ( `
<templates>
2019-06-20 09:42:33 +02:00
<div t-name="ParentWidget"><t t-component="child" t-on-ev="onEv({val: 3})"/></div>
2019-06-03 15:12:21 +02:00
</templates>
` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-06-03 15:12:21 +02:00
onEv ( o , ev ) {
expect ( o ) . toEqual ( { val : 3 } ) ;
expect ( ev . detail ) . toBe ( 43 ) ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-06-03 15:12:21 +02:00
await widget . mount ( fixture ) ;
let child = children ( widget ) [ 0 ] ;
child . trigger ( "ev" , 43 ) ;
expect ( env . qweb . templates . ParentWidget . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-on with handler bound to empty object" , async ( ) = > {
expect . assertions ( 3 ) ;
env . qweb . addTemplates ( `
<templates>
2019-06-20 09:42:33 +02:00
<div t-name="ParentWidget"><t t-component="child" t-on-ev="onEv({})"/></div>
2019-06-03 15:12:21 +02:00
</templates>
` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-06-03 15:12:21 +02:00
onEv ( o , ev ) {
expect ( o ) . toEqual ( { } ) ;
expect ( ev . detail ) . toBe ( 43 ) ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-06-03 15:12:21 +02:00
await widget . mount ( fixture ) ;
let child = children ( widget ) [ 0 ] ;
child . trigger ( "ev" , 43 ) ;
expect ( env . qweb . templates . ParentWidget . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-on with handler bound to empty object (with non empty inner string)" , async ( ) = > {
expect . assertions ( 3 ) ;
env . qweb . addTemplates ( `
<templates>
2019-06-20 09:42:33 +02:00
<div t-name="ParentWidget"><t t-component="child" t-on-ev="onEv({ })"/></div>
2019-06-03 15:12:21 +02:00
</templates>
` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-06-03 15:12:21 +02:00
onEv ( o , ev ) {
expect ( o ) . toEqual ( { } ) ;
expect ( ev . detail ) . toBe ( 43 ) ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-06-03 15:12:21 +02:00
await widget . mount ( fixture ) ;
let child = children ( widget ) [ 0 ] ;
child . trigger ( "ev" , 43 ) ;
expect ( env . qweb . templates . ParentWidget . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-06-03 14:28:10 +02:00
test ( "t-on with stop and/or prevent modifiers" , async ( ) = > {
expect . assertions ( 7 ) ;
env . qweb . addTemplates ( `
<templates>
<div t-name="ParentWidget">
2019-06-20 09:42:33 +02:00
<t t-component="child"
2019-06-03 14:28:10 +02:00
t-on-ev-1.stop="onEv1"
t-on-ev-2.prevent="onEv2"
t-on-ev-3.stop.prevent="onEv3"/>
</div>
</templates>
` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-06-03 14:28:10 +02:00
onEv1 ( ev ) {
expect ( ev . defaultPrevented ) . toBe ( false ) ;
expect ( ev . cancelBubble ) . toBe ( true ) ;
}
onEv2 ( ev ) {
expect ( ev . defaultPrevented ) . toBe ( true ) ;
expect ( ev . cancelBubble ) . toBe ( false ) ;
}
onEv3 ( ev ) {
expect ( ev . defaultPrevented ) . toBe ( true ) ;
expect ( ev . cancelBubble ) . toBe ( true ) ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-06-03 14:28:10 +02:00
await widget . mount ( fixture ) ;
const child = children ( widget ) [ 0 ] ;
child . trigger ( "ev-1" ) ;
child . trigger ( "ev-2" ) ;
child . trigger ( "ev-3" ) ;
expect ( env . qweb . templates . ParentWidget . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-on with self modifier" , async ( ) = > {
env . qweb . addTemplates ( `
<templates>
<div t-name="ParentWidget">
2019-06-20 09:42:33 +02:00
<t t-component="child" t-on-ev-1="onEv1" t-on-ev-2.self="onEv2"/>
2019-06-03 14:28:10 +02:00
</div>
2019-06-20 09:42:33 +02:00
<div t-name="Child"><t t-component="child"/></div>
2019-06-03 14:28:10 +02:00
</templates>
` ) ;
const steps : string [ ] = [ ] ;
2020-02-05 21:30:14 +01:00
class GrandChild extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : GrandChild } ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-06-03 14:28:10 +02:00
onEv1 ( ev ) {
steps . push ( "onEv1" ) ;
}
onEv2 ( ev ) {
steps . push ( "onEv2" ) ;
}
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-06-03 14:28:10 +02:00
await widget . mount ( fixture ) ;
const child = children ( widget ) [ 0 ] ;
const grandChild = children ( child ) [ 0 ] ;
child . trigger ( "ev-1" ) ;
child . trigger ( "ev-2" ) ;
expect ( steps ) . toEqual ( [ "onEv1" , "onEv2" ] ) ;
grandChild . trigger ( "ev-1" ) ;
grandChild . trigger ( "ev-2" ) ;
expect ( steps ) . toEqual ( [ "onEv1" , "onEv2" , "onEv1" ] ) ;
expect ( env . qweb . templates . ParentWidget . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-on with self and prevent modifiers (order matters)" , async ( ) = > {
env . qweb . addTemplates ( `
<templates>
<div t-name="ParentWidget">
2019-06-20 09:42:33 +02:00
<t t-component="child" t-on-ev.self.prevent="onEv"/>
2019-06-03 14:28:10 +02:00
</div>
2019-06-20 09:42:33 +02:00
<div t-name="Child"><t t-component="child"/></div>
2019-06-03 14:28:10 +02:00
</templates>
` ) ;
const steps : boolean [ ] = [ ] ;
2020-02-05 21:30:14 +01:00
class GrandChild extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : GrandChild } ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-06-03 14:28:10 +02:00
onEv() { }
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-06-03 14:28:10 +02:00
await widget . mount ( fixture ) ;
( < HTMLElement > fixture ) . addEventListener ( "ev" , function ( e ) {
steps . push ( e . defaultPrevented ) ;
} ) ;
const child = children ( widget ) [ 0 ] ;
const grandChild = children ( child ) [ 0 ] ;
child . trigger ( "ev" ) ;
grandChild . trigger ( "ev" ) ;
expect ( steps ) . toEqual ( [ true , false ] ) ;
expect ( env . qweb . templates . ParentWidget . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-on with prevent and self modifiers (order matters)" , async ( ) = > {
env . qweb . addTemplates ( `
<templates>
<div t-name="ParentWidget">
2019-06-18 09:24:52 +02:00
<Child t-on-ev.prevent.self="onEv"/>
2019-06-03 14:28:10 +02:00
</div>
2019-06-18 09:24:52 +02:00
<div t-name="Child"><GrandChild/></div>
2019-06-03 14:28:10 +02:00
</templates>
` ) ;
const steps : boolean [ ] = [ ] ;
2020-02-05 21:30:14 +01:00
class GrandChild extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-09-10 22:37:08 +02:00
static components = { GrandChild } ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { Child } ;
2019-06-03 14:28:10 +02:00
onEv() { }
}
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-06-03 14:28:10 +02:00
await widget . mount ( fixture ) ;
( < HTMLElement > fixture ) . addEventListener ( "ev" , function ( e ) {
steps . push ( e . defaultPrevented ) ;
} ) ;
const child = children ( widget ) [ 0 ] ;
const grandChild = children ( child ) [ 0 ] ;
child . trigger ( "ev" ) ;
grandChild . trigger ( "ev" ) ;
expect ( steps ) . toEqual ( [ true , true ] ) ;
expect ( env . qweb . templates . ParentWidget . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-10-25 10:57:08 +02:00
test ( "t-on with getter as handler" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-10-25 10:57:08 +02:00
static template = xml ` <span></span> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-10-25 10:57:08 +02:00
static template = xml `
<div>
<t t-esc="state.counter"/>
<Child t-on-ev="handler"/>
</div> ` ;
static components = { Child } ;
2019-10-26 08:55:38 +02:00
state = useState ( { counter : 0 } ) ;
2019-10-25 10:57:08 +02:00
get handler() {
this . state . counter ++ ;
return ( ) = > { } ;
}
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-10-25 10:57:08 +02:00
await parent . mount ( fixture ) ;
expect ( env . qweb . templates [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-10-26 08:55:38 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div>0<span></span></div>" ) ;
2019-10-25 10:57:08 +02:00
let child = children ( parent ) [ 0 ] ;
child . trigger ( "ev" ) ;
await nextTick ( ) ;
2019-10-26 08:55:38 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div>1<span></span></div>" ) ;
2019-10-25 10:57:08 +02:00
} ) ;
test ( "t-on with inline statement" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-10-25 10:57:08 +02:00
static template = xml ` <span></span> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-10-25 10:57:08 +02:00
static template = xml `
<div>
<t t-esc="state.counter"/>
<Child t-on-ev="state.counter++"/>
</div> ` ;
static components = { Child } ;
2019-10-26 08:55:38 +02:00
state = useState ( { counter : 0 } ) ;
2019-10-25 10:57:08 +02:00
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-10-25 10:57:08 +02:00
await parent . mount ( fixture ) ;
expect ( env . qweb . templates [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-10-26 08:55:38 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div>0<span></span></div>" ) ;
2019-10-25 10:57:08 +02:00
let child = children ( parent ) [ 0 ] ;
child . trigger ( "ev" ) ;
await nextTick ( ) ;
2019-10-26 08:55:38 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div>1<span></span></div>" ) ;
2019-10-25 10:57:08 +02:00
} ) ;
2019-10-25 10:58:18 +02:00
test ( "t-on with no handler (only modifiers)" , async ( ) = > {
expect . assertions ( 2 ) ;
2020-02-05 21:30:14 +01:00
class ComponentB extends Component {
2019-10-25 10:58:18 +02:00
static template = xml ` <span></span> ` ;
}
2020-02-05 21:30:14 +01:00
class ComponentA extends Component {
2019-10-25 10:58:18 +02:00
static template = xml ` <p><ComponentB t-on-ev.prevent=""/></p> ` ;
static components = { ComponentB } ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-10-25 10:58:18 +02:00
static template = xml ` <div><ComponentA t-on-ev="onEv"/></div> ` ;
static components = { ComponentA } ;
onEv ( ev ) {
expect ( ev . defaultPrevented ) . toBe ( true ) ;
}
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-10-25 10:58:18 +02:00
await parent . mount ( fixture ) ;
let componentB = children ( children ( parent ) [ 0 ] ) [ 0 ] ;
componentB . trigger ( "ev" ) ;
expect ( env . qweb . templates [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-11-21 11:23:05 +01:00
test ( "t-on on nested components with collapsing root nodes" , async ( ) = > {
const steps : string [ ] = [ ] ;
let grandChild ;
2020-02-05 21:30:14 +01:00
class GrandChild extends Component {
2019-11-21 11:23:05 +01:00
static template = xml ` <span t-on-ev="_onEv"/> ` ;
constructor ( ) {
super ( . . . arguments ) ;
grandChild = this ;
}
_onEv() {
2019-11-30 21:36:41 +01:00
steps . push ( "GrandChild" ) ;
2019-11-21 11:23:05 +01:00
}
}
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-11-21 11:23:05 +01:00
static template = xml ` <GrandChild t-on-ev="_onEv"/> ` ;
static components = { GrandChild } ;
_onEv() {
2019-11-30 21:36:41 +01:00
steps . push ( "Child" ) ;
2019-11-21 11:23:05 +01:00
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-11-21 11:23:05 +01:00
static template = xml ` <Child t-on-ev="_onEv"/> ` ;
static components = { Child } ;
_onEv() {
2019-11-30 21:36:41 +01:00
steps . push ( "Parent" ) ;
2019-11-21 11:23:05 +01:00
}
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
grandChild . trigger ( "ev" ) ;
2019-11-30 21:36:41 +01:00
expect ( steps ) . toEqual ( [ "GrandChild" , "Child" , "Parent" ] ) ;
2019-11-21 11:23:05 +01:00
} ) ;
2019-12-04 21:29:34 +01:00
test ( "t-on on unmounted components" , async ( ) = > {
const steps : string [ ] = [ ] ;
let child ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-04 21:29:34 +01:00
static template = xml ` <div t-on-click="onClick"/> ` ;
mounted() {
child = this ;
}
onClick() {
steps . push ( "click" ) ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-04 21:29:34 +01:00
static template = xml ` <div><Child/></div> ` ;
static components = { Child } ;
state = useState ( { flag : true } ) ;
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
let el = child . el ;
el . click ( ) ;
expect ( steps ) . toEqual ( [ "click" ] ) ;
parent . unmount ( ) ;
expect ( child . __owl__ . isMounted ) . toBe ( false ) ;
el . click ( ) ;
expect ( steps ) . toEqual ( [ "click" ] ) ;
} ) ;
2020-02-20 11:27:47 +01:00
test ( "t-on with .capture modifier" , async ( ) = > {
const steps : string [ ] = [ ] ;
class Child extends Component {
static template = xml ` <div><span t-on-click="click">hey</span></div> ` ;
click() {
steps . push ( "click" ) ;
}
}
class ParentWidget extends Component {
static components = { Child } ;
static template = xml ` <div><Child t-on-click.capture="capture"/></div> ` ;
capture() {
steps . push ( "captured" ) ;
}
}
const widget = new ParentWidget ( ) ;
await widget . mount ( fixture ) ;
fixture . querySelector ( "span" ) ! . click ( ) ;
expect ( env . qweb . templates [ ParentWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
expect ( steps ) . toEqual ( [ "captured" , "click" ] ) ;
} ) ;
2019-12-04 21:29:34 +01:00
test ( "t-on on destroyed components" , async ( ) = > {
const steps : string [ ] = [ ] ;
let child ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-04 21:29:34 +01:00
static template = xml ` <div t-on-click="onClick"/> ` ;
mounted() {
child = this ;
}
onClick() {
steps . push ( "click" ) ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-04 21:29:34 +01:00
static template = xml ` <div><Child t-if="state.flag"/></div> ` ;
static components = { Child } ;
state = useState ( { flag : true } ) ;
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
let el = child . el ;
el . click ( ) ;
expect ( steps ) . toEqual ( [ "click" ] ) ;
parent . state . flag = false ;
await nextTick ( ) ;
expect ( child . __owl__ . isDestroyed ) . toBe ( true ) ;
el . click ( ) ;
expect ( steps ) . toEqual ( [ "click" ] ) ;
} ) ;
test ( "t-on on destroyed components, part 2" , async ( ) = > {
const steps : string [ ] = [ ] ;
let child ;
2020-02-05 21:30:14 +01:00
class GrandChild extends Component {
2019-12-04 21:29:34 +01:00
static template = xml ` <div>grandchild</div> ` ;
}
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-04 21:29:34 +01:00
static template = xml ` <div><GrandChild t-ref="gc" t-on-click="onClick"/></div> ` ;
static components = { GrandChild } ;
gc = useRef ( "gc" ) ;
mounted() {
child = this ;
}
onClick() {
steps . push ( "click" ) ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-04 21:29:34 +01:00
static template = xml ` <div><Child t-if="state.flag"/></div> ` ;
static components = { Child } ;
state = useState ( { flag : true } ) ;
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
let el = child . gc . el ;
el . click ( ) ;
expect ( steps ) . toEqual ( [ "click" ] ) ;
parent . state . flag = false ;
await nextTick ( ) ;
expect ( child . __owl__ . isDestroyed ) . toBe ( true ) ;
el . click ( ) ;
expect ( steps ) . toEqual ( [ "click" ] ) ;
} ) ;
2019-06-20 09:42:33 +02:00
test ( "t-if works with t-component" , async ( ) = > {
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "ParentWidget" , ` <div><t t-component="child" t-if="state.flag"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component { }
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : true } ) ;
2019-03-04 09:29:27 +01:00
}
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "Child" , "<span>hey</span>" ) ;
2019-03-04 09:29:27 +01:00
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-03-04 09:29:27 +01:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>hey</span></div>" ) ;
2019-04-17 10:07:31 +02:00
widget . state . flag = false ;
await nextTick ( ) ;
2019-03-04 09:29:27 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div></div>" ) ;
2019-04-17 10:07:31 +02:00
widget . state . flag = true ;
await nextTick ( ) ;
2019-03-04 09:29:27 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div><span>hey</span></div>" ) ;
} ) ;
2019-03-13 14:29:57 +01:00
2019-06-20 09:42:33 +02:00
test ( "t-else works with t-component" , async ( ) = > {
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate (
"ParentWidget" ,
`
2019-03-13 14:29:57 +01:00
<div>
<div t-if="state.flag">somediv</div>
2019-12-11 11:01:54 +01:00
<t t-else="" t-component="child"/>
2019-05-15 11:15:08 +02:00
</div> `
) ;
2020-02-05 21:30:14 +01:00
class Child extends Component { }
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : true } ) ;
2019-03-13 14:29:57 +01:00
}
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "Child" , "<span>hey</span>" ) ;
2019-03-13 14:29:57 +01:00
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-03-13 14:29:57 +01:00
await widget . mount ( fixture ) ;
expect ( normalize ( fixture . innerHTML ) ) . toBe ( "<div><div>somediv</div></div>" ) ;
2019-04-17 10:07:31 +02:00
widget . state . flag = false ;
await nextTick ( ) ;
2019-03-13 14:29:57 +01:00
expect ( normalize ( fixture . innerHTML ) ) . toBe ( "<div><span>hey</span></div>" ) ;
} ) ;
2019-04-16 14:17:02 +02:00
2019-06-20 09:42:33 +02:00
test ( "t-elif works with t-component" , async ( ) = > {
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate (
"ParentWidget" ,
`
2019-05-10 19:35:58 +02:00
<div>
<div t-if="state.flag">somediv</div>
2019-06-20 09:42:33 +02:00
<t t-elif="!state.flag" t-component="child"/>
2019-05-15 11:15:08 +02:00
</div> `
) ;
2020-02-05 21:30:14 +01:00
class Child extends Component { }
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : true } ) ;
2019-05-10 19:35:58 +02:00
}
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "Child" , "<span>hey</span>" ) ;
2019-05-10 19:35:58 +02:00
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-05-10 19:35:58 +02:00
await widget . mount ( fixture ) ;
expect ( normalize ( fixture . innerHTML ) ) . toBe ( "<div><div>somediv</div></div>" ) ;
widget . state . flag = false ;
await nextTick ( ) ;
expect ( normalize ( fixture . innerHTML ) ) . toBe ( "<div><span>hey</span></div>" ) ;
} ) ;
2019-06-20 09:42:33 +02:00
test ( "t-else with empty string works with t-component" , async ( ) = > {
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate (
"ParentWidget" ,
`
2019-04-16 14:17:02 +02:00
<div>
<div t-if="state.flag">somediv</div>
2019-06-20 09:42:33 +02:00
<t t-else="" t-component="child"/>
2019-05-15 11:15:08 +02:00
</div> `
) ;
2020-02-05 21:30:14 +01:00
class Child extends Component { }
class ParentWidget extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : true } ) ;
2019-04-16 14:17:02 +02:00
}
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "Child" , "<span>hey</span>" ) ;
2019-04-16 14:17:02 +02:00
2019-10-14 15:09:17 +02:00
const widget = new ParentWidget ( ) ;
2019-04-16 14:17:02 +02:00
await widget . mount ( fixture ) ;
expect ( normalize ( fixture . innerHTML ) ) . toBe ( "<div><div>somediv</div></div>" ) ;
2019-04-17 10:07:31 +02:00
widget . state . flag = false ;
await nextTick ( ) ;
2019-04-16 14:17:02 +02:00
expect ( normalize ( fixture . innerHTML ) ) . toBe ( "<div><span>hey</span></div>" ) ;
} ) ;
2019-11-28 12:11:57 +01:00
test ( "t-foreach with t-component, and update" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-11-28 12:11:57 +01:00
static template = xml `
<span>
<t t-esc="state.val"/>
<t t-esc="props.val"/>
</span> ` ;
2019-11-21 11:23:05 +01:00
state = useState ( { val : "A" } ) ;
2019-11-28 12:11:57 +01:00
mounted() {
2019-11-21 11:23:05 +01:00
this . state . val = "B" ;
2019-11-28 12:11:57 +01:00
}
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-11-28 12:11:57 +01:00
static components = { Child } ;
static template = xml `
<div>
<t t-foreach="Array(2)" t-as="n" t-key="n_index">
<Child val="n_index"/>
</t>
</div> ` ;
}
const widget = new ParentWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>A0</span><span>A1</span></div>" ) ;
await nextTick ( ) ; // wait for changes triggered in mounted to be applied
expect ( fixture . innerHTML ) . toBe ( "<div><span>B0</span><span>B1</span></div>" ) ;
} ) ;
2020-01-02 14:01:45 +01:00
test ( "t-set outside modified in t-foreach" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-02 14:01:45 +01:00
static template = xml `
<div>
<t t-set="iter" t-value="0"/>
<t t-foreach="state.values" t-as="val" t-key="val">
<p>InLoop: <t t-esc="iter"/></p>
<t t-set="iter" t-value="iter + 1"/>
</t>
<p>EndLoop: <t t-esc="iter"/></p>
</div> ` ;
2020-01-09 10:02:27 +01:00
state = useState ( { values : [ "a" , "b" ] } ) ;
2020-01-02 14:01:45 +01:00
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>InLoop: 0</p><p>InLoop: 1</p><p>EndLoop: 2</p></div>" ) ;
expect ( QWeb . TEMPLATES [ SomeWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-set outside modified in t-if" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-02 14:01:45 +01:00
static template = xml `
<div>
<t t-set="iter" t-value="0"/>
<t t-set="flag" t-value="state.flag" />
<t t-if="flag === 'if'">
<t t-set="iter" t-value="2"/>
</t>
<t t-elif="flag === 'elif'">
<t t-set="iter" t-value="3"/>
</t>
<t t-else="">
<t t-set="iter" t-value="4"/>
</t>
<p><t t-esc="iter"/></p>
</div> ` ;
2020-01-09 10:02:27 +01:00
state = { flag : "if" } ;
2020-01-02 14:01:45 +01:00
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>2</p></div>" ) ;
2020-01-09 10:02:27 +01:00
widget . state . flag = "elif" ;
2020-01-02 14:01:45 +01:00
await widget . render ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>3</p></div>" ) ;
2020-01-09 10:02:27 +01:00
widget . state . flag = "false" ;
2020-01-02 14:01:45 +01:00
await widget . render ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>4</p></div>" ) ;
} ) ;
test ( "t-set in t-if" , async ( ) = > {
// Weird that code block within 'if' leaks outside of it
2020-01-09 10:02:27 +01:00
// Python does the same
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-02 14:01:45 +01:00
static template = xml `
<div>
<t t-set="flag" t-value="state.flag" />
<t t-if="flag === 'if'">
<t t-set="iter" t-value="2"/>
</t>
<t t-elif="flag === 'elif'">
<t t-set="iter" t-value="3"/>
</t>
<t t-else="">
<t t-set="iter" t-value="4"/>
</t>
<p><t t-esc="iter"/></p>
</div> ` ;
2020-01-09 10:02:27 +01:00
state = { flag : "if" } ;
2020-01-02 14:01:45 +01:00
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>2</p></div>" ) ;
2020-01-09 10:02:27 +01:00
widget . state . flag = "elif" ;
2020-01-02 14:01:45 +01:00
await widget . render ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>3</p></div>" ) ;
2020-01-09 10:02:27 +01:00
widget . state . flag = "false" ;
2020-01-02 14:01:45 +01:00
await widget . render ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>4</p></div>" ) ;
} ) ;
test ( "t-set can't alter component" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-02 14:01:45 +01:00
static template = xml `
<div>
<p><t t-esc="iter"/></p>
<t t-set="iter" t-value="5"/>
<p><t t-esc="iter"/></p>
</div> ` ;
iter = 1 ;
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>1</p><p>5</p></div>" ) ;
expect ( widget . iter ) . toBe ( 1 ) ;
expect ( QWeb . TEMPLATES [ SomeWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-set can't alter from within callee" , async ( ) = > {
2020-01-09 10:02:27 +01:00
env . qweb . addTemplate (
"ChildWidget" ,
` <div><t t-esc="iter"/><t t-set="iter" t-value="'called'"/><t t-esc="iter"/></div> `
) ;
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-02 14:01:45 +01:00
static template = xml `
<div>
<t t-set="iter" t-value="'source'"/>
<p><t t-esc="iter"/></p>
<t t-call="ChildWidget"/>
<p><t t-esc="iter"/></p>
</div> ` ;
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>source</p><div>sourcecalled</div><p>source</p></div>" ) ;
expect ( QWeb . TEMPLATES [ SomeWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-set can't alter in t-call body" , async ( ) = > {
2020-01-09 10:02:27 +01:00
env . qweb . addTemplate (
"ChildWidget" ,
` <div><t t-esc="iter"/><t t-set="iter" t-value="'called'"/><t t-esc="iter"/></div> `
) ;
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-02 14:01:45 +01:00
static template = xml `
<div>
<t t-set="iter" t-value="'source'"/>
<p><t t-esc="iter"/></p>
<t t-call="ChildWidget">
<t t-set="iter" t-value="'inCall'"/>
</t>
<p><t t-esc="iter"/></p>
</div> ` ;
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>source</p><div>inCallcalled</div><p>source</p></div>" ) ;
expect ( QWeb . TEMPLATES [ SomeWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "slot setted value (with t-set) not accessible with t-esc" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2020-01-02 14:01:45 +01:00
static template = xml ` <div><t t-esc="iter"/><t t-set="iter" t-value="'called'"/><t t-esc="iter"/></div> ` ;
}
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-02 14:01:45 +01:00
static components = { ChildWidget } ;
static template = xml `
<div>
<t t-set="iter" t-value="'source'"/>
<p><t t-esc="iter"/></p>
<ChildWidget>
<t t-set="iter" t-value="'inCall'"/>
</ChildWidget>
<p><t t-esc="iter"/></p>
</div> ` ;
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>source</p><div>called</div><p>source</p></div>" ) ;
expect ( QWeb . TEMPLATES [ SomeWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-set not altered by child widget" , async ( ) = > {
let child ;
2020-02-05 21:30:14 +01:00
class ChildWidget extends Component {
2020-01-02 14:01:45 +01:00
static template = xml ` <div><t t-esc="iter"/><t t-set="iter" t-value="'called'"/><t t-esc="iter"/></div> ` ;
2020-01-09 10:02:27 +01:00
iter = "child" ;
2020-01-02 14:01:45 +01:00
constructor ( ) {
super ( . . . arguments ) ;
child = this ;
}
}
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-02 14:01:45 +01:00
static components = { ChildWidget } ;
static template = xml `
<div>
<t t-set="iter" t-value="'source'"/>
<p><t t-esc="iter"/></p>
<ChildWidget/>
<p><t t-esc="iter"/></p>
</div> ` ;
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>source</p><div>childcalled</div><p>source</p></div>" ) ;
2020-01-09 10:02:27 +01:00
expect ( child . iter ) . toBe ( "child" ) ;
2020-01-02 14:01:45 +01:00
expect ( QWeb . TEMPLATES [ SomeWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-set outside modified in t-foreach increment-after operator" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-02 14:01:45 +01:00
static template = xml `
<div>
<t t-set="iter" t-value="0"/>
<t t-foreach="state.values" t-as="val" t-key="val">
<p>InLoop: <t t-esc="iter"/></p>
<t t-set="iter" t-value="iter++"/>
</t>
<p>EndLoop: <t t-esc="iter"/></p>
</div> ` ;
2020-01-09 10:02:27 +01:00
state = useState ( { values : [ "a" , "b" ] } ) ;
2020-01-02 14:01:45 +01:00
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>InLoop: 0</p><p>InLoop: 1</p><p>EndLoop: 0</p></div>" ) ;
} ) ;
test ( "t-set outside modified in t-foreach increment-before operator" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-02 14:01:45 +01:00
static template = xml `
<div>
<t t-set="iter" t-value="0"/>
<t t-foreach="state.values" t-as="val" t-key="val">
<p>InLoop: <t t-esc="iter"/></p>
<t t-set="iter" t-value="++iter"/>
</t>
<p>EndLoop: <t t-esc="iter"/></p>
</div> ` ;
2020-01-09 10:02:27 +01:00
state = useState ( { values : [ "a" , "b" ] } ) ;
2020-01-02 14:01:45 +01:00
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>InLoop: 0</p><p>InLoop: 1</p><p>EndLoop: 1</p></div>" ) ;
} ) ;
2020-01-06 17:01:43 +01:00
test ( "t-on expression in t-foreach" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-06 17:01:43 +01:00
static template = xml `
<div>
<div t-foreach="state.values" t-as="val" t-key="val">
<t t-esc="val_index"/>: <t t-esc="val + ''"/>
<button t-on-click="otherState.vals.push(val)">Expr</button>
</div>
</div> ` ;
2020-01-09 10:02:27 +01:00
state = useState ( { values : [ "a" , "b" ] } ) ;
otherState = { vals : [ ] } ;
2020-01-06 17:01:43 +01:00
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
2020-01-09 10:02:27 +01:00
expect ( fixture . innerHTML ) . toBe (
"<div><div>0: a<button>Expr</button></div><div>1: b<button>Expr</button></div></div>"
) ;
2020-01-06 17:01:43 +01:00
expect ( widget . otherState . vals ) . toStrictEqual ( [ ] ) ;
2020-01-09 10:02:27 +01:00
const buttons = fixture . querySelectorAll ( "button" ) ;
2020-01-06 17:01:43 +01:00
buttons [ 0 ] . click ( ) ;
buttons [ 1 ] . click ( ) ;
2020-01-09 10:02:27 +01:00
expect ( widget . otherState . vals ) . toStrictEqual ( [ "a" , "b" ] ) ;
2020-01-06 17:01:43 +01:00
expect ( QWeb . TEMPLATES [ SomeWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-on expression in t-foreach with t-set" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-06 17:01:43 +01:00
static template = xml `
<div>
<t t-set="bossa" t-value="'nova'"/>
<div t-foreach="state.values" t-as="val" t-key="val">
<t t-set="bossa" t-value="bossa + '_' + val_index" />
<t t-esc="val_index"/>: <t t-esc="val + ''"/>
<button t-on-click="otherState.vals.push(val + '_' + bossa)">Expr</button>
</div>
</div> ` ;
2020-01-09 10:02:27 +01:00
state = useState ( { values : [ "a" , "b" ] } ) ;
otherState = { vals : [ ] } ;
2020-01-06 17:01:43 +01:00
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
2020-01-09 10:02:27 +01:00
expect ( fixture . innerHTML ) . toBe (
"<div><div>0: a<button>Expr</button></div><div>1: b<button>Expr</button></div></div>"
) ;
2020-01-06 17:01:43 +01:00
expect ( widget . otherState . vals ) . toStrictEqual ( [ ] ) ;
2020-01-09 10:02:27 +01:00
const buttons = fixture . querySelectorAll ( "button" ) ;
2020-01-06 17:01:43 +01:00
buttons [ 0 ] . click ( ) ;
buttons [ 1 ] . click ( ) ;
2020-01-09 10:02:27 +01:00
expect ( widget . otherState . vals ) . toStrictEqual ( [ "a_nova_0" , "b_nova_0_1" ] ) ;
2020-01-06 17:01:43 +01:00
expect ( QWeb . TEMPLATES [ SomeWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-on method call in t-foreach" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-06 17:01:43 +01:00
static template = xml `
<div>
<div t-foreach="state.values" t-as="val" t-key="val">
<t t-esc="val_index"/>: <t t-esc="val + ''"/>
<button t-on-click="addVal(val)">meth call</button>
</div>
</div> ` ;
2020-01-09 10:02:27 +01:00
state = useState ( { values : [ "a" , "b" ] } ) ;
otherState = { vals : new Array < string > ( ) } ;
2020-01-06 17:01:43 +01:00
addVal ( val : string ) {
this . otherState . vals . push ( val ) ;
}
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
2020-01-09 10:02:27 +01:00
expect ( fixture . innerHTML ) . toBe (
"<div><div>0: a<button>meth call</button></div><div>1: b<button>meth call</button></div></div>"
) ;
2020-01-06 17:01:43 +01:00
expect ( widget . otherState . vals ) . toStrictEqual ( [ ] ) ;
2020-01-09 10:02:27 +01:00
const buttons = fixture . querySelectorAll ( "button" ) ;
2020-01-06 17:01:43 +01:00
buttons [ 0 ] . click ( ) ;
buttons [ 1 ] . click ( ) ;
2020-01-09 10:02:27 +01:00
expect ( widget . otherState . vals ) . toStrictEqual ( [ "a" , "b" ] ) ;
2020-01-06 17:01:43 +01:00
expect ( QWeb . TEMPLATES [ SomeWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "t-on expression captured in t-foreach" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeWidget extends Component {
2020-01-06 17:01:43 +01:00
static template = xml `
<div>
<t t-set="iter" t-value="0" />
<div t-foreach="arr" t-as="val" t-key="val">
<button t-on-click="otherState.vals.push(iter + '_' + iter)">expr</button>
<t t-set="iter" t-value="iter + 1" />
</div>
</div> ` ;
2020-01-09 10:02:27 +01:00
arr = [ "a" , "b" ] ;
otherState = { vals : new Array < string > ( ) } ;
2020-01-06 17:01:43 +01:00
}
const widget = new SomeWidget ( ) ;
await widget . mount ( fixture ) ;
2020-01-09 10:02:27 +01:00
expect ( fixture . innerHTML ) . toBe (
"<div><div><button>expr</button></div><div><button>expr</button></div></div>"
) ;
2020-01-06 17:01:43 +01:00
expect ( widget . otherState . vals ) . toStrictEqual ( [ ] ) ;
2020-01-09 10:02:27 +01:00
const buttons = fixture . querySelectorAll ( "button" ) ;
2020-01-06 17:01:43 +01:00
buttons [ 0 ] . click ( ) ;
buttons [ 1 ] . click ( ) ;
2020-01-09 10:02:27 +01:00
expect ( widget . otherState . vals ) . toStrictEqual ( [ "0_0" , "1_1" ] ) ;
2020-01-06 17:01:43 +01:00
expect ( QWeb . TEMPLATES [ SomeWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-02-02 18:17:21 +01:00
} ) ;
describe ( "random stuff/miscellaneous" , ( ) = > {
2019-01-30 13:33:37 +01:00
test ( "widget after a t-foreach" , async ( ) = > {
// this test makes sure that the foreach directive does not pollute sub
2019-06-20 09:42:33 +02:00
// context with the inLoop variable, which is then used in the t-component
2019-01-30 13:33:37 +01:00
// directive as a key
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate (
"Test" ,
2019-06-20 09:42:33 +02:00
` <div><t t-foreach="Array(2)">txt</t><t t-component="widget"/></div> `
2019-05-15 11:15:08 +02:00
) ;
2019-12-11 11:36:59 +01:00
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class Test extends Component {
2019-12-11 11:36:59 +01:00
static components = { widget : SomeComponent } ;
2019-01-30 13:33:37 +01:00
}
2019-10-14 15:09:17 +02:00
const widget = new Test ( ) ;
2019-01-30 13:33:37 +01:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>txttxt<div></div></div>" ) ;
} ) ;
2019-01-30 14:56:47 +01:00
2019-06-21 16:23:42 +02:00
test ( "t-on with handler bound to dynamic argument on a t-foreach" , async ( ) = > {
expect . assertions ( 3 ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-11 11:36:59 +01:00
static template = xml ` <div/> ` ;
}
2020-02-05 21:30:14 +01:00
class ParentWidget extends Component {
2019-10-30 14:57:24 +01:00
static template = xml `
<div>
<t t-foreach="items" t-as="item">
<Child t-key="item" t-on-ev="onEv(item)"/>
</t>
</div> ` ;
2019-09-10 22:37:08 +02:00
static components = { Child } ;
2019-10-30 14:57:24 +01:00
items = [ 1 , 2 , 3 , 4 ] ;
2019-06-21 16:23:42 +02:00
onEv ( n , ev ) {
expect ( n ) . toBe ( 1 ) ;
expect ( ev . detail ) . toBe ( 43 ) ;
}
}
2019-10-30 14:57:24 +01:00
const widget = new ParentWidget ( ) ;
2019-06-21 16:23:42 +02:00
await widget . mount ( fixture ) ;
children ( widget ) [ 0 ] . trigger ( "ev" , 43 ) ;
2019-10-30 14:57:24 +01:00
expect ( env . qweb . templates [ ParentWidget . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-06-21 16:23:42 +02:00
} ) ;
2019-01-30 14:56:47 +01:00
test ( "updating widget immediately" , async ( ) = > {
// in this situation, we protect against a bug that occurred: because of the
2019-06-20 09:42:33 +02:00
// interplay between components and vnodes, a sub widget vnode was patched
2019-01-30 14:56:47 +01:00
// twice.
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "Parent" , ` <div><t t-component="child" flag="state.flag"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component { }
class Parent extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : false } ) ;
2019-01-30 14:56:47 +01:00
}
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "Child" , ` <span>abc<t t-if="props.flag">def</t></span> ` ) ;
2019-01-30 14:56:47 +01:00
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-01-30 14:56:47 +01:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>abc</span></div>" ) ;
2019-04-17 10:07:31 +02:00
widget . state . flag = true ;
await nextTick ( ) ;
2019-01-30 14:56:47 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div><span>abcdef</span></div>" ) ;
} ) ;
2019-02-14 12:37:07 +01:00
test ( "snapshotting compiled code" , async ( ) = > {
env . qweb . addTemplate (
2019-05-15 11:15:08 +02:00
"Parent" ,
2019-06-20 09:42:33 +02:00
` <div><t t-component="child" t-key="'somestring'" flag="state.flag"/></div> `
2019-02-14 12:37:07 +01:00
) ;
2020-02-05 21:30:14 +01:00
class Child extends Component { }
class Parent extends Component {
2019-09-10 22:37:08 +02:00
static components = { child : Child } ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : false } ) ;
2019-02-14 12:37:07 +01:00
}
2019-06-28 10:32:03 +02:00
env . qweb . addTemplate ( "Child" , ` <span>abc<t t-if="props.flag">def</t></span> ` ) ;
2019-02-14 12:37:07 +01:00
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-03-28 17:35:08 +01:00
await widget . mount ( fixture ) ;
2019-05-15 11:15:08 +02:00
expect ( env . qweb . templates . Parent . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-03-28 17:35:08 +01:00
} ) ;
2019-05-08 15:21:33 +02:00
test ( "component semantics" , async ( ) = > {
let steps : string [ ] = [ ] ;
let c : C ;
2020-02-05 21:30:14 +01:00
class TestWidget extends Component {
2019-05-08 15:21:33 +02:00
name : string = "test" ;
async willStart() {
steps . push ( ` ${ this . name } :willStart ` ) ;
}
2019-09-19 11:42:00 +02:00
__render ( f ) {
2019-05-08 15:21:33 +02:00
steps . push ( ` ${ this . name } :render ` ) ;
2019-09-19 11:42:00 +02:00
return super . __render ( f ) ;
2019-05-08 15:21:33 +02:00
}
2020-02-16 09:46:47 +01:00
__patch ( target , vnode ) {
2019-06-24 13:02:12 +02:00
steps . push ( ` ${ this . name } :__patch ` ) ;
2020-02-16 09:46:47 +01:00
super . __patch ( target , vnode ) ;
2019-05-08 15:21:33 +02:00
}
mounted() {
steps . push ( ` ${ this . name } :mounted ` ) ;
}
async willUpdateProps() {
steps . push ( ` ${ this . name } :willUpdateProps ` ) ;
}
willPatch() {
steps . push ( ` ${ this . name } :willPatch ` ) ;
}
patched() {
steps . push ( ` ${ this . name } :patched ` ) ;
}
willUnmount() {
steps . push ( ` ${ this . name } :willUnmount ` ) ;
}
destroy() {
super . destroy ( ) ;
steps . push ( ` ${ this . name } :destroy ` ) ;
}
}
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "B" , ` <div>B</div> ` ) ;
2019-05-08 15:21:33 +02:00
class B extends TestWidget {
name = "B" ;
constructor ( parent , props ) {
super ( parent , props ) ;
steps . push ( "B:constructor" ) ;
}
}
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "D" , ` <div>D</div> ` ) ;
2019-05-08 15:21:33 +02:00
class D extends TestWidget {
name = "D" ;
constructor ( parent , props ) {
super ( parent , props ) ;
steps . push ( "D:constructor" ) ;
}
}
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "E" , ` <div>E</div> ` ) ;
2019-05-08 15:21:33 +02:00
class E extends TestWidget {
name = "E" ;
constructor ( parent , props ) {
super ( parent , props ) ;
steps . push ( "E:constructor" ) ;
}
}
2019-05-15 11:15:08 +02:00
env . qweb . addTemplate ( "F" , ` <div>F</div> ` ) ;
2019-05-08 15:21:33 +02:00
class F extends TestWidget {
name = "F" ;
constructor ( parent , props ) {
super ( parent , props ) ;
steps . push ( "F:constructor" ) ;
}
}
2019-09-10 22:37:08 +02:00
env . qweb . addTemplate (
"C" ,
`
<div>C<D />
<E t-if="state.flag" />
<F t-else="!state.flag" />
</div> `
) ;
class C extends TestWidget {
static components = { D , E , F } ;
name = "C" ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : true } ) ;
2019-09-10 22:37:08 +02:00
constructor ( parent , props ) {
super ( parent , props ) ;
c = this ;
steps . push ( "C:constructor" ) ;
}
}
env . qweb . addTemplate ( "A" , ` <div>A<B /><C /></div> ` ) ;
class A extends TestWidget {
static components = { B , C } ;
name = "A" ;
}
2019-10-14 15:09:17 +02:00
const a = new A ( ) ;
2019-05-08 15:21:33 +02:00
await a . mount ( fixture ) ;
2019-06-28 10:32:03 +02:00
expect ( fixture . innerHTML ) . toBe ( ` <div>A<div>B</div><div>C<div>D</div><div>E</div></div></div> ` ) ;
2019-05-08 15:21:33 +02:00
expect ( steps ) . toEqual ( [
"A:willStart" ,
"A:render" ,
"B:constructor" ,
"B:willStart" ,
"C:constructor" ,
"C:willStart" ,
"B:render" ,
"C:render" ,
"D:constructor" ,
"D:willStart" ,
"E:constructor" ,
"E:willStart" ,
"D:render" ,
"E:render" ,
2019-11-21 11:23:05 +01:00
"E:__patch" ,
"D:__patch" ,
"C:__patch" ,
"B:__patch" ,
2019-06-24 13:02:12 +02:00
"A:__patch" ,
2019-05-08 15:21:33 +02:00
"E:mounted" ,
2019-11-21 11:23:05 +01:00
"D:mounted" ,
2019-05-08 15:21:33 +02:00
"C:mounted" ,
2019-11-21 11:23:05 +01:00
"B:mounted" ,
2019-05-10 15:03:41 +02:00
"A:mounted"
2019-05-08 15:21:33 +02:00
] ) ;
// update
steps = [ ] ;
c ! . state . flag = false ;
await nextTick ( ) ;
expect ( steps ) . toEqual ( [
"C:render" ,
"D:willUpdateProps" ,
"F:constructor" ,
"F:willStart" ,
"D:render" ,
"F:render" ,
"C:willPatch" ,
"D:willPatch" ,
2019-11-21 11:23:05 +01:00
"F:__patch" ,
"D:__patch" ,
2019-06-24 13:02:12 +02:00
"C:__patch" ,
2019-05-08 15:21:33 +02:00
"E:willUnmount" ,
"E:destroy" ,
"F:mounted" ,
"D:patched" ,
"C:patched"
] ) ;
} ) ;
2019-09-21 23:05:44 +02:00
test ( "can inject values in tagged templates" , async ( ) = > {
2019-09-22 22:24:24 +02:00
const SUBTEMPLATE = xml ` <span><t t-esc="state.n"/></span> ` ;
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-22 22:24:24 +02:00
static template = xml ` <div><t t-call=" ${ SUBTEMPLATE } "/></div> ` ;
2019-09-24 14:06:53 +02:00
state = useState ( { n : 42 } ) ;
2019-09-22 22:24:24 +02:00
}
2019-09-21 23:05:44 +02:00
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-09-21 23:05:44 +02:00
await widget . mount ( fixture ) ;
expect ( env . qweb . templates [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-09-22 22:24:24 +02:00
expect ( fixture . innerHTML ) . toBe ( "<div><span>42</span></div>" ) ;
2019-09-21 23:05:44 +02:00
} ) ;
2019-01-30 14:56:47 +01:00
} ) ;
2019-02-07 12:46:30 +01:00
2020-01-18 09:02:37 +01:00
describe ( "widget and observable state" , ( ) = > {
test ( "widget is rerendered when its state is changed" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class TestWidget extends Component {
2020-01-18 09:02:37 +01:00
static template = xml ` <div><t t-esc="state.drink"/></div> ` ;
state = useState ( { drink : "water" } ) ;
2019-02-08 09:17:04 +01:00
}
2020-01-18 09:02:37 +01:00
const widget = new TestWidget ( ) ;
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>water</div>" ) ;
widget . state . drink = "beer" ;
2019-02-08 09:17:04 +01:00
await nextTick ( ) ;
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div>beer</div>" ) ;
2019-02-08 09:17:04 +01:00
} ) ;
2020-01-18 09:02:37 +01:00
test ( "subcomponents cannot change observable state received from parent" , async ( ) = > {
const consoleError = console . error ;
console . error = jest . fn ( ) ;
env . qweb . addTemplate ( "Parent" , ` <div><Child obj="state.obj"/></div> ` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2020-01-18 09:02:37 +01:00
static template = xml ` <div/> ` ;
2019-02-08 09:17:04 +01:00
constructor ( parent , props ) {
super ( parent , props ) ;
2020-01-18 09:02:37 +01:00
props . obj . coffee = 2 ;
2019-02-08 09:17:04 +01:00
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2020-01-18 09:02:37 +01:00
state = useState ( { obj : { coffee : 1 } } ) ;
2019-09-10 22:37:08 +02:00
static components = { Child } ;
}
2020-01-18 09:02:37 +01:00
const parent = new Parent ( ) ;
let error ;
try {
await parent . mount ( fixture ) ;
} catch ( e ) {
error = e ;
}
expect ( error ) . toBeDefined ( ) ;
expect ( error . message ) . toBe ( 'Observed state cannot be changed here! (key: "coffee", val: "2")' ) ;
expect ( console . error ) . toBeCalledTimes ( 0 ) ;
console . error = consoleError ;
2019-02-08 09:17:04 +01:00
} ) ;
2020-01-18 09:02:37 +01:00
} ) ;
2019-02-08 09:17:04 +01:00
2020-01-18 09:02:37 +01:00
describe ( "can deduce template from name" , ( ) = > {
test ( "can find template if name of component" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class ABC extends Component { }
2020-01-18 09:02:37 +01:00
env . qweb . addTemplate ( "ABC" , "<span>Orval</span>" ) ;
const abc = new ABC ( ) ;
await abc . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>Orval</span>" ) ;
2019-02-07 12:46:30 +01:00
} ) ;
2019-02-07 20:24:01 +01:00
2020-01-18 09:02:37 +01:00
test ( "can find template of parent component" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class ABC extends Component { }
2020-01-18 09:02:37 +01:00
class DEF extends ABC { }
env . qweb . addTemplate ( "ABC" , "<span>Orval</span>" ) ;
const def = new DEF ( ) ;
await def . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>Orval</span>" ) ;
} ) ;
2019-02-07 20:24:01 +01:00
2020-01-18 09:02:37 +01:00
test ( "can find template of parent component, defined by template key" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class ABC extends Component {
2020-01-18 09:02:37 +01:00
static template = "Achel" ;
2019-02-07 20:24:01 +01:00
}
2020-01-18 09:02:37 +01:00
class DEF extends ABC { }
env . qweb . addTemplate ( "Achel" , "<span>Orval</span>" ) ;
const def = new DEF ( ) ;
await def . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>Orval</span>" ) ;
2019-10-25 15:45:56 +02:00
} ) ;
2020-01-18 09:02:37 +01:00
test ( "templates are found in proper qweb instance" , async ( ) = > {
const env2 = makeTestEnv ( ) ;
env . qweb . addTemplate ( "ABC" , "<span>Rochefort 8</span>" ) ;
env2 . qweb . addTemplate ( "ABC" , "<span>Rochefort 10</span>" ) ;
2020-02-05 21:30:14 +01:00
class ABC extends Component { }
2020-01-18 09:02:37 +01:00
const abc = new ABC ( ) ;
await abc . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>Rochefort 8</span>" ) ;
abc . destroy ( ) ;
Component . env = env2 ;
const abc2 = new ABC ( ) ;
await abc2 . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>Rochefort 10</span>" ) ;
} ) ;
} ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
describe ( "t-model directive" , ( ) = > {
test ( "basic use, on an input" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2019-10-25 15:45:56 +02:00
static template = xml `
<div>
2020-01-18 09:02:37 +01:00
<input t-model="state.text"/>
<span><t t-esc="state.text"/></span>
2019-10-25 15:45:56 +02:00
</div> ` ;
2020-01-18 09:02:37 +01:00
state = useState ( { text : "" } ) ;
2019-10-25 15:45:56 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
2019-10-24 21:20:52 +02:00
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div><input><span></span></div>" ) ;
2019-10-24 21:20:52 +02:00
2020-01-18 09:02:37 +01:00
const input = fixture . querySelector ( "input" ) ! ;
await editInput ( input , "test" ) ;
expect ( comp . state . text ) . toBe ( "test" ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><input><span>test</span></div>" ) ;
expect ( env . qweb . templates [ SomeComponent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-02-07 20:24:01 +01:00
} ) ;
2019-05-06 14:23:29 +02:00
2020-01-18 09:02:37 +01:00
test ( "basic use, on another key in component" , async ( ) = > {
2019-05-15 15:18:27 +02:00
env . qweb . addTemplates ( `
2020-01-18 09:02:37 +01:00
<templates>
<div t-name="SomeComponent">
<input t-model="some.text"/>
<span><t t-esc="some.text"/></span>
</div>
</templates> ` ) ;
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
some = useState ( { text : "" } ) ;
2019-05-06 14:23:29 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
2019-05-06 14:23:29 +02:00
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div><input><span></span></div>" ) ;
2019-05-06 14:23:29 +02:00
2020-01-18 09:02:37 +01:00
const input = fixture . querySelector ( "input" ) ! ;
await editInput ( input , "test" ) ;
expect ( comp . some . text ) . toBe ( "test" ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><input><span>test</span></div>" ) ;
expect ( env . qweb . templates . SomeComponent . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-05-06 14:23:29 +02:00
} ) ;
2019-06-01 21:44:43 +02:00
2020-01-18 09:02:37 +01:00
test ( "on an input, type=checkbox" , async ( ) = > {
2019-06-01 21:44:43 +02:00
env . qweb . addTemplates ( `
2020-01-18 09:02:37 +01:00
<templates>
<div t-name="SomeComponent">
<input type="checkbox" t-model="state.flag"/>
<span>
<t t-if="state.flag">yes</t>
<t t-else="">no</t>
2019-06-01 21:44:43 +02:00
</span>
2020-01-18 09:02:37 +01:00
</div>
</templates> ` ) ;
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
state = useState ( { flag : false } ) ;
2019-06-01 21:44:43 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
2019-06-01 21:44:43 +02:00
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe ( '<div><input type="checkbox"><span>no</span></div>' ) ;
2019-06-01 21:44:43 +02:00
2020-01-18 09:02:37 +01:00
let input = fixture . querySelector ( "input" ) ! ;
input . click ( ) ;
2019-06-01 21:44:43 +02:00
await nextTick ( ) ;
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe ( '<div><input type="checkbox"><span>yes</span></div>' ) ;
expect ( comp . state . flag ) . toBe ( true ) ;
expect ( env . qweb . templates . SomeComponent . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-06-01 21:44:43 +02:00
2020-01-18 09:02:37 +01:00
input . click ( ) ;
2019-06-01 21:44:43 +02:00
await nextTick ( ) ;
2020-01-18 09:02:37 +01:00
expect ( comp . state . flag ) . toBe ( false ) ;
2019-06-01 21:44:43 +02:00
} ) ;
2019-06-14 10:17:13 +02:00
2020-01-18 09:02:37 +01:00
test ( "on an textarea" , async ( ) = > {
env . qweb . addTemplates ( `
<templates>
<div t-name="SomeComponent">
<textarea t-model="state.text"/>
<span><t t-esc="state.text"/></span>
</div>
</templates> ` ) ;
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
state = useState ( { text : "" } ) ;
2019-10-25 15:45:56 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
2019-07-30 08:47:31 +02:00
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div><textarea></textarea><span></span></div>" ) ;
2019-07-30 08:47:31 +02:00
2020-01-18 09:02:37 +01:00
const textarea = fixture . querySelector ( "textarea" ) ! ;
await editInput ( textarea , "test" ) ;
expect ( comp . state . text ) . toBe ( "test" ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><textarea></textarea><span>test</span></div>" ) ;
2019-07-30 08:47:31 +02:00
} ) ;
2019-03-19 11:48:30 +01:00
2020-01-18 09:02:37 +01:00
test ( "on an input type=radio" , async ( ) = > {
env . qweb . addTemplates ( `
<templates>
<div t-name="SomeComponent">
<input type="radio" id="one" value="One" t-model="state.choice"/>
<input type="radio" id="two" value="Two" t-model="state.choice"/>
<span>Choice: <t t-esc="state.choice"/></span>
</div>
</templates> ` ) ;
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
state = useState ( { choice : "" } ) ;
2019-09-10 22:37:08 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
2019-07-09 14:33:56 +02:00
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe (
'<div><input type="radio" id="one" value="One"><input type="radio" id="two" value="Two"><span>Choice: </span></div>'
) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
const firstInput = fixture . querySelector ( "input" ) ! ;
firstInput . click ( ) ;
2019-10-25 15:45:56 +02:00
await nextTick ( ) ;
2020-01-18 09:02:37 +01:00
expect ( comp . state . choice ) . toBe ( "One" ) ;
expect ( fixture . innerHTML ) . toBe (
'<div><input type="radio" id="one" value="One"><input type="radio" id="two" value="Two"><span>Choice: One</span></div>'
) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
const secondInput = fixture . querySelectorAll ( "input" ) [ 1 ] ;
secondInput . click ( ) ;
2019-10-25 15:45:56 +02:00
await nextTick ( ) ;
2020-01-18 09:02:37 +01:00
expect ( comp . state . choice ) . toBe ( "Two" ) ;
expect ( fixture . innerHTML ) . toBe (
'<div><input type="radio" id="one" value="One"><input type="radio" id="two" value="Two"><span>Choice: Two</span></div>'
) ;
expect ( env . qweb . templates . SomeComponent . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-10-25 15:45:56 +02:00
} ) ;
2020-01-18 09:02:37 +01:00
test ( "on a select" , async ( ) = > {
env . qweb . addTemplates ( `
<templates>
<div t-name="SomeComponent">
<select t-model="state.color">
<option value="">Please select one</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
</select>
<span>Choice: <t t-esc="state.color"/></span>
</div>
</templates> ` ) ;
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
state = useState ( { color : "" } ) ;
2019-10-25 15:45:56 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe (
'<div><select><option value="">Please select one</option><option value="red">Red</option><option value="blue">Blue</option></select><span>Choice: </span></div>'
) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
const select = fixture . querySelector ( "select" ) ! ;
select . value = "red" ;
select . dispatchEvent ( new Event ( "change" ) ) ;
2019-10-25 15:45:56 +02:00
await nextTick ( ) ;
2020-01-18 09:02:37 +01:00
expect ( comp . state . color ) . toBe ( "red" ) ;
expect ( fixture . innerHTML ) . toBe (
'<div><select><option value="">Please select one</option><option value="red">Red</option><option value="blue">Blue</option></select><span>Choice: red</span></div>'
) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
expect ( env . qweb . templates . SomeComponent . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-10-25 15:45:56 +02:00
} ) ;
2020-01-18 09:02:37 +01:00
test ( "on a select, initial state" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
static template = xml `
<div>
<select t-model="state.color">
<option value="">Please select one</option>
<option value="red">Red</option>
<option value="blue">Blue</option>
</select>
</div>
` ;
state = useState ( { color : "red" } ) ;
2019-10-25 15:45:56 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
const select = fixture . querySelector ( "select" ) ! ;
expect ( select . value ) . toBe ( "red" ) ;
} ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
test ( "on a sub state key" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
static template = xml `
<div>
<input t-model="state.something.text"/>
<span><t t-esc="state.something.text"/></span>
</div>
` ;
state = useState ( { something : { text : "" } } ) ;
2019-10-25 15:45:56 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div><input><span></span></div>" ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
const input = fixture . querySelector ( "input" ) ! ;
await editInput ( input , "test" ) ;
expect ( comp . state . something . text ) . toBe ( "test" ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><input><span>test</span></div>" ) ;
expect ( env . qweb . templates [ SomeComponent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
test ( ".lazy modifier" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
static template = xml `
<div>
<input t-model.lazy="state.text"/>
<span><t t-esc="state.text"/></span>
</div>
` ;
state = useState ( { text : "" } ) ;
}
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div><input><span></span></div>" ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
const input = fixture . querySelector ( "input" ) ! ;
input . value = "test" ;
input . dispatchEvent ( new Event ( "input" ) ) ;
2019-10-25 15:45:56 +02:00
await nextTick ( ) ;
2020-01-18 09:02:37 +01:00
expect ( comp . state . text ) . toBe ( "" ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><input><span></span></div>" ) ;
input . dispatchEvent ( new Event ( "change" ) ) ;
2019-10-25 15:45:56 +02:00
await nextTick ( ) ;
2020-01-18 09:02:37 +01:00
expect ( comp . state . text ) . toBe ( "test" ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><input><span>test</span></div>" ) ;
expect ( env . qweb . templates [ SomeComponent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-10-25 15:45:56 +02:00
} ) ;
2020-01-18 09:02:37 +01:00
test ( ".trim modifier" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
static template = xml `
<div t-name="SomeComponent">
<input t-model.trim="state.text"/>
<span><t t-esc="state.text"/></span>
</div>
` ;
state = useState ( { text : "" } ) ;
2019-10-25 15:45:56 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
const input = fixture . querySelector ( "input" ) ! ;
await editInput ( input , " test " ) ;
expect ( comp . state . text ) . toBe ( "test" ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><input><span>test</span></div>" ) ;
} ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
test ( ".number modifier" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
static template = xml `
<div>
<input t-model.number="state.number"/>
<span><t t-esc="state.number"/></span>
</div>
` ;
state = useState ( { number : 0 } ) ;
2019-10-25 15:45:56 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><input><span>0</span></div>" ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
const input = fixture . querySelector ( "input" ) ! ;
await editInput ( input , "13" ) ;
expect ( comp . state . number ) . toBe ( 13 ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><input><span>13</span></div>" ) ;
2019-10-25 15:45:56 +02:00
2020-01-18 09:02:37 +01:00
await editInput ( input , "invalid" ) ;
expect ( comp . state . number ) . toBe ( "invalid" ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><input><span>invalid</span></div>" ) ;
2019-10-25 15:45:56 +02:00
} ) ;
2020-01-18 09:02:37 +01:00
test ( "in a t-foreach" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2020-01-18 09:02:37 +01:00
static template = xml `
<div>
<t t-foreach="state" t-as="thing" t-key="thing.id">
<input type="checkbox" t-model="thing.f"/>
</t>
</div>
` ;
state = useState ( [
{ f : false , id : 1 } ,
{ f : false , id : 2 } ,
{ f : false , id : 3 }
] ) ;
2019-10-25 15:45:56 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe (
'<div><input type="checkbox"><input type="checkbox"><input type="checkbox"></div>'
) ;
2019-07-09 14:33:56 +02:00
2020-01-18 09:02:37 +01:00
const input = fixture . querySelectorAll ( "input" ) [ 1 ] ! ;
input . click ( ) ;
expect ( comp . state [ 1 ] . f ) . toBe ( true ) ;
expect ( comp . state [ 0 ] . f ) . toBe ( false ) ;
expect ( comp . state [ 2 ] . f ) . toBe ( false ) ;
expect ( env . qweb . templates [ SomeComponent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-07-09 14:33:56 +02:00
} ) ;
2020-01-18 09:02:37 +01:00
test ( "two inputs in a div with a t-key" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class SomeComponent extends Component {
2019-10-25 15:45:56 +02:00
static template = xml `
2020-01-18 09:02:37 +01:00
<div t-key="'key'">
<input class="a" t-if="state.flag"/>
<input class="b" t-if="!state.flag"/>
2019-07-09 14:33:56 +02:00
</div>
2020-01-18 09:02:37 +01:00
` ;
state = useState ( { flag : true } ) ;
2019-07-09 14:33:56 +02:00
}
2020-01-18 09:02:37 +01:00
const comp = new SomeComponent ( ) ;
await comp . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( '<div><input class="a"></div>' ) ;
expect ( env . qweb . templates [ SomeComponent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
fixture . querySelector ( "input" ) ! . value = "asdf" ;
expect ( fixture . querySelector ( "input" ) ! . value ) . toBe ( "asdf" ) ;
comp . state . flag = false ;
2019-07-09 14:33:56 +02:00
await nextTick ( ) ;
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe ( '<div><input class="b"></div>' ) ;
expect ( fixture . querySelector ( "input" ) ! . value ) . toBe ( "" ) ;
} ) ;
} ) ;
2019-10-26 16:44:12 +02:00
2020-01-18 09:02:37 +01:00
describe ( "environment and plugins" , ( ) = > {
// some source of external events
let bus = new EventBus ( ) ;
2019-10-26 16:44:12 +02:00
2020-01-18 09:02:37 +01:00
// definition of a plugin
const somePlugin = env = > {
env . someFlag = true ;
bus . on ( "some-event" , null , ( ) = > {
env . someFlag = ! env . someFlag ;
env . qweb . forceUpdate ( ) ;
} ) ;
} ;
2019-10-26 16:44:12 +02:00
2020-01-18 09:02:37 +01:00
test ( "plugin works as expected" , async ( ) = > {
somePlugin ( env ) ;
2020-02-05 21:30:14 +01:00
class App extends Component {
2020-01-18 09:02:37 +01:00
static template = xml `
<div>
<t t-if="env.someFlag">Red</t>
<t t-else="">Blue</t>
</div>
` ;
2019-10-26 16:44:12 +02:00
}
2019-10-14 15:09:17 +02:00
const app = new App ( ) ;
2019-10-26 16:44:12 +02:00
await app . mount ( fixture ) ;
2020-01-18 09:02:37 +01:00
expect ( fixture . innerHTML ) . toBe ( "<div>Red</div>" ) ;
bus . trigger ( "some-event" ) ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>Blue</div>" ) ;
2019-10-26 16:44:12 +02:00
} ) ;
2019-10-30 11:35:28 +01:00
2020-01-18 09:02:37 +01:00
test ( "can define specific env for root components" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class App1 extends Component {
2019-10-30 11:35:28 +01:00
static template = xml ` <span></span> ` ;
}
2020-01-18 09:02:37 +01:00
App1 . env = { test : 1 } ;
2020-02-05 21:30:14 +01:00
class App2 extends Component {
2020-01-18 09:02:37 +01:00
static template = xml ` <span></span> ` ;
2019-10-30 11:35:28 +01:00
}
2020-01-18 09:02:37 +01:00
App2 . env = { test : 2 } ;
class App1B extends App1 { }
class App2B extends App2 { }
App2B . env = { test : 3 } ;
2019-10-30 11:35:28 +01:00
2020-01-18 09:02:37 +01:00
const app1 = new App1 ( ) ;
const app2 = new App2 ( ) ;
const app1B = new App1B ( ) ;
const app2B = new App2B ( ) ;
expect ( app1 . env . qweb ) . toBeDefined ( ) ;
expect ( app1 . env . test ) . toBe ( 1 ) ;
expect ( app2 . env . test ) . toBe ( 2 ) ;
expect ( app1B . env . test ) . toBe ( 1 ) ;
expect ( app2B . env . test ) . toBe ( 3 ) ;
2019-10-30 11:35:28 +01:00
} ) ;
2019-07-09 14:33:56 +02:00
} ) ;
2019-07-11 16:19:59 +02:00
describe ( "top level sub widgets" , ( ) = > {
test ( "basic use" , async ( ) = > {
env . qweb . addTemplates ( `
<templates>
<t t-name="Parent">
<Child p="1"/>
</t>
<span t-name="Child">child<t t-esc="props.p"/></span>
</templates> ` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component { }
class Parent extends Component {
2019-09-10 22:37:08 +02:00
static components = { Child } ;
2019-07-11 16:19:59 +02:00
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-07-11 16:19:59 +02:00
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>child1</span>" ) ;
expect ( env . qweb . templates . Parent . fn . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
test ( "sub widget is interactive" , async ( ) = > {
env . qweb . addTemplates ( `
<templates>
<t t-name="Parent">
<Child p="1"/>
</t>
<span t-name="Child"><button t-on-click="inc">click</button>child<t t-esc="state.val"/></span>
</templates> ` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-09-24 14:06:53 +02:00
state = useState ( { val : 1 } ) ;
2019-07-30 08:47:31 +02:00
inc() {
this . state . val ++ ;
}
2019-07-11 16:19:59 +02:00
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-10 22:37:08 +02:00
static components = { Child } ;
2019-07-11 16:19:59 +02:00
}
2019-10-14 15:09:17 +02:00
const parent = new Parent ( ) ;
2019-07-11 16:19:59 +02:00
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span><button>click</button>child1</span>" ) ;
2019-07-30 08:47:31 +02:00
const button = fixture . querySelector ( "button" ) ! ;
2019-07-11 16:19:59 +02:00
button . click ( ) ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<span><button>click</button>child2</span>" ) ;
} ) ;
test ( "can select a sub widget " , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <span>CHILD 1</span> ` ;
}
2020-02-05 21:30:14 +01:00
class OtherChild extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <div>CHILD 2</div> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<t>
<t t-if="env.flag"><Child /></t>
<t t-if="!env.flag"><OtherChild /></t>
</t>
` ;
2019-09-10 22:37:08 +02:00
static components = { Child , OtherChild } ;
2019-07-11 16:19:59 +02:00
}
( < any > env ) . flag = true ;
2019-10-14 15:09:17 +02:00
let parent = new Parent ( ) ;
2019-07-11 16:19:59 +02:00
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>CHILD 1</span>" ) ;
parent . destroy ( ) ;
( < any > env ) . flag = false ;
2019-10-14 15:09:17 +02:00
parent = new Parent ( ) ;
2019-07-11 16:19:59 +02:00
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>CHILD 2</div>" ) ;
2019-09-12 09:45:32 +02:00
expect ( env . qweb . templates [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-07-11 16:19:59 +02:00
} ) ;
test ( "can select a sub widget, part 2" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <span>CHILD 1</span> ` ;
}
2020-02-05 21:30:14 +01:00
class OtherChild extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <div>CHILD 2</div> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<t>
<t t-if="state.flag"><Child /></t>
<t t-if="!state.flag"><OtherChild /></t>
</t>
` ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : true } ) ;
2019-09-10 22:37:08 +02:00
static components = { Child , OtherChild } ;
2019-07-11 16:19:59 +02:00
}
2019-10-14 15:09:17 +02:00
let parent = new Parent ( ) ;
2019-07-11 16:19:59 +02:00
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>CHILD 1</span>" ) ;
parent . state . flag = false ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>CHILD 2</div>" ) ;
} ) ;
2019-10-25 15:45:56 +02:00
test ( "top level sub widget with a parent" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class ComponentC extends Component {
2019-10-25 15:45:56 +02:00
static template = xml ` <span>Hello</span> ` ;
}
2020-02-05 21:30:14 +01:00
class ComponentB extends Component {
2019-10-25 15:45:56 +02:00
static template = xml ` <ComponentC /> ` ;
static components = { ComponentC } ;
}
2020-02-05 21:30:14 +01:00
class ComponentA extends Component {
2019-10-25 15:45:56 +02:00
static template = xml ` <div><ComponentB/></div> ` ;
static components = { ComponentB } ;
}
2019-10-14 15:09:17 +02:00
const component = new ComponentA ( ) ;
2019-10-25 15:45:56 +02:00
await component . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>Hello</span></div>" ) ;
} ) ;
2019-07-11 16:19:59 +02:00
} ) ;
2019-08-22 13:32:53 +02:00
2019-09-11 11:28:41 +02:00
describe ( "dynamic root nodes" , ( ) = > {
test ( "template with t-if, part 1" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class TestWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<t>
<t t-if="true"><span>hey</span></t>
<t t-if="false"><div>abc</div></t>
</t>
` ;
}
2019-09-11 11:28:41 +02:00
2019-10-14 15:09:17 +02:00
const widget = new TestWidget ( ) ;
2019-09-11 11:28:41 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>hey</span>" ) ;
} ) ;
test ( "template with t-if, part 2" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class TestWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<t>
<t t-if="false"><span>hey</span></t>
<t t-if="true"><div>abc</div></t>
</t>
` ;
}
2019-09-11 11:28:41 +02:00
2019-10-14 15:09:17 +02:00
const widget = new TestWidget ( ) ;
2019-09-11 11:28:41 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>abc</div>" ) ;
} ) ;
test ( "switching between sub branches dynamically" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class TestWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<t>
<t t-if="state.flag"><span>hey</span></t>
<t t-if="!state.flag"><div>abc</div></t>
</t>
` ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : true } ) ;
2019-09-11 11:28:41 +02:00
}
2019-10-14 15:09:17 +02:00
const widget = new TestWidget ( ) ;
2019-09-11 11:28:41 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>hey</span>" ) ;
widget . state . flag = false ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>abc</div>" ) ;
} ) ;
test ( "switching between sub components dynamically" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class ChildA extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <span>hey</span> ` ;
}
2020-02-05 21:30:14 +01:00
class ChildB extends Component {
2019-09-12 09:45:32 +02:00
static template = xml ` <div>abc</div> ` ;
}
2020-02-05 21:30:14 +01:00
class TestWidget extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<t>
<t t-if="state.flag"><ChildA/></t>
<t t-if="!state.flag"><ChildB/></t>
</t>
` ;
2019-09-11 14:01:49 +02:00
static components = { ChildA , ChildB } ;
2019-09-24 14:06:53 +02:00
state = useState ( { flag : true } ) ;
2019-09-11 11:28:41 +02:00
}
2019-10-14 15:09:17 +02:00
const widget = new TestWidget ( ) ;
2019-09-11 11:28:41 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>hey</span>" ) ;
widget . state . flag = false ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div>abc</div>" ) ;
} ) ;
2019-09-11 14:01:49 +02:00
} ) ;
2019-09-11 11:28:41 +02:00
2019-09-11 14:01:49 +02:00
describe ( "dynamic t-props" , ( ) = > {
test ( "basic use" , async ( ) = > {
expect . assertions ( 4 ) ;
2019-09-12 09:45:32 +02:00
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<span>
<t t-esc="props.a + props.b"/>
</span>
` ;
2019-09-11 14:01:49 +02:00
constructor ( parent , props ) {
super ( parent , props ) ;
expect ( props ) . toEqual ( { a : 1 , b : 2 } ) ;
expect ( props ) . not . toBe ( widget . some . obj ) ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-09-12 09:45:32 +02:00
static template = xml `
<div>
<Child t-props="some.obj"/>
</div>
` ;
2019-09-11 14:01:49 +02:00
static components = { Child } ;
some = { obj : { a : 1 , b : 2 } } ;
}
2019-10-14 15:09:17 +02:00
const widget = new Parent ( ) ;
2019-09-11 14:01:49 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>3</span></div>" ) ;
2019-09-12 09:45:32 +02:00
expect ( env . qweb . templates [ Parent . template ] . fn . toString ( ) ) . toMatchSnapshot ( ) ;
2019-09-11 14:01:49 +02:00
} ) ;
} ) ;
2019-09-26 15:19:45 +02:00
describe ( "support svg components" , ( ) = > {
test ( "add proper namespace to svg" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class GComp extends Component {
2019-09-26 15:19:45 +02:00
static template = xml `
<g>
<circle cx="50" cy="50" r="4" stroke="green" stroke-width="1" fill="yellow"/>
</g> ` ;
}
2020-02-05 21:30:14 +01:00
class Svg extends Component {
2019-09-26 15:19:45 +02:00
static template = xml `
<svg>
<GComp/>
</svg> ` ;
static components = { GComp } ;
}
2019-10-14 15:09:17 +02:00
const widget = new Svg ( ) ;
2019-09-26 15:19:45 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe (
'<svg><g><circle cx="50" cy="50" r="4" stroke="green" stroke-width="1" fill="yellow"></circle></g></svg>'
) ;
} ) ;
} ) ;
2019-10-12 09:07:25 +02:00
describe ( "t-raw in components" , ( ) = > {
test ( "update properly on state changes" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class TestW extends Component {
2019-10-12 09:07:25 +02:00
static template = xml ` <div><t t-raw="state.value"/></div> ` ;
state = useState ( { value : "<b>content</b>" } ) ;
}
2019-10-14 15:09:17 +02:00
const widget = new TestW ( ) ;
2019-10-12 09:07:25 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><b>content</b></div>" ) ;
widget . state . value = "<span>other content</span>" ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>other content</span></div>" ) ;
} ) ;
test ( "can render list of t-raw " , async ( ) = > {
2020-02-05 21:30:14 +01:00
class TestW extends Component {
2019-10-12 09:07:25 +02:00
static template = xml `
<div>
<t t-foreach="state.items" t-as="item">
<t t-esc="item"/>
<t t-raw="item"/>
</t>
</div> ` ;
state = useState ( { items : [ "<b>one</b>" , "<b>two</b>" , "<b>tree</b>" ] } ) ;
}
2019-10-14 15:09:17 +02:00
const widget = new TestW ( ) ;
2019-10-12 09:07:25 +02:00
await widget . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe (
"<div><b>one</b><b>one</b><b>two</b><b>two</b><b>tree</b><b>tree</b></div>"
) ;
} ) ;
} ) ;
2019-12-12 11:21:49 +01:00
describe ( "t-call" , ( ) = > {
test ( "handlers are properly bound through a t-call" , async ( ) = > {
expect . assertions ( 3 ) ;
env . qweb . addTemplate ( "sub" , ` <p t-on-click="update">lucas</p> ` ) ;
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-12 11:21:49 +01:00
static template = xml ` <div><t t-call="sub"/></div> ` ;
update() {
expect ( this ) . toBe ( parent ) ;
}
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><p>lucas</p></div>" ) ;
fixture . querySelector ( "p" ) ! . click ( ) ;
expect ( env . qweb . subTemplates [ "sub" ] . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-12-13 16:02:03 +01:00
test ( "parent is set within t-call" , async ( ) = > {
env . qweb . addTemplate ( "sub" , ` <Child/> ` ) ;
let child ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-13 14:50:49 +01:00
static template = xml ` <span>lucas</span> ` ;
2019-12-13 16:02:03 +01:00
constructor ( ) {
super ( . . . arguments ) ;
child = this ;
}
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-13 14:50:49 +01:00
static components = { Child } ;
2019-12-13 16:02:03 +01:00
static template = xml ` <div><t t-call="sub"/></div> ` ;
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>lucas</span></div>" ) ;
expect ( child . __owl__ . parent ) . toBe ( parent ) ;
expect ( env . qweb . subTemplates [ "sub" ] . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-12-16 14:20:36 +01:00
test ( "t-call in t-foreach and children component" , async ( ) = > {
env . qweb . addTemplate ( "sub" , ` <Child val="val"/> ` ) ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-16 14:20:36 +01:00
static template = xml ` <span><t t-esc="props.val"/></span> ` ;
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-16 14:20:36 +01:00
static components = { Child } ;
static template = xml `
<div>
<t t-foreach="['a', 'b', 'c']" t-as="val" t-key="val">
<t t-call="sub"/>
</t>
</div> ` ;
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>a</span><span>b</span><span>c</span></div>" ) ;
} ) ;
2019-12-13 16:02:03 +01:00
test ( "parent is set within t-call with no parentNode" , async ( ) = > {
env . qweb . addTemplate ( "sub" , ` <Child/> ` ) ;
let child ;
2020-02-05 21:30:14 +01:00
class Child extends Component {
2019-12-13 16:02:03 +01:00
constructor ( ) {
super ( . . . arguments ) ;
child = this ;
}
2019-12-13 14:50:49 +01:00
static template = xml ` <span>lucas</span> ` ;
2019-12-13 16:02:03 +01:00
}
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-13 14:50:49 +01:00
static components = { Child } ;
2019-12-13 16:02:03 +01:00
static template = xml ` <t t-call="sub"/> ` ;
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<span>lucas</span>" ) ;
expect ( child . __owl__ . parent ) . toBe ( parent ) ;
expect ( env . qweb . subTemplates [ "sub" ] . toString ( ) ) . toMatchSnapshot ( ) ;
} ) ;
2019-12-12 11:21:49 +01:00
test ( "handlers with arguments are properly bound through a t-call" , async ( ) = > {
expect . assertions ( 3 ) ;
env . qweb . addTemplate ( "sub" , ` <p t-on-click="update(a)">lucas</p> ` ) ;
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2019-12-12 11:21:49 +01:00
static template = xml ` <div><t t-call="sub"/></div> ` ;
update ( a ) {
expect ( this ) . toBe ( parent ) ;
expect ( a ) . toBe ( 3 ) ;
}
a = 3 ;
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
expect ( env . qweb . subTemplates [ "sub" ] . toString ( ) ) . toMatchSnapshot ( ) ;
fixture . querySelector ( "p" ) ! . click ( ) ;
} ) ;
2020-01-08 15:05:19 +01:00
test ( "sub components in two t-calls" , async ( ) = > {
2020-02-05 21:30:14 +01:00
class Child extends Component {
2020-01-08 15:05:19 +01:00
static template = xml ` <span><t t-esc="props.val"/></span> ` ;
}
env . qweb . addTemplate ( "sub" , ` <Child val="state.val"/> ` ) ;
2020-02-05 21:30:14 +01:00
class Parent extends Component {
2020-01-08 15:05:19 +01:00
static template = xml `
<div>
<t t-if="state.val===1">
<t t-call="sub"/>
</t>
<div t-else=""><t t-call="sub"/></div>
</div> ` ;
static components = { Child } ;
2020-01-09 10:02:27 +01:00
state = useState ( { val : 1 } ) ;
2020-01-08 15:05:19 +01:00
}
const parent = new Parent ( ) ;
await parent . mount ( fixture ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><span>1</span></div>" ) ;
parent . state . val = 2 ;
await nextTick ( ) ;
expect ( fixture . innerHTML ) . toBe ( "<div><div><span>2</span></div></div>" ) ;
} ) ;
2019-12-12 11:21:49 +01:00
} ) ;