Skip to content

Commit

Permalink
Tests for Util.scrollToBottom
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCarlino committed Nov 13, 2019
1 parent b6d3852 commit dd077d0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
25 changes: 25 additions & 0 deletions frontend/util/__tests__/util_test.ts
Expand Up @@ -3,6 +3,31 @@ import { times } from "lodash";
import { fakeTimeSettings } from "../../__test_support__/fake_time_settings";

describe("util", () => {
describe("scrollToBottom", () => {
it("returns early if element is not found", () => {
document.body.innerHTML =
"<div>" +
" <button id=\"button\" />" +
"</div>";
jest.useFakeTimers();
Util.scrollToBottom("wow");
jest.runAllTimers();
expect(setTimeout).toHaveBeenCalledTimes(0);
});

it("scrolls to bottom when an element is found", () => {
document.body.innerHTML =
"<div>" +
" <span id=\"wow\" />" +
" <button id=\"button\" />" +
"</div>";
jest.useFakeTimers();
Util.scrollToBottom("wow");
jest.runAllTimers();
expect(setTimeout).toHaveBeenCalledTimes(1);
});
});

describe("safeStringFetch", () => {
const data = {
// tslint:disable-next-line:no-null-keyword
Expand Down
1 change: 0 additions & 1 deletion frontend/util/util.ts
Expand Up @@ -172,7 +172,6 @@ export const timestamp = (date = new Date()) => Math.round(date.getTime());
export function scrollToBottom(elementId: string) {
const elToScroll = document.getElementById(elementId);
if (!elToScroll) { return; }

// Wait for the new element height and scroll to the bottom.
setTimeout(() => elToScroll.scrollTop = elToScroll.scrollHeight, 1);
}
Expand Down

0 comments on commit dd077d0

Please sign in to comment.