From b7cea9820f025b42df6a0fcfd95020267a7c8d36 Mon Sep 17 00:00:00 2001 From: Douglas Wade Date: Sun, 31 Dec 2023 15:50:00 -0800 Subject: [PATCH 1/2] feat(NOBUG): multiple interpolations in a single attribute triggers warning --- packages/core/src/api/render.ts | 13 +++++++++++-- packages/core/tst/api/render.test.ts | 13 ++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/core/src/api/render.ts b/packages/core/src/api/render.ts index ffea1f15..39c9d623 100644 --- a/packages/core/src/api/render.ts +++ b/packages/core/src/api/render.ts @@ -47,7 +47,9 @@ const renderToString = ( const placeholder = uuid(); let suffix = ''; - if (!strings[i + 1].startsWith('"')) { + if (!strings[i + 1].includes('"')) { + throw new Error('Tybalt currently only supports one reactive per attribute. Please consolidate.'); + } else if (!strings[i + 1].startsWith('"')) { suffix = strings[i + 1].split('"')[0]; } @@ -85,13 +87,20 @@ const renderToString = ( if (Array.isArray(key)) { const children = key.map((key: HtmlTemplate) => renderToString(key, eventPlaceholders, setAttributePlaceholders)).join(''); return `${prev}${curr}${children}`; - } + } if (key?.addListener) { if (key?.value.strings && key?.value.keys) { return `${prev}${curr}${renderToString(key, eventPlaceholders, setAttributePlaceholders)}`; } + if (Array.isArray(key.value)) { + const children = key + .value + .map((templ: HtmlTemplate) => renderToString(templ, eventPlaceholders, setAttributePlaceholders)).join(''); + return `${prev}${curr}${children}`; + } + return `${prev}${curr}${key.value}`; } diff --git a/packages/core/tst/api/render.test.ts b/packages/core/tst/api/render.test.ts index 32fc5d10..3696b4f2 100644 --- a/packages/core/tst/api/render.test.ts +++ b/packages/core/tst/api/render.test.ts @@ -105,5 +105,16 @@ describe('render', () => { const wrapper = render(template)[0]; expect(wrapper.outerHTML).toBe(``); - }) + }); + + it('should not support multiple interpolations in a single attribute', () => { + const firstAttributeValue = reactive('bar'); + const secondAttributeValue = reactive('baz'); + + const template = html`
`; + + expect(() => { + render(template) + }).toThrow('Tybalt currently only supports one reactive per attribute. Please consolidate.') + }); }); \ No newline at end of file From 71d8c39e5c73b28967e4e642c670d5da753ae11e Mon Sep 17 00:00:00 2001 From: Douglas Wade Date: Sun, 31 Dec 2023 16:10:21 -0800 Subject: [PATCH 2/2] chore(NOBUG): Increase code coverage --- packages/core/tst/api/render.test.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/core/tst/api/render.test.ts b/packages/core/tst/api/render.test.ts index 3696b4f2..4b4827af 100644 --- a/packages/core/tst/api/render.test.ts +++ b/packages/core/tst/api/render.test.ts @@ -117,4 +117,16 @@ describe('render', () => { render(template) }).toThrow('Tybalt currently only supports one reactive per attribute. Please consolidate.') }); -}); \ No newline at end of file + + it('should emit a warning if a placeholder is unexpectedly not found', () => { + const listener = jest.fn(); + const template = html``; + + jest.spyOn(Element.prototype, 'querySelector').mockImplementation(() => null); + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => ''); + render(template)[0]; + + expect(warnSpy).toHaveBeenCalledTimes(1); + expect(warnSpy.mock.calls[0][0].startsWith('expected to find element with selector ')).toBeTruthy(); + }); +});