Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
test: ✅ init jest, and add 1 test, add to ci (#113)
Browse files Browse the repository at this point in the history
* test: init jest, and add 1 test, add to ci

* test: init jest, and add 1 test, add to ci

* fix: tests for get elapsed time
  • Loading branch information
velenyx committed Sep 25, 2023
1 parent d04a207 commit e84ea2d
Show file tree
Hide file tree
Showing 7 changed files with 1,624 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ jobs:
yarn lint:ts:check
yarn prettier:check
- name: Unit testing 💀💀
working-directory: ./client
run: |
yarn test:unit
- name: Build project
working-directory: ./client
run: yarn build
Expand Down
25 changes: 25 additions & 0 deletions client/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import nextJest from 'next/jest.js'

const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: './',
})

// Add any custom config to be passed to Jest
/** @type {import('jest').Config} */
const config = {
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json', 'node'],
testMatch: ['<rootDir>/src/**/*(*.)@(test).[tj]s?(x)'],
moduleDirectories: ['node_modules', '<rootDir>/'],
clearMocks: true,
collectCoverage: true,
coverageDirectory: 'coverage',
moduleNameMapper: {
'^~/(.*)$': '<rootDir>/src/$1',
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testEnvironment: 'jest-environment-jsdom',
}

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(config)
1 change: 1 addition & 0 deletions client/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom';
6 changes: 6 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"lint:ts:fix": "npx eslint \"**/*.{ts,tsx}\" --fix",
"prettier:check": "prettier --check \"**/*.{js,ts,tsx,css,mdx}\"",
"prettier:fix": "prettier --write \"**/*.{js,ts,tsx,css,mdx}\"",
"test:unit": "npx jest",
"prepare": "cd .. && husky install ./client/.husky",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
Expand Down Expand Up @@ -58,11 +59,16 @@
"@storybook/nextjs": "7.2.1",
"@storybook/react": "7.2.1",
"@storybook/testing-library": "0.2.0",
"@testing-library/jest-dom": "^6.1.3",
"@testing-library/react": "^14.0.0",
"@types/jest": "^29.5.5",
"@types/react-modal": "^3.16.0",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"eslint-plugin-storybook": "^0.6.13",
"husky": "^8.0.3",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"prettier": "^3.0.1",
"storybook": "7.2.1"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { getElapsedTime } from './get-elapsed-time';

describe('getElapsedTime', () => {
// Generated by CodiumAI

describe('getElapsedTime', () => {
// Returns elapsed time in seconds for a time that is less than a minute ago
it('should return elapsed time in seconds when the time is less than a minute ago', () => {
const currentTime = new Date();
const pastTime = new Date(currentTime.getTime() - 5000); // 5 seconds ago
expect(getElapsedTime(pastTime)).toBe('5s ago');
});

// Returns elapsed time in minutes for a time that is less than an hour ago
it('should return elapsed time in minutes when the time is less than an hour ago', () => {
const currentTime = new Date();
const pastTime = new Date(currentTime.getTime() - 300000); // 5 minutes ago
expect(getElapsedTime(pastTime)).toBe('5m ago');
});

// Returns elapsed time in hours for a time that is less than a day ago
it('should return elapsed time in hours when the time is less than a day ago', () => {
const currentTime = new Date();
const pastTime = new Date(currentTime.getTime() - 7200000); // 2 hours ago
expect(getElapsedTime(pastTime)).toBe('2h ago');
});

// Returns "0s ago" for a time that is 1 millisecond ago
it('should return "1s ago" for a time that is 0 millisecond ago', () => {
const currentTime = new Date();
const pastTime = new Date(currentTime.getTime() - 1); // 1 millisecond ago
expect(getElapsedTime(pastTime)).toBe('0s ago');
});

// Returns "1s ago" for a time that is 1 secons ago
it('should return ""1s ago" for a time that is 1 secons ago', () => {
const currentTime = new Date();
const pastTime = new Date(currentTime.getTime() - 1000); // 1 second ago
expect(getElapsedTime(pastTime)).toBe('1s ago');
});

// Returns "0s ago" in all cases in milliseconds
it('should return "0s ago" in all cases in milliseconds', () => {
const currentTime = new Date();
const pastTime = new Date(currentTime.getTime() - 10); // 10 millisecond ago
expect(getElapsedTime(pastTime)).toBe('0s ago');
const pastTime2 = new Date(currentTime.getTime() - 100); // 100 millisecond ago
expect(getElapsedTime(pastTime2)).toBe('0s ago');
const pastTime3 = new Date(currentTime.getTime() - 999); // 999 millisecond ago
expect(getElapsedTime(pastTime3)).toBe('0s ago');
});

// Returns "1m ago" for a time that is 1 minute and 30 seconds ago
it('should return "1m ago" for a time that is 1 minute and 30 seconds ago', () => {
const currentTime = new Date();
const pastTime = new Date(currentTime.getTime() - 90000); // 1 minute and 30 seconds ago
expect(getElapsedTime(pastTime)).toBe('1m ago');
});
});

// Returns elapsed time in years for a time that is more than a year ago
it('should return elapsed time in years when the time is more than a year ago', () => {
const currentTime = new Date();
const pastTime = new Date(currentTime.getTime() - 31536000000); // 1 year ago
expect(getElapsedTime(pastTime)).toBe('1y ago');
});

// Returns elapsed time in days for a time that is less than a month ago
it('should return elapsed time in days when the time is less than a month ago', () => {
const currentTime = new Date();
const pastTime = new Date(currentTime.getTime() - 86400000); // 1 day ago
expect(getElapsedTime(pastTime)).toBe('1d ago');
});

// Returns elapsed time in months for a time that is less than a year ago
it('should return elapsed time in months when the time is less than a year ago', () => {
const currentTime = new Date();
const pastTime = new Date(currentTime.getTime() - 2592000000); // 30 days ago
expect(getElapsedTime(pastTime)).toBe('1mo ago');
});

it('should throw an error for invalid date input', () => {
expect(() => getElapsedTime('invalid date')).toThrow(Error);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
export const getElapsedTime = (time: string | number | Date) => {
const datePast = new Date(time);
if (isNaN(datePast.getTime())) {
throw new Error('Invalid date input');
}
const dateNow = new Date();
const timeDiff = dateNow.getTime() - datePast.getTime();

Expand Down
Loading

2 comments on commit e84ea2d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for teameights ready!

✅ Preview
https://teameights-elm3cy59h-exortme1ster.vercel.app

Built with commit e84ea2d.
This pull request is being automatically deployed with vercel-action

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for teameights-storybook ready!

✅ Preview
https://teameights-storybook-9n22djet5-exortme1ster.vercel.app

Built with commit e84ea2d.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.