1
+ /**
2
+ * @license
3
+ * Copyright 2017 Google Inc. All rights reserved.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ import { expect } from 'chai'
19
+
20
+ function skip ( ) {
21
+ this . skip ( ) ;
22
+ return { } ;
23
+ }
24
+
25
+ export default function (
26
+ {
27
+ renderComponentWithoutChildren = skip ,
28
+ renderComponentWithChildren = skip ,
29
+ renderComponentWithChildrenRerender = skip ,
30
+ renderComponentWithDifferentViews = skip ,
31
+ renderComponentWithProperties = skip ,
32
+ renderComponentWithImperativeEvent = skip ,
33
+ } ) {
34
+
35
+ describe ( 'basic support' , function ( ) {
36
+
37
+ describe ( 'no children' , function ( ) {
38
+ it ( 'can display a Custom Element with no children' , async function ( ) {
39
+ this . weight = 3
40
+ const { wc } = await renderComponentWithoutChildren . call ( this ) ;
41
+ expect ( wc ) . to . exist
42
+ } )
43
+ } )
44
+
45
+ describe ( 'with children' , function ( ) {
46
+ function expectHasChildren ( wc ) {
47
+ expect ( wc ) . to . exist
48
+ let shadowRoot = wc . shadowRoot
49
+ let heading = shadowRoot . querySelector ( 'h1' )
50
+ expect ( heading ) . to . exist
51
+ expect ( heading . textContent ) . to . eql ( 'Test h1' )
52
+ let paragraph = shadowRoot . querySelector ( 'p' )
53
+ expect ( paragraph ) . to . exist
54
+ expect ( paragraph . textContent ) . to . eql ( 'Test p' )
55
+ }
56
+
57
+ it ( 'can display a Custom Element with children in a Shadow Root' , async function ( ) {
58
+ this . weight = 3
59
+ const { wc } = await renderComponentWithChildren . call ( this ) ;
60
+ expectHasChildren ( wc )
61
+ } )
62
+
63
+ it ( 'can display a Custom Element with children in a Shadow Root and pass in Light DOM children' , async function ( ) {
64
+ this . weight = 3
65
+ const { wc } = await renderComponentWithChildrenRerender . call ( this ) ;
66
+ expectHasChildren ( wc )
67
+ expect ( wc . textContent . includes ( '2' ) ) . to . be . true
68
+ } )
69
+
70
+ it ( 'can display a Custom Element with children in the Shadow DOM and handle hiding and showing the element' , async function ( ) {
71
+ this . weight = 3
72
+ const { wc, toggle } = await renderComponentWithDifferentViews . call ( this ) ;
73
+ expectHasChildren ( wc )
74
+ toggle ( )
75
+ let dummy = document . querySelector ( '#dummy' )
76
+ expect ( dummy ) . to . exist
77
+ expect ( dummy . textContent ) . to . eql ( 'Dummy view' )
78
+ toggle ( )
79
+ expectHasChildren ( wc )
80
+ } )
81
+ } )
82
+
83
+ describe ( 'attributes and properties' , function ( ) {
84
+ it ( 'will pass boolean data as either an attribute or a property' , async function ( ) {
85
+ this . weight = 3
86
+ const { wc } = await renderComponentWithProperties . call ( this ) ;
87
+ let data = wc . bool || wc . hasAttribute ( 'bool' )
88
+ expect ( data ) . to . be . true
89
+ } )
90
+
91
+ it ( 'will pass numeric data as either an attribute or a property' , async function ( ) {
92
+ this . weight = 3
93
+ const { wc } = await renderComponentWithProperties . call ( this ) ;
94
+ let data = wc . num || wc . getAttribute ( 'num' )
95
+ expect ( parseInt ( data , 10 ) ) . to . eql ( 42 )
96
+ } )
97
+
98
+ it ( 'will pass string data as either an attribute or a property' , async function ( ) {
99
+ this . weight = 3
100
+ const { wc } = await renderComponentWithProperties . call ( this ) ;
101
+ let data = wc . str || wc . getAttribute ( 'str' )
102
+ expect ( data ) . to . eql ( 'riot' )
103
+ } )
104
+ } )
105
+
106
+ describe ( 'events' , async function ( ) {
107
+ it ( 'can imperatively listen to a DOM event dispatched by a Custom Element' , async function ( ) {
108
+ this . weight = 3
109
+ const { wc } = await renderComponentWithImperativeEvent . call ( this )
110
+ expect ( wc ) . to . exist
111
+ let handled = document . querySelector ( '#handled' )
112
+ expect ( handled . textContent ) . to . eql ( 'false' )
113
+ wc . click ( )
114
+ expect ( handled . textContent ) . to . eql ( 'true' )
115
+ } )
116
+ } )
117
+ } )
118
+ }
0 commit comments