-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add isDate() * refine * more tests * more config
- Loading branch information
1 parent
4232796
commit a0aeb8e
Showing
14 changed files
with
242 additions
and
45 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
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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
export default (value: number): string => { | ||
const hours = Math.floor(value / 3600); | ||
const minutes = Math.floor((value - hours * 3600) / 60); | ||
const seconds = value - hours * 3600 - minutes * 60; | ||
/** | ||
* Formats a value in seconds to HHh:MMm:SSs | ||
* @param {number} seconds - Duration in seconds | ||
* @return {string} A formatted string | ||
*/ | ||
const formatDayLength = (seconds: number): string => { | ||
const hrs = Math.floor(seconds / 3600); | ||
const min = Math.floor((seconds - hrs * 3600) / 60); | ||
const sec = seconds - hrs * 3600 - min * 60; | ||
const formatValue = (val: number) => (val < 10 ? `0${val}` : `${val}`); | ||
|
||
const formattedHours = hours < 10 ? `0${hours}` : hours.toString(); | ||
const formattedMinutes = minutes < 10 ? `0${minutes}` : minutes.toString(); | ||
const formattedSeconds = seconds < 10 ? `0${seconds}` : seconds.toString(); | ||
|
||
return `${formattedHours}h:${formattedMinutes}m:${formattedSeconds}s`; | ||
return `${formatValue(hrs)}h:${formatValue(min)}m:${formatValue(sec)}s`; | ||
}; | ||
|
||
export default formatDayLength; |
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 |
---|---|---|
@@ -1,2 +1,10 @@ | ||
export default (dateString: string): string => | ||
/** | ||
* Formats dateString toLocaleTimeString() | ||
* for consistent styling | ||
* @param {string} dateString - A valid date string | ||
* @return {string} A formatted string | ||
*/ | ||
const formatTime = (dateString: string): string => | ||
new Date(dateString).toLocaleTimeString(); | ||
|
||
export default formatTime; |
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,15 @@ | ||
/** | ||
* Basic validation for dateString | ||
* @param {string} dateString | ||
*/ | ||
const isDate = (dateString: string): boolean => { | ||
const pattern = /^\d{4}-\d{2}-\d{2}$/; | ||
|
||
if (!pattern.test(dateString)) { | ||
return false; | ||
} | ||
|
||
return isNaN(Date.parse(dateString)) ? false : true; | ||
}; | ||
|
||
export default isDate; |
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
File renamed without changes.
File renamed without changes.
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
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,15 @@ | ||
import isDate from "../../src/isDate"; | ||
|
||
describe("isDate()", () => { | ||
it("is returns false with a badly formatted date", () => { | ||
expect(isDate("21-21-21")).toEqual(false); | ||
}); | ||
|
||
it("is returns false with an impossible date", () => { | ||
expect(isDate("2021-90-90")).toEqual(false); | ||
}); | ||
|
||
it("is returns true with a valid date", () => { | ||
expect(isDate("2021-01-01")).toEqual(true); | ||
}); | ||
}); |
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,108 @@ | ||
import main from "../../src/main"; | ||
import { diskCache } from "../../src/cache"; | ||
import drawTable from "../../src/drawTable"; | ||
import * as getIPData from "../../src/getIPData"; | ||
import * as getSunData from "../../src/getSunData"; | ||
|
||
import storedIPData from "../mock/storedIPData"; | ||
import storedSunData from "../mock/storedSunData"; | ||
|
||
jest.mock("../../src/drawTable", () => jest.fn()); | ||
|
||
describe("main()", () => { | ||
describe("logging", () => { | ||
it("displays options", async () => { | ||
const consoleSpy = jest.spyOn(console, "info").mockImplementation(); | ||
const options = { | ||
clean: false, | ||
date: "bad-date", | ||
debug: true, | ||
}; | ||
await main(options); | ||
|
||
expect(consoleSpy).toHaveBeenNthCalledWith(1, "[Options]:"); | ||
expect(consoleSpy).toHaveBeenNthCalledWith(2, options); | ||
}); | ||
}); | ||
|
||
describe("validation", () => { | ||
it("returns an error with a bad date", async () => { | ||
const response = await main({ | ||
clean: false, | ||
date: "bad-date", | ||
debug: false, | ||
}); | ||
|
||
expect(response).toEqual( | ||
"Date must be a valid date with format YYYY-MM-DD" | ||
); | ||
}); | ||
}); | ||
|
||
describe("clean", () => { | ||
it("clears the cache successfully", async () => { | ||
const cacheSpy = jest | ||
.spyOn(diskCache, "reset") | ||
.mockImplementationOnce(() => "mocked clear"); | ||
|
||
const response = await main({ | ||
clean: true, | ||
date: "2021-01-01", | ||
debug: false, | ||
}); | ||
|
||
expect(cacheSpy).toHaveBeenCalled(); | ||
expect(response).toEqual("Cache is cleared"); | ||
}); | ||
|
||
it("reports a error when reset fails", async () => { | ||
const cacheSpy = jest | ||
.spyOn(diskCache, "reset") | ||
.mockImplementationOnce(() => { | ||
throw new Error("uh oh"); | ||
}); | ||
|
||
const response = await main({ | ||
clean: true, | ||
date: "2021-01-01", | ||
debug: false, | ||
}); | ||
|
||
expect(cacheSpy).toHaveBeenCalled(); | ||
expect(response).toEqual("Error clearing cache"); | ||
}); | ||
}); | ||
|
||
describe("main", () => { | ||
it("creates a table", async () => { | ||
jest | ||
.spyOn(getIPData, "getIPData") | ||
.mockImplementationOnce(() => Promise.resolve(storedIPData)); | ||
jest | ||
.spyOn(getSunData, "getSunData") | ||
.mockImplementationOnce(() => Promise.resolve(storedSunData)); | ||
|
||
await main({ | ||
clean: false, | ||
date: storedSunData.date, | ||
debug: false, | ||
}); | ||
|
||
expect(drawTable).toHaveBeenCalledWith(storedSunData); | ||
}); | ||
|
||
it("throws an error when data fetching fails", async () => { | ||
jest | ||
.spyOn(getIPData, "getIPData") | ||
.mockImplementationOnce(() => Promise.reject("Failed")); | ||
|
||
await expect( | ||
main({ | ||
clean: false, | ||
date: storedSunData.date, | ||
debug: false, | ||
}) | ||
).rejects.toThrow("Failed"); | ||
}); | ||
}); | ||
}); |