From c1a04f3d65c32dc30a2f60eaee7a8e41a6fdeb4b Mon Sep 17 00:00:00 2001 From: Joachim Viide Date: Thu, 23 Jan 2020 22:54:03 +0200 Subject: [PATCH 1/2] Add a test for forcing subtree staticness --- test/statics-caching.test.mjs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/statics-caching.test.mjs b/test/statics-caching.test.mjs index 11c9820..5e314f8 100644 --- a/test/statics-caching.test.mjs +++ b/test/statics-caching.test.mjs @@ -89,4 +89,25 @@ describe('htm', () => { expect(html`
`).toBe(3); }); }); + + describe('the h function should be able to modify `this[0]`', () => { + test('should be able to force subtrees to be static', () => { + function wrapH(h) { + return function(type, props, ...children) { + if (props['@static']) { + this[0] &= ~3; + } + return h(type, props, ...children); + }; + } + + const html = htm.bind(wrapH(h)); + const x = () => html`
${'a'}
`; + const a = x(); + const b = x(); + expect(a).toEqual({ tag: 'div', props: { '@static': true }, children: ['a'] }); + expect(b).toEqual({ tag: 'div', props: { '@static': true }, children: ['a'] }); + expect(a).toBe(b); + }); + }); }); From 55ed1664d91a84e5947389ab5f25d52f617db99c Mon Sep 17 00:00:00 2001 From: Joachim Viide Date: Thu, 23 Jan 2020 23:11:09 +0200 Subject: [PATCH 2/2] Add a test for forcing the subtree to be static via a special flag --- test/statics-caching.test.mjs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/test/statics-caching.test.mjs b/test/statics-caching.test.mjs index 5e314f8..78bb627 100644 --- a/test/statics-caching.test.mjs +++ b/test/statics-caching.test.mjs @@ -91,16 +91,20 @@ describe('htm', () => { }); describe('the h function should be able to modify `this[0]`', () => { - test('should be able to force subtrees to be static', () => { - function wrapH(h) { - return function(type, props, ...children) { - if (props['@static']) { - this[0] &= ~3; - } - return h(type, props, ...children); - }; - } - + function wrapH(h) { + return function(type, props, ...children) { + if (type === '@static') { + this[0] &= ~3; + return children; + } + if (props['@static']) { + this[0] &= ~3; + } + return h(type, props, ...children); + }; + } + + test('should be able to force subtrees to be static via a prop', () => { const html = htm.bind(wrapH(h)); const x = () => html`
${'a'}
`; const a = x(); @@ -109,5 +113,15 @@ describe('htm', () => { expect(b).toEqual({ tag: 'div', props: { '@static': true }, children: ['a'] }); expect(a).toBe(b); }); + + test('should be able to force subtrees to be static via a special tag', () => { + const html = htm.bind(wrapH(h)); + const x = () => html`<@static>${'a'}`; + const a = x(); + const b = x(); + expect(a).toEqual(['a']); + expect(b).toEqual(['a']); + expect(a).toBe(b); + }); }); });