16
16
import test from 'ava' ;
17
17
import { DocumentNodeProto , NodeProto , TreeProto } from '../src/protos.js' ;
18
18
import { getTagId } from '../src/htmltagenum.js' ;
19
- import { renderAst } from '../src/index.js' ;
19
+ import { renderAstDocument , renderAstNodes } from '../src/index.js' ;
20
20
21
21
/**
22
22
* Helper for generating NodeProtos.
@@ -54,7 +54,7 @@ function treeProto(
54
54
55
55
test ( 'should have no effect with empty instructions' , ( t ) => {
56
56
const ast = treeProto ( ) ;
57
- t . deepEqual ( renderAst ( ast , { } ) , ast ) ;
57
+ t . deepEqual ( renderAstDocument ( ast , { } ) , ast ) ;
58
58
} ) ;
59
59
60
60
test ( 'should render provided instruction' , ( t ) => {
@@ -70,7 +70,7 @@ test('should render provided instruction', (t) => {
70
70
const expected = treeProto ( [
71
71
h ( 'amp-element' , { rendered : '' } , [ 'answer: 42' ] ) ,
72
72
] ) ;
73
- t . deepEqual ( renderAst ( ast , instructions ) , expected ) ;
73
+ t . deepEqual ( renderAstDocument ( ast , instructions ) , expected ) ;
74
74
} ) ;
75
75
76
76
test ( 'should return the error if a single instruction throws' , ( t ) => {
@@ -81,7 +81,7 @@ test('should return the error if a single instruction throws', (t) => {
81
81
} ;
82
82
83
83
const ast = treeProto ( h ( 'amp-fail' ) ) ;
84
- t . throws ( ( ) => renderAst ( ast , instructions ) , { message : / a m p - f a i l / } ) ;
84
+ t . throws ( ( ) => renderAstDocument ( ast , instructions ) , { message : / a m p - f a i l / } ) ;
85
85
} ) ;
86
86
87
87
test ( 'should only throw the first error even if multiple would throw' , ( t ) => {
@@ -97,10 +97,10 @@ test('should only throw the first error even if multiple would throw', (t) => {
97
97
} ,
98
98
} ;
99
99
100
- const ast = treeProto (
101
- h ( 'amp-success' , { } , [ h ( 'amp-fail1' ) , h ( 'amp-fail2' ) , h ( 'amp-fail2' ) ] )
102
- ) ;
103
- t . throws ( ( ) => renderAst ( ast , instructions ) , { message : / a m p - f a i l 1 / } ) ;
100
+ const nodes = h ( 'amp-success' , { } , [ h ( 'amp-fail1' ) , h ( 'amp-fail2' ) ] ) ;
101
+ const ast = treeProto ( nodes ) ;
102
+ t . throws ( ( ) => renderAstNodes ( [ nodes ] , instructions ) , { message : / a m p - f a i l 1 / } ) ;
103
+ t . throws ( ( ) => renderAstDocument ( ast , instructions ) , { message : / a m p - f a i l 1 / } ) ;
104
104
} ) ;
105
105
106
106
test ( 'should allow a custom error handler' , ( t ) => {
@@ -122,7 +122,7 @@ test('should allow a custom error handler', (t) => {
122
122
123
123
const errors = [ ] ;
124
124
const handleError = ( tagName ) => errors . push ( tagName ) ;
125
- renderAst ( ast , instructions , { handleError} ) ;
125
+ renderAstDocument ( ast , instructions , { handleError} ) ;
126
126
t . deepEqual ( errors , [ 'amp-fail1' , 'amp-fail1' , 'amp-fail2' ] ) ;
127
127
} ) ;
128
128
@@ -136,7 +136,7 @@ test('should be unaffected by async modifications', async (t) => {
136
136
} ;
137
137
138
138
const ast = treeProto ( [ h ( 'amp-element' ) ] ) ;
139
- const rendered = renderAst ( ast , instructions ) ;
139
+ const rendered = renderAstDocument ( ast , instructions ) ;
140
140
await new Promise ( ( r ) => setTimeout ( r ) ) ; // Waits a macrotask.
141
141
142
142
t . deepEqual ( rendered , ast ) ;
@@ -150,18 +150,18 @@ test('should not render elements within templates', (t) => {
150
150
} ;
151
151
152
152
const ast = treeProto ( h ( 'template' , { } , [ h ( 'amp-element' ) ] ) ) ;
153
- const rendered = renderAst ( ast , instructions ) ;
153
+ const rendered = renderAstDocument ( ast , instructions ) ;
154
154
155
155
t . deepEqual ( rendered , ast ) ;
156
156
} ) ;
157
157
158
158
test ( 'should conserve quirks_mode and root' , ( t ) => {
159
159
const tree : [ DocumentNodeProto ] = [ { tagid : 92 , children : [ ] } ] ;
160
160
let ast : TreeProto = { root : 42 , quirks_mode : true , tree} ;
161
- t . deepEqual ( renderAst ( ast , { } ) , ast ) ;
161
+ t . deepEqual ( renderAstDocument ( ast , { } ) , ast ) ;
162
162
163
163
ast = { root : 7 , quirks_mode : false , tree} ;
164
- t . deepEqual ( renderAst ( ast , { } ) , ast ) ;
164
+ t . deepEqual ( renderAstDocument ( ast , { } ) , ast ) ;
165
165
} ) ;
166
166
167
167
test ( 'should set tagids of element nodes' , ( t ) => {
@@ -172,7 +172,7 @@ test('should set tagids of element nodes', (t) => {
172
172
}
173
173
174
174
const inputAst : TreeProto = treeProto ( h ( 'amp-list' ) ) ;
175
- let renderedAst = renderAst ( inputAst , { 'amp-list' : buildAmpList } ) ;
175
+ let renderedAst = renderAstDocument ( inputAst , { 'amp-list' : buildAmpList } ) ;
176
176
177
177
t . deepEqual (
178
178
renderedAst ,
@@ -191,7 +191,7 @@ test('should set num_terms of text nodes', (t) => {
191
191
}
192
192
193
193
const inputAst : TreeProto = treeProto ( [ h ( 'amp-list' ) ] ) ;
194
- let result = renderAst ( inputAst , { 'amp-list' : buildAmpList } ) ;
194
+ let result = renderAstDocument ( inputAst , { 'amp-list' : buildAmpList } ) ;
195
195
196
196
t . deepEqual (
197
197
result ,
@@ -200,3 +200,33 @@ test('should set num_terms of text nodes', (t) => {
200
200
t . is ( result . tree [ 0 ] . children [ 0 ] ?. [ 'children' ] ?. [ 0 ] ?. num_terms , 2 ) ;
201
201
t . is ( result . tree [ 0 ] . children [ 0 ] ?. [ 'children' ] ?. [ 1 ] ?. num_terms , 3 ) ;
202
202
} ) ;
203
+
204
+ test ( 'should render node partials' , ( t ) => {
205
+ function buildAmpList ( element ) {
206
+ const doc = element . ownerDocument ;
207
+ element . appendChild ( doc . createTextNode ( 'element text' ) ) ;
208
+ }
209
+
210
+ const inputNode : NodeProto = h ( 'amp-list' ) ;
211
+ const renderedNode : NodeProto = h ( 'amp-list' , { } , [ 'element text' ] ) ;
212
+
213
+ let resultSingle = renderAstNodes ( [ inputNode ] , { 'amp-list' : buildAmpList } ) ;
214
+ t . deepEqual ( resultSingle , [ renderedNode ] ) ;
215
+
216
+ let resultDouble = renderAstNodes ( [ inputNode , inputNode ] , {
217
+ 'amp-list' : buildAmpList ,
218
+ } ) ;
219
+ t . deepEqual ( resultDouble , [ renderedNode , renderedNode ] ) ;
220
+ } ) ;
221
+
222
+ test ( 'should deeply render node partials' , ( t ) => {
223
+ function buildAmpEl ( element : Element ) {
224
+ element . setAttribute ( 'rendered' , '' ) ;
225
+ }
226
+
227
+ const inputAst : NodeProto = h ( 'amp-el' , { } , [ h ( 'amp-el' ) ] ) ;
228
+ let result = renderAstNodes ( [ inputAst ] , { 'amp-el' : buildAmpEl } ) ;
229
+ t . deepEqual ( result , [
230
+ h ( 'amp-el' , { rendered : '' } , [ h ( 'amp-el' , { rendered : '' } ) ] ) ,
231
+ ] ) ;
232
+ } ) ;
0 commit comments