From 1a631ba7a48874f6ecbbe636863f0daeb8867edd Mon Sep 17 00:00:00 2001 From: Johannes Burghardt Date: Mon, 14 Oct 2019 09:39:05 +0200 Subject: [PATCH] update escape-tests --- test/escape-test.js | 72 +++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/test/escape-test.js b/test/escape-test.js index 22ad365..dbe0929 100644 --- a/test/escape-test.js +++ b/test/escape-test.js @@ -5,59 +5,61 @@ import { serialize, toScript, fromScript } from '..'; describe('escaping', () => { 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({