Skip to content

Commit 2fc3e99

Browse files
Issue #26 Refactor & Added tests to increase coverage
Added tests to increase coverage
2 parents a13ab06 + 3360fd1 commit 2fc3e99

1 file changed

Lines changed: 63 additions & 19 deletions

File tree

src/utilities/scroll-utils.test.ts

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ describe("ScrollUtils", () => {
77
// -----------------------------------------------------------------------------------------
88
describe("scrollToElementById", () => {
99
it("when element is retrieved, then attempts to scroll", () => {
10-
//Arrange
10+
// Arrange
1111
const element = document.createElement("div");
1212
const scrollIntoViewMock = jest.fn();
1313
element.scrollIntoView = scrollIntoViewMock;
1414
jest.spyOn(document, "getElementById").mockImplementation(
1515
() => element
1616
);
17-
const elementId = faker.random.uuid();
17+
const elementId = faker.random
18+
.number({ min: 1, max: 999 })
19+
.toString();
1820

1921
// Act
2022
ScrollUtils.scrollToElementById(elementId);
@@ -24,8 +26,10 @@ describe("ScrollUtils", () => {
2426
});
2527

2628
test("when element is not found, it attempts to retrieve the element up to 50 times", () => {
27-
//Arrange
28-
const elementId = faker.random.uuid();
29+
// Arrange
30+
const elementId = faker.random
31+
.number({ min: 1, max: 999 })
32+
.toString();
2933
const getElementByIdMock = jest.spyOn(document, "getElementById");
3034

3135
// Act
@@ -36,6 +40,41 @@ describe("ScrollUtils", () => {
3640
// Assert
3741
expect(getElementByIdMock).toBeCalledTimes(50);
3842
});
43+
44+
test("when element is not found, it logs a console warning for development environment", () => {
45+
// Arrange
46+
const elementId = faker.random
47+
.number({ min: 1, max: 999 })
48+
.toString();
49+
process.env.NODE_ENV = "development";
50+
const consoleWarnMock = jest.spyOn(console, "warn");
51+
52+
// Act
53+
jest.useFakeTimers();
54+
ScrollUtils.scrollToElementById(elementId);
55+
jest.runAllTimers();
56+
57+
// Assert
58+
expect(consoleWarnMock).toBeCalled();
59+
});
60+
61+
test("when scrollOption has initial delay, it calls setTimeout with supplied delay", () => {
62+
// Arrange
63+
const options = { initialDelay: 200 };
64+
const elementId = faker.random
65+
.number({ min: 1, max: 999 })
66+
.toString();
67+
const setTimeoutMock = jest.spyOn(window, "setTimeout");
68+
69+
// Act
70+
ScrollUtils.scrollToElementById(elementId, options);
71+
72+
// Assert
73+
expect(setTimeoutMock).toBeCalledWith(
74+
expect.any(Function),
75+
options.initialDelay
76+
);
77+
});
3978
});
4079
// #endregion scrollToElementById
4180

@@ -54,21 +93,26 @@ describe("ScrollUtils", () => {
5493
expect(result).toBeUndefined();
5594
});
5695

57-
it("when location is null, then returns", () => {
58-
// Arrange & Act
59-
const result = ScrollUtils.scrollToHash(null);
60-
61-
// Assert
62-
expect(result).toBeUndefined();
63-
});
64-
65-
it("when location is undefined, then returns", () => {
66-
// Arrange & Act
67-
const result = ScrollUtils.scrollToHash(undefined);
68-
69-
// Assert
70-
expect(result).toBeUndefined();
71-
});
96+
test.each`
97+
location
98+
${null}
99+
${undefined}
100+
`(
101+
"when location is '$location', it does not scroll",
102+
({ location }) => {
103+
// Arrange
104+
const scrollToElementByIdSpy = jest.spyOn(
105+
ScrollUtils,
106+
"scrollToElementById"
107+
);
108+
109+
// Act
110+
ScrollUtils.scrollToHash(location);
111+
112+
// Assert
113+
expect(scrollToElementByIdSpy).not.toBeCalled();
114+
}
115+
);
72116

73117
it("when location hash has value, then calls scrollToElementById", () => {
74118
// Arrange

0 commit comments

Comments
 (0)