mirror of https://github.com/MaxLeiter/Drift
client: add jest and basic lib/ tests
parent
454ea303a6
commit
c5d2e9ac63
@ -0,0 +1,23 @@
|
||||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
|
||||
module.exports = {
|
||||
preset: "ts-jest",
|
||||
testEnvironment: "jsdom",
|
||||
// setupFiles: ["<rootDir>/test/setup-tests.ts"],
|
||||
moduleNameMapper: {
|
||||
"@lib/(.*)": "<rootDir>/lib/$1",
|
||||
"@components/(.*)": "<rootDir>/routes/$1",
|
||||
"@pages/(.*)": "<rootDir>/routes/$1",
|
||||
"@public/(.*)": "<rootDir>/public/$1",
|
||||
"@styles/(.*)": "<rootDir>/styles/$1"
|
||||
},
|
||||
testPathIgnorePatterns: [
|
||||
"<rootDir>/node_modules/",
|
||||
"<rootDir>/dist/",
|
||||
"<rootDir>/.next/",
|
||||
"<rootDir>/public/"
|
||||
],
|
||||
testMatch: [
|
||||
"**/__tests__/**/*.+(ts|tsx|js)",
|
||||
"**/?(*.)+(spec|test).+(ts|tsx|js)"
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
// const byteToMB = (bytes: number) =>
|
||||
// Math.round((bytes / 1024 / 1024) * 100) / 100
|
||||
|
||||
import formatBytes from "@lib/format-bytes"
|
||||
|
||||
describe("formatBytes", () => {
|
||||
it("should return 0 Bytes", () => {
|
||||
expect(formatBytes(0)).toBe("0 Bytes")
|
||||
})
|
||||
|
||||
it("should return 512 Bytes", () => {
|
||||
expect(formatBytes(512)).toBe("512 Bytes")
|
||||
})
|
||||
|
||||
it("should return 1 KB", () => {
|
||||
expect(formatBytes(1024)).toBe("1 KB")
|
||||
})
|
||||
|
||||
it("should return 1 MB", () => {
|
||||
expect(formatBytes(1024 * 1024)).toBe("1 MB")
|
||||
})
|
||||
|
||||
it("should return 1 GB", () => {
|
||||
expect(formatBytes(1024 * 1024 * 1024)).toBe("1 GB")
|
||||
})
|
||||
|
||||
it("should return 256 GB", () => {
|
||||
expect(formatBytes(1024 * 1024 * 1024 * 256)).toBe("256 GB")
|
||||
})
|
||||
|
||||
it("should return 1 TB", () => {
|
||||
expect(formatBytes(1024 * 1024 * 1024 * 1024)).toBe("1 TB")
|
||||
})
|
||||
})
|
||||
@ -0,0 +1,21 @@
|
||||
// examples:
|
||||
// Title --> Title 1
|
||||
// Title 1 --> Title 2
|
||||
// Title 2 --> Title 3
|
||||
// My Title 12 huh -> My Title 12 huh 1
|
||||
|
||||
import getTitleForPostCopy from "@lib/get-title-for-post-copy";
|
||||
|
||||
describe("getTitleForPostCopy", () => {
|
||||
it("should add a number if no number is present", () => {
|
||||
expect(getTitleForPostCopy("Title")).toBe("Title 1");
|
||||
});
|
||||
|
||||
it("should increment the number if present", () => {
|
||||
expect(getTitleForPostCopy("Title 1")).toBe("Title 2");
|
||||
});
|
||||
|
||||
it("should ignore numbers not at the end of the title", () => {
|
||||
expect(getTitleForPostCopy("My Title 12 words")).toBe("My Title 12 words 1");
|
||||
});
|
||||
})
|
||||
@ -0,0 +1,11 @@
|
||||
import { timeAgo } from "@lib/time-ago";
|
||||
|
||||
describe("timeAgo", () => {
|
||||
it("should return '1 second ago' for 1 second ago", () => {
|
||||
expect(timeAgo(new Date(Date.now() - 1000))).toBe("1 second ago");
|
||||
});
|
||||
|
||||
it("should handle negative values", () => {
|
||||
expect(timeAgo(new Date(Date.now() - -1000))).toBe("0 second ago");
|
||||
})
|
||||
})
|
||||
@ -1,4 +0,0 @@
|
||||
const byteToMB = (bytes: number) =>
|
||||
Math.round((bytes / 1024 / 1024) * 100) / 100
|
||||
|
||||
export default byteToMB
|
||||
@ -0,0 +1,13 @@
|
||||
function formatBytes(bytes: number, decimals = 2) {
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
|
||||
const k = 1024;
|
||||
const dm = decimals < 0 ? 0 : decimals;
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
||||
}
|
||||
|
||||
export default formatBytes
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue