Skip to content

Commit e1a8a09

Browse files
committed
Failing tests: not updating
1 parent c468223 commit e1a8a09

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

src/processors.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type {TemplatePart, TemplateTypeInit} from './types.js'
22
import {TemplateInstance} from './template-instance.js'
3-
import {AttributeTemplatePart} from './attribute-template-part.js'
43
import {InnerTemplatePart} from './inner-template-part.js'
4+
import {AttributeTemplatePart} from './attribute-template-part.js'
55

66
type PartProcessor = (part: TemplatePart, value: unknown, state: unknown) => void
77

@@ -21,16 +21,22 @@ export function createProcessor(processPart: PartProcessor): TemplateTypeInit {
2121
}
2222
}
2323

24-
export function processPropertyIdentity(part: TemplatePart, value: unknown, state: unknown): void {
24+
export function processPropertyIdentity(part: TemplatePart, value: unknown, state?: unknown): void {
2525
if (part instanceof InnerTemplatePart) {
26-
part.template.content.replaceChildren(new TemplateInstance(part.template, state))
26+
const instance = new TemplateInstance(part.template, state)
27+
part.template.content.replaceChildren(instance)
2728
} else {
2829
part.value = value instanceof Node ? value : String(value)
2930
}
3031
}
3132

32-
export function processBooleanAttribute(part: TemplatePart, value: unknown): boolean {
33-
if (
33+
export function processBooleanAttribute(part: TemplatePart, value: unknown, state?: unknown): boolean {
34+
if (part instanceof InnerTemplatePart) {
35+
const instance = new TemplateInstance(part.template, state, propertyIdentityOrBooleanAttribute)
36+
part.template.content.replaceChildren(instance)
37+
38+
return true
39+
} else if (
3440
typeof value === 'boolean' &&
3541
part instanceof AttributeTemplatePart &&
3642
typeof part.element[part.attributeName as keyof Element] === 'boolean'
@@ -44,7 +50,7 @@ export function processBooleanAttribute(part: TemplatePart, value: unknown): boo
4450
export const propertyIdentity = createProcessor(processPropertyIdentity)
4551
export const propertyIdentityOrBooleanAttribute = createProcessor(
4652
(part: TemplatePart, value: unknown, state: unknown) => {
47-
if (!processBooleanAttribute(part, value)) {
53+
if (!processBooleanAttribute(part, value, state)) {
4854
processPropertyIdentity(part, value, state)
4955
}
5056
},

test/template-instance.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,32 @@ describe('template-instance', () => {
2828
root.appendChild(instance)
2929
expect(root.innerHTML).to.equal(`<div>Hello world</div>`)
3030
})
31-
it('applies data to nested templated element nodes', () => {
31+
it('applies data to nested templated element nodes with the default processPropertyIdentity', () => {
3232
const root = document.createElement('div')
3333
const template = Object.assign(document.createElement('template'), {
3434
innerHTML: '<template><div>{{x}}</div></template>',
3535
})
36-
root.appendChild(new TemplateInstance(template, {x: 'Hello world'}))
36+
const instance = new TemplateInstance(template, {x: 'Hello world'})
3737

38+
root.appendChild(instance)
3839
expect(root.innerHTML).to.equal('<template><div>Hello world</div></template>')
3940
expect(template.innerHTML).to.equal('<template><div>{{x}}</div></template>')
41+
instance.update({x: 'Goodbye world'})
42+
expect(root.innerHTML).to.equal('<template><div>Goodbye world</div></template>')
43+
expect(template.innerHTML).to.equal('<template><div>{{x}}</div></template>')
44+
})
45+
it('applies data to nested templated element nodes with propertyIdentityOrBooleanAttribute', () => {
46+
const template = Object.assign(document.createElement('template'), {
47+
innerHTML: '<template><div hidden="{{hidden}}"></div></template>',
48+
})
49+
const instance = new TemplateInstance(template, {hidden: true}, propertyIdentityOrBooleanAttribute)
50+
51+
const root = document.createElement('div')
52+
root.appendChild(instance)
53+
expect(root.innerHTML).to.equal('<template><div hidden=""></div></template>')
54+
expect(template.innerHTML).to.equal('<template><div hidden="{{hidden}}"></div></template>')
55+
instance.update({hidden: false})
56+
expect(root.innerHTML).to.equal('<template><div></div></template>')
4057
})
4158
it('applies data to templated DocumentFragment nodes', () => {
4259
const template = document.createElement('template')
@@ -367,7 +384,7 @@ describe('template-instance', () => {
367384
part.replace()
368385
}
369386
} else {
370-
processPropertyIdentity(part, value, state)
387+
processPropertyIdentity(part, value)
371388
}
372389
})
373390
const template = Object.assign(document.createElement('template'), {
@@ -382,12 +399,12 @@ describe('template-instance', () => {
382399
expect(root.innerHTML).to.equal('x')
383400
})
384401

385-
it('makes outer state available to InnerTemplatePart elements without attributes', () => {
402+
it('makes outer state available to InnerTemplatePart elements without attributes with default propertyIdentity processing', () => {
386403
let callCount = 0
387-
const processor = createProcessor((part, value, state) => {
404+
const processor = createProcessor((part, value) => {
388405
if (part instanceof InnerTemplatePart && value === part.expression) {
389406
callCount += 1
390-
processPropertyIdentity(part, value, state)
407+
processPropertyIdentity(part, value)
391408
}
392409
})
393410
const template = Object.assign(document.createElement('template'), {

0 commit comments

Comments
 (0)