diff --git a/package.json b/package.json index 8ab3b93b..5717269c 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "eslint": "^3.19.0", "eslint-config-airbnb-base": "^11.2.0", "eslint-plugin-import": "^2.3.0", - "jest": "^21.1.0", + "jest": "21.3.0-beta.3", "rimraf": "^2.6.1", "standard-version": "^4.2.0", "uglify-js": "^3.0.8", diff --git a/src/scripts/ace-extraction.test.js b/src/scripts/ace-extraction.test.js index 403d8bff..b3932b5b 100644 --- a/src/scripts/ace-extraction.test.js +++ b/src/scripts/ace-extraction.test.js @@ -1,35 +1,46 @@ /** - * @jest-environment jsdom + * @jest-environment ./tests/jest-env-puppeteer.js */ /* eslint-env browser, jest */ 'use strict'; -require('./ace-extraction'); +const $ = require('../../tests/browser-test-utils'); -// Mock function defined in other modules -window.daisy.epub = { - createCFI: jest.fn(), -}; -const mockH5O = jest.fn(); -mockH5O.mockReturnValue({ - asHTML: jest.fn(), +beforeAll(async () => { + await $.loadXHTMLPage(); + await $.injectScripts([require.resolve('./ace-extraction.js')]); + await $.injectJestMock(); + await global.page.evaluate(() => { + const mockH5O = window.mock.fn(); + mockH5O.mockReturnValue({ + asHTML: window.mock.fn(), + }); + window.HTML5Outline = mockH5O; + window.daisy.epub = { + createCFI: window.mock.fn(), + }; + window.daisy.epub.createCFI.mockReturnValue('42'); + }); }); -window.HTML5Outline = mockH5O; - -const ace = window.daisy.ace; -const epub = window.daisy.epub; -beforeEach(() => { - epub.createCFI.mockReturnValue('42'); -}); -afterEach(() => { - epub.createCFI.mockClear(); +afterAll(async () => { + await $.closePage(); }); -test('createReport', () => { - const report = {}; - ace.createReport(report); +async function run(name, markup) { + return global.page.evaluate(($name, $markup) => { + document.body.innerHTML = $markup; + return window.ace[$name](); + }, name, markup); +} + +test('createReport', async () => { + const report = await global.page.evaluate(() => { + const ret = {}; + window.ace.createReport(ret); + return ret; + }); expect(report.outlines).toBeDefined(); expect(report.data).toBeDefined(); expect(report.data).toEqual({}); @@ -39,40 +50,32 @@ test('createReport', () => { }); describe('extracting audio', () => { - test('simple audio', () => { - const html = ` - `; - document.body.innerHTML = html; - const results = ace.getAudios(); + test('simple audio', async () => { + const results = await run('getAudios', ''); expect(results.length).toBe(1); - expect(results[0].html).toBe(html.trim()); + expect(results[0].html).toBeDefined(); expect(results[0].id).toBe('foo'); expect(results[0].controls).toBeTruthy(); expect(results[0].tracks).toEqual([]); }); - test('audio with no id', () => { - document.body.innerHTML = ` -