import * as sinon from 'sinon'; import test from 'ava'; import { EventEmitter2 } from 'eventemitter2'; import { FetchEnd } from 'hint/dist/src/lib/types'; import { Engine } from 'hint'; import * as HTMLParser from '../src/parser'; import { HTMLEvents, HTMLParse } from '../src/parser'; test('If `fetch::end::html` is received, then the code should be parsed and the `parse::end::html` event emitted', async (t) => { const sandbox = sinon.createSandbox(); const engine: Engine = new EventEmitter2({ delimiter: '::', maxListeners: 0, wildcard: true }) as Engine; const code = '
Test
'; new HTMLParser.default(engine); // eslint-disable-line const engineEmitAsyncSpy = sandbox.spy(engine, 'emitAsync'); await engine.emitAsync('fetch::end::html', { resource: 'http://example.com/test.html', response: { body: { content: code }, mediaType: 'text/html', url: 'test.html' } } as FetchEnd); const args = engineEmitAsyncSpy.args; const document = (args[2][1] as HTMLParse).document; const div = document.querySelectorAll('div')[0]; const div2 = document.querySelectorAll('body > div')[0]; const location = div.getLocation(); let id = null; for (let i = 0; i < div.attributes.length; i++) { if (div.attributes[i].name === 'id') { id = div.attributes[i]; break; } } t.is(args[1][0], 'parse::start::html'); t.is(args[2][0], 'parse::end::html'); t.is((args[2][1] as HTMLParse).resource, 'http://example.com/test.html'); t.is((args[2][1] as HTMLParse).html, code); t.is(document.pageHTML(), '
Test
'); t.is(div.outerHTML, '
Test
'); t.is(div.nodeName.toLowerCase(), 'div'); t.is(div.getAttribute('id'), 'test'); t.is(location && location.line, 0); t.is(location && location.column, 15); t.is(id && id.value, 'test'); t.true(div.isSame(div2)); sandbox.restore(); });