Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
fix(web): the scroll-into-view method should not start to scroll unti…
Browse files Browse the repository at this point in the history
…l the page stops moving
  • Loading branch information
clebert committed Apr 25, 2018
1 parent c8d4cb4 commit b138b93
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
26 changes: 24 additions & 2 deletions @pageobject/web/src/WebComponent.ts
Expand Up @@ -127,8 +127,29 @@ export abstract class WebComponent extends Component<WebNode, WebAdapter> {
}

public scrollIntoView(): Effect<void> {
return async () =>
(await this.findUniqueNode()).execute(element => {
const getRect = async () =>
(await this.findUniqueNode()).execute(element =>
element.getBoundingClientRect()
);

const isPageMoving = async (lastRect: ClientRect) => {
const currentRect = await getRect();

return (
currentRect.left !== lastRect.left || currentRect.top !== lastRect.top
);
};

return async () => {
let lastRect: ClientRect;

do {
lastRect = await getRect();

await new Promise<void>(resolve => setTimeout(resolve, 10));
} while (await isPageMoving(lastRect));

await (await this.findUniqueNode()).execute(element => {
const {height, left, top, width} = element.getBoundingClientRect();
const {innerHeight, innerWidth} = window;

Expand All @@ -137,5 +158,6 @@ export abstract class WebComponent extends Component<WebNode, WebAdapter> {
top - innerHeight / 2 + height / 2
);
});
};
}
}
62 changes: 62 additions & 0 deletions @pageobject/web/src/tests/WebComponent.test.ts
Expand Up @@ -296,5 +296,67 @@ describe('WebComponent', () => {
expect(scrollBy).toHaveBeenCalledTimes(1);
expect(scrollBy).toHaveBeenLastCalledWith(-475, -275);
});

it('should not start to scroll until the page stops moving vertically', async () => {
let top = 100;

element.getBoundingClientRect.mockImplementation(() => ({
height: 0,
left: 0,
top,
width: 0
}));

scrollBy.mockImplementation(() => {
if (top !== 0) {
throw new Error('page is moving');
}
});

const intervalId = setInterval(() => {
top -= 1;

if (top === 0) {
clearInterval(intervalId);
}
}, 1);

await component.scrollIntoView()();

expect(scrollBy).toHaveBeenCalledTimes(1);
});

it('should not start to scroll until the page stops moving horizontally', async () => {
let left = 100;

element.getBoundingClientRect.mockImplementation(() => ({
height: 0,
left,
top: 0,
width: 0
}));

scrollBy.mockImplementation(() => {
if (left !== 0) {
throw new Error('page is moving');
}
});

const intervalId = setInterval(() => {
left -= 1;

if (left === 0) {
clearInterval(intervalId);
}
}, 1);

await component.scrollIntoView()();

expect(scrollBy).toHaveBeenCalledTimes(1);

expect(element.getBoundingClientRect.mock.calls.length).toBeGreaterThan(
1
);
});
});
});

0 comments on commit b138b93

Please sign in to comment.