client: add jest and basic lib/ tests

This commit is contained in:
Max Leiter 2022-04-16 22:43:57 -07:00
parent 454ea303a6
commit c5d2e9ac63
No known key found for this signature in database
GPG key ID: A3512F2F2F17EBDA
8 changed files with 2165 additions and 41 deletions

23
client/jest.config.js Normal file
View file

@ -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)"
]
}

View file

@ -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")
})
})

View file

@ -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");
});
})

View file

@ -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");
})
})

View file

@ -1,4 +0,0 @@
const byteToMB = (bytes: number) =>
Math.round((bytes / 1024 / 1024) * 100) / 100
export default byteToMB

View file

@ -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

View file

@ -8,7 +8,8 @@
"start": "next start --port 3001", "start": "next start --port 3001",
"lint": "next lint && prettier --list-different --config .prettierrc '{components,lib,pages}/**/*.{ts,tsx}' --write", "lint": "next lint && prettier --list-different --config .prettierrc '{components,lib,pages}/**/*.{ts,tsx}' --write",
"analyze": "cross-env ANALYZE=true next build", "analyze": "cross-env ANALYZE=true next build",
"find:unused": "next-unused" "find:unused": "next-unused",
"test": "jest --silent"
}, },
"dependencies": { "dependencies": {
"@geist-ui/core": "2.3.8", "@geist-ui/core": "2.3.8",
@ -32,6 +33,7 @@
}, },
"devDependencies": { "devDependencies": {
"@next/bundle-analyzer": "12.1.5", "@next/bundle-analyzer": "12.1.5",
"@types/jest": "^27.4.1",
"@types/node": "17.0.23", "@types/node": "17.0.23",
"@types/react": "18.0.5", "@types/react": "18.0.5",
"@types/react-datepicker": "4.4.0", "@types/react-datepicker": "4.4.0",
@ -39,8 +41,10 @@
"cross-env": "7.0.3", "cross-env": "7.0.3",
"eslint": "8.13.0", "eslint": "8.13.0",
"eslint-config-next": "12.1.5", "eslint-config-next": "12.1.5",
"jest": "^27.5.1",
"next-unused": "0.0.6", "next-unused": "0.0.6",
"prettier": "2.6.2", "prettier": "2.6.2",
"ts-jest": "^27.1.4",
"typescript": "4.6.3", "typescript": "4.6.3",
"typescript-plugin-css-modules": "3.4.0" "typescript-plugin-css-modules": "3.4.0"
}, },

File diff suppressed because it is too large Load diff