|
1 | 1 | import {expect} from '@open-wc/testing' |
2 | 2 | import {TemplateInstance} from '../src/template-instance' |
3 | 3 | import {NodeTemplatePart} from '../src/node-template-part' |
4 | | -import {propertyIdentityOrBooleanAttribute, createProcessor} from '../src/processors' |
| 4 | +import {InnerTemplatePart} from '../src/inner-template-part' |
| 5 | +import {processPropertyIdentity, propertyIdentityOrBooleanAttribute, createProcessor} from '../src/processors' |
5 | 6 |
|
6 | 7 | describe('template-instance', () => { |
7 | 8 | it('applies data to templated text nodes', () => { |
@@ -354,5 +355,31 @@ describe('template-instance', () => { |
354 | 355 | expect(processCallCount).to.equal(2) |
355 | 356 | }) |
356 | 357 | }) |
| 358 | + |
| 359 | + describe('handling InnerTemplatePart', () => { |
| 360 | + it('makes outer state available to inner parts', () => { |
| 361 | + const processor = createProcessor((part, value, state) => { |
| 362 | + if (part instanceof InnerTemplatePart && part.directive === 'if') { |
| 363 | + if (typeof state === 'object' && (state as Record<string, unknown>)[part.expression]) { |
| 364 | + part.replace(new TemplateInstance(part.template, state, processor)) |
| 365 | + } else { |
| 366 | + part.replace() |
| 367 | + } |
| 368 | + } else { |
| 369 | + processPropertyIdentity(part, value) |
| 370 | + } |
| 371 | + }) |
| 372 | + const template = Object.assign(document.createElement('template'), { |
| 373 | + innerHTML: '{{x}}<template directive="if" expression="y">{{y}}</template>', |
| 374 | + }) |
| 375 | + |
| 376 | + const root = document.createElement('div') |
| 377 | + root.appendChild(new TemplateInstance(template, {x: 'x', y: 'y'}, processor)) |
| 378 | + expect(root.innerHTML).to.equal('xy') |
| 379 | + |
| 380 | + root.replaceChildren(new TemplateInstance(template, {x: 'x', y: false}, processor)) |
| 381 | + expect(root.innerHTML).to.equal('x') |
| 382 | + }) |
| 383 | + }) |
357 | 384 | }) |
358 | 385 | }) |
0 commit comments