From 1b9a3686537170dcb0a1d03535d7537e7791ca73 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Fri, 4 May 2018 00:44:54 +0200 Subject: [PATCH] Fix substitution evaluation (fixes #143) --- src/TemplateTag/TemplateTag.js | 2 +- src/TemplateTag/TemplateTag.test.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/TemplateTag/TemplateTag.js b/src/TemplateTag/TemplateTag.js index 000541f..4598dcf 100644 --- a/src/TemplateTag/TemplateTag.js +++ b/src/TemplateTag/TemplateTag.js @@ -77,7 +77,7 @@ export default class TemplateTag { substitutions.shift(), resultSoFar, ); - return resultSoFar + substitution + remainingPart; + return ''.concat(resultSoFar, substitution, remainingPart); } /** diff --git a/src/TemplateTag/TemplateTag.test.js b/src/TemplateTag/TemplateTag.test.js index d06cead..48e6b9d 100644 --- a/src/TemplateTag/TemplateTag.test.js +++ b/src/TemplateTag/TemplateTag.test.js @@ -121,3 +121,25 @@ test('supports passing string as a first argument', () => { expect(raw).toBe('FOO BAR\n 500'); expect(onSubstitutionCalls).toBe(0); }); + +test('transforms substitutions to string as per spec', () => { + const get = jest + .fn() + .mockImplementationOnce((target, prop) => { + expect(prop).toBe(Symbol.toPrimitive); + }) + .mockImplementationOnce((target, prop) => { + expect(prop).toBe('toString'); + }) + .mockImplementationOnce((target, prop) => { + expect(prop).toBe('valueOf'); + return () => 42; + }); + + const val = new Proxy({}, { get }); + const tag = new TemplateTag(); + const result = tag`foo ${val} bar`; + + expect(get).toHaveBeenCalledTimes(3); + expect(result).toBe('foo 42 bar'); +});