diff --git a/src/index.js b/src/index.js index 824a5fe..f3374ce 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,7 @@ const RIGHT = '-->'; const ENCODE = [ ['&', '&'], ['>', '>'], + [' { it('escapes', () => { - const html = serialize('foo', '', { foo: '', bar: '>' }); + const html = serialize('foo', '', { foo: '', bar: '>', baz: ' ({})) - .describe('with fromScript', () => { - it('loads the escaped content correctly', () => { - const html = toScript({ a: 'b' }, { foo: '', bar: '>', baz: '&' }); - const $ = cheerio.load(html); + .withGlobal('document', () => ({})) + .describe('with fromScript', () => { + it('loads the escaped content correctly', () => { + const html = toScript({ a: 'b' }, { foo: '', bar: '>', baz: '&', foobar: ' ({ innerHTML: $($('script')[0]).html() }); + global.document.querySelector = () => ({ innerHTML: $($('script')[0]).html() }); - const res = fromScript({ - a: 'b', - }); - - assert.isObject(res); + const res = fromScript({ + a: 'b', + }); - assert.equal(res.foo, ''); - assert.equal(res.bar, '>'); - assert.equal(res.baz, '&'); - }); + assert.isObject(res); - it('escapes multiple times the same, with interleaved decoding', () => { - const makeHTML = () => toScript({ attr: 'key' }, { - props: 'yay', - needsEncoding: '" > ', // "needsEncoding" is necessary + assert.equal(res.foo, ''); + assert.equal(res.bar, '>'); + assert.equal(res.baz, '&'); + assert.equal(res.foobar, ' { + const makeHTML = () => toScript({ attr: 'key' }, { + props: 'yay', + needsEncoding: '" > ', // "needsEncoding" is necessary + }); + const script1 = makeHTML(); + const script2 = makeHTML(); + assert.equal(script1, script2, 'two successive toScripts result in identical HTML'); - global.document.querySelector = () => ({ innerHTML: $($('script')[0]).html() }); + const $ = cheerio.load(script1); - const res = fromScript({ attr: 'key' }); + global.document.querySelector = () => ({ innerHTML: $($('script')[0]).html() }); - const script3 = makeHTML(); - assert.equal( - script1, - script3, - 'third toScript after a fromScript call results in the same HTML', - ); + const res = fromScript({ attr: 'key' }); - assert.isObject(res); + const script3 = makeHTML(); + assert.equal( + script1, + script3, + 'third toScript after a fromScript call results in the same HTML', + ); - assert.equal(res.props, 'yay'); + assert.isObject(res); + + assert.equal(res.props, 'yay'); + }); }); - }); it('escapes quotes and fixes data attributes', () => { const markup = toScript({