-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix position offset computing for window (#920)
Fix position offset computing for window
- Loading branch information
Showing
7 changed files
with
303 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/** | ||
* @jest-environment ./source/jest-puppeteer-environment.js | ||
*/ | ||
|
||
const bootstrap = async () => { | ||
const page = await global.__BROWSER__.newPage(); | ||
const scripts = [ | ||
'./node_modules/react/umd/react.development.js', | ||
'./node_modules/react-dom/umd/react-dom.development.js', | ||
'./dist/umd/react-virtualized.js', | ||
]; | ||
|
||
for (const path of scripts) { | ||
await page.addScriptTag({path}); | ||
} | ||
|
||
return page; | ||
}; | ||
|
||
const renderWindowScroller = ({scrollElement}) => { | ||
const {render} = window.ReactDOM; | ||
const {createElement} = window.React; | ||
const {WindowScroller} = window.ReactVirtualized; | ||
|
||
const container = document.createElement('div'); | ||
container.id = 'container'; | ||
container.style.margin = '100px'; | ||
container.style.padding = '50px'; | ||
document.body.appendChild(container); | ||
document.body.style.margin = 0; | ||
|
||
if (scrollElement === 'container') { | ||
container.style.width = '100%'; | ||
container.style.height = '100%'; | ||
container.style.overflow = 'auto'; | ||
} | ||
|
||
render( | ||
createElement( | ||
WindowScroller, | ||
{ | ||
scrollElement: scrollElement === 'container' ? container : window, | ||
onScroll: window.scrollFn, | ||
onResize: window.resizeFn, | ||
}, | ||
() => createElement('div', {style: {width: 2000, height: 3000}}), | ||
), | ||
container, | ||
); | ||
}; | ||
|
||
const delay = time => new Promise(resolve => setTimeout(resolve, time)); | ||
|
||
test('save position after resize and then scroll in window', async () => { | ||
const page = await bootstrap(); | ||
const scrollFn = jest.fn(); | ||
const resizeFn = jest.fn(); | ||
await page.exposeFunction('scrollFn', scrollFn); | ||
await page.exposeFunction('resizeFn', resizeFn); | ||
|
||
await page.setViewport({width: 400, height: 600}); | ||
await page.evaluate(renderWindowScroller, {scrollElement: 'window'}); | ||
|
||
// scroll more than viewport | ||
await page.evaluate(() => window.scrollTo(610, 830)); | ||
await delay(100); | ||
// resize a bit container/window | ||
await page.setViewport({width: 300, height: 500}); | ||
await delay(100); | ||
// scroll again | ||
await page.evaluate(() => window.scrollTo(620, 840)); | ||
await delay(100); | ||
|
||
await page.close(); | ||
|
||
expect(scrollFn.mock.calls).toEqual([ | ||
[{scrollLeft: 610 - 150, scrollTop: 830 - 150}], | ||
[{scrollLeft: 620 - 150, scrollTop: 840 - 150}], | ||
]); | ||
expect(resizeFn.mock.calls).toEqual([[{width: 300, height: 500}]]); | ||
}); | ||
|
||
test('save position after resize and then scroll in container', async () => { | ||
const page = await bootstrap(); | ||
const scrollFn = jest.fn(); | ||
const resizeFn = jest.fn(); | ||
await page.exposeFunction('scrollFn', scrollFn); | ||
await page.exposeFunction('resizeFn', resizeFn); | ||
|
||
await page.setViewport({width: 400, height: 600}); | ||
await page.evaluate(renderWindowScroller, {scrollElement: 'container'}); | ||
|
||
// scroll more than viewport | ||
await page.$eval('#container', el => el.scrollTo(610, 830)); | ||
await delay(100); | ||
// resize a bit container/window | ||
await page.setViewport({width: 300, height: 500}); | ||
await delay(100); | ||
// scroll again | ||
await page.$eval('#container', el => el.scrollTo(620, 840)); | ||
await delay(100); | ||
|
||
await page.close(); | ||
|
||
expect(scrollFn.mock.calls).toEqual([ | ||
[{scrollLeft: 610 - 50, scrollTop: 830 - 50}], | ||
[{scrollLeft: 620 - 50, scrollTop: 840 - 50}], | ||
]); | ||
expect(resizeFn.mock.calls).toEqual([ | ||
[{width: 500, height: 700}], | ||
[{width: 400, height: 600}], | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const os = require('os'); | ||
const util = require('util'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const makeDir = require('mkdirp'); | ||
const puppeteer = require('puppeteer'); | ||
|
||
const writeFile = util.promisify(fs.writeFile); | ||
|
||
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); | ||
|
||
module.exports = async function() { | ||
if (process.env.JEST !== 'ci') { | ||
console.log('Setup Puppeteer Environment.'); | ||
const browser = await puppeteer.launch({}); | ||
global.__BROWSER__ = browser; | ||
await makeDir(DIR); | ||
await writeFile(path.join(DIR, 'wsEndpoint'), browser.wsEndpoint()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const os = require('os'); | ||
const util = require('util'); | ||
const path = require('path'); | ||
const puppeteer = require('puppeteer'); | ||
const rimraf = require('rimraf'); | ||
|
||
const del = util.promisify(rimraf); | ||
|
||
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); | ||
|
||
module.exports = async function() { | ||
if (process.env.JEST !== 'ci') { | ||
console.log('Teardown Puppeteer Environment.'); | ||
await global.__BROWSER__.close(); | ||
await del(DIR); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const os = require('os'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const NodeEnvironment = require('jest-environment-node'); | ||
const puppeteer = require('puppeteer'); | ||
|
||
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); | ||
|
||
class PuppeteerEnvironment extends NodeEnvironment { | ||
constructor(config) { | ||
super(config); | ||
} | ||
|
||
async setup() { | ||
console.log('Setup Puppeteer Test Environment.'); | ||
await super.setup(); | ||
const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8'); | ||
if (!wsEndpoint) { | ||
throw new Error('wsEndpoint not found'); | ||
} | ||
this.global.__BROWSER__ = await puppeteer.connect({ | ||
browserWSEndpoint: wsEndpoint, | ||
}); | ||
} | ||
|
||
async teardown() { | ||
await super.teardown(); | ||
} | ||
|
||
runScript(script) { | ||
return super.runScript(script); | ||
} | ||
} | ||
|
||
module.exports = PuppeteerEnvironment; |
Oops, something went wrong.