import 'mocha'; import mochaDom = require('mocha-jsdom'); import { expect } from 'chai'; import * as fs from 'fs'; import * as path from 'path'; import { JSDOM } from 'jsdom'; import { snapshot, rebuild } from '../src'; const htmlFolder = path.join(__dirname, 'html'); const htmls = fs.readdirSync(htmlFolder).map(filePath => { return { filePath, content: fs.readFileSync(path.resolve(htmlFolder, filePath), 'utf-8'), }; }); describe('integration tests', () => { mochaDom({ url: 'http://localhost' }); for (const html of htmls) { it('[html file]:' + html.filePath, () => { const dom = new JSDOM(html.content); const snap = snapshot(dom.window.document); const rebuildDom = rebuild(snap); const htmlStr = dom.window.document.documentElement.outerHTML .replace(/') .replace(//g, '') .replace(/<\/script>/g, ''); expect((rebuildDom as Document).documentElement.outerHTML).to.equal( htmlStr, ); }); } it('will snapshot document type', () => { const raw = ''; const dom = new JSDOM(raw); const snap = snapshot(dom.window.document); expect(snap).to.deep.equal({ type: 0, childNodes: [ { type: 2, tagName: 'html', attributes: {}, childNodes: [ { type: 2, tagName: 'head', attributes: {}, childNodes: [], id: 3, }, { type: 2, tagName: 'body', attributes: {}, childNodes: [], id: 4, }, ], id: 2, }, ], id: 1, }); }); it('will not throw error with invalid attribute', () => { const raw = ``; const dom = new JSDOM(raw); expect(() => rebuild(snapshot(dom.window.document))).not.to.throw(); }); it('will inline text input value', () => { const raw = ''; const dom = new JSDOM(raw); dom.window.document.querySelector('input').value = '1'; const rebuildDom = rebuild(snapshot(dom.window.document)); expect((rebuildDom as Document).querySelector('input').outerHTML).to.equal( '', ); }); it('will inline radio input value', () => { const raw = ''; const dom = new JSDOM(raw); dom.window.document.querySelector('input').checked = true; const rebuildDom = rebuild(snapshot(dom.window.document)); expect((rebuildDom as Document).querySelector('input').outerHTML).to.equal( '', ); }); it('will inline checkbox input value', () => { const raw = ''; const dom = new JSDOM(raw); dom.window.document.querySelector('input').checked = true; const rebuildDom = rebuild(snapshot(dom.window.document)); expect((rebuildDom as Document).querySelector('input').outerHTML).to.equal( '', ); }); it('will inline textarea value into text node', () => { const raw = ''; const dom = new JSDOM(raw); dom.window.document.querySelector('textarea').value = '1234'; const rebuildDom = rebuild(snapshot(dom.window.document)); expect( (rebuildDom as Document).querySelector('textarea').outerHTML, ).to.equal(''); }); it('will inline options state', () => { const raw = ` `; const dom = new JSDOM(raw); dom.window.document.querySelector('select').value = '2'; const rebuildDom = rebuild(snapshot(dom.window.document)); expect((rebuildDom as Document).querySelector('select').outerHTML).to.equal( ``, ); }); });