feat: Best Practices (#102)
* feat: best practices * feat: add tests workflow * feat: add best-practices workflow * fix: wrong indentation in workflow
This commit is contained in:
@@ -6,7 +6,7 @@ export interface CourseColors {
|
||||
}
|
||||
|
||||
// calculates luminance of a hex string
|
||||
function getLuminance(hex: string): number {
|
||||
export function getLuminance(hex: string): number {
|
||||
let r = parseInt(hex.substring(1, 3), 16);
|
||||
let g = parseInt(hex.substring(3, 5), 16);
|
||||
let b = parseInt(hex.substring(5, 7), 16);
|
||||
|
||||
22
src/shared/util/tests/colors.test.ts
Normal file
22
src/shared/util/tests/colors.test.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { getLuminance } from '../colors';
|
||||
|
||||
describe('getLuminance', () => {
|
||||
it('should return the correct luminance value for a given hex color', () => {
|
||||
// Test case 1: Hex color #FFFFFF (white)
|
||||
expect(getLuminance('#FFFFFF')).toBeCloseTo(1);
|
||||
|
||||
// Test case 2: Hex color #000000 (black)
|
||||
expect(getLuminance('#000000')).toBeCloseTo(0);
|
||||
|
||||
// Test case 3: Hex color #FF0000 (red)
|
||||
expect(getLuminance('#FF0000')).toBeCloseTo(0.2126);
|
||||
|
||||
// Test case 4: Hex color #00FF00 (green)
|
||||
expect(getLuminance('#00FF00')).toBeCloseTo(0.7152);
|
||||
|
||||
// Test case 5: Hex color #0000FF (blue)
|
||||
expect(getLuminance('#0000FF')).toBeCloseTo(0.0722);
|
||||
});
|
||||
});
|
||||
26
src/shared/util/tests/random.test.ts
Normal file
26
src/shared/util/tests/random.test.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { generateRandomId } from '../random';
|
||||
|
||||
describe('generateRandomId', () => {
|
||||
it('should generate a random ID with the specified length', () => {
|
||||
// Test case 1: Length 5
|
||||
expect(generateRandomId(5)).toHaveLength(5);
|
||||
|
||||
// Test case 2: Length 10
|
||||
expect(generateRandomId(10)).toHaveLength(10);
|
||||
|
||||
// Test case 3: Length 15
|
||||
expect(generateRandomId(15)).toHaveLength(15);
|
||||
});
|
||||
|
||||
it('should generate a unique ID each time', () => {
|
||||
// Generate 100 IDs and check if they are all unique
|
||||
const ids = new Set<string>();
|
||||
for (let i = 0; i < 100; i += 1) {
|
||||
const id = generateRandomId();
|
||||
expect(ids.has(id)).toBe(false);
|
||||
ids.add(id);
|
||||
}
|
||||
});
|
||||
});
|
||||
39
src/shared/util/tests/string.test.ts
Normal file
39
src/shared/util/tests/string.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { capitalize } from '../string';
|
||||
|
||||
// TODO: Fix `string.ts` and `string.test.ts` to make the tests pass
|
||||
// `capitalize` is adding an extra space at the end of the word.
|
||||
describe('capitalize', () => {
|
||||
it('should capitalize the first letter of each word', () => {
|
||||
// Debug
|
||||
const word = 'hello world';
|
||||
const capitalized = capitalize(word);
|
||||
console.log(capitalize(word));
|
||||
console.log(capitalized.length);
|
||||
console.log(capitalized.split(''));
|
||||
|
||||
// Test case 1: Single word
|
||||
expect(capitalize('hello')).toBe('Hello');
|
||||
|
||||
// Test case 2: Multiple words
|
||||
expect(capitalize('hello world')).toBe('Hello World');
|
||||
|
||||
// Test case 3: Words with hyphens
|
||||
expect(capitalize('hello-world')).toBe('Hello-World');
|
||||
|
||||
// Test case 4: Words with hyphens and spaces
|
||||
expect(capitalize('hello-world test')).toBe('Hello-World Test');
|
||||
});
|
||||
|
||||
it('should not change the capitalization of the remaining letters', () => {
|
||||
// Test case 1: All lowercase
|
||||
expect(capitalize('hello')).toBe('Hello');
|
||||
|
||||
// Test case 2: All uppercase
|
||||
expect(capitalize('WORLD')).toBe('WORLD');
|
||||
|
||||
// Test case 3: Mixed case
|
||||
expect(capitalize('HeLLo WoRLd')).toBe('Hello World');
|
||||
});
|
||||
});
|
||||
14
src/shared/util/tests/time.test.ts
Normal file
14
src/shared/util/tests/time.test.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { sleep } from '../time';
|
||||
|
||||
describe('sleep', () => {
|
||||
it('should resolve after the specified number of milliseconds', async () => {
|
||||
const start = Date.now();
|
||||
const milliseconds = 1000;
|
||||
await sleep(milliseconds);
|
||||
const end = Date.now();
|
||||
const elapsed = end - start;
|
||||
expect(elapsed).toBeGreaterThanOrEqual(milliseconds);
|
||||
});
|
||||
});
|
||||
12
src/views/hooks/tests/useFlattenedCourseSchedule.test.ts
Normal file
12
src/views/hooks/tests/useFlattenedCourseSchedule.test.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { convertMinutesToIndex } from '../useFlattenedCourseSchedule';
|
||||
|
||||
describe('useFlattenedCourseSchedule', () => {
|
||||
it('should convert minutes to index correctly', () => {
|
||||
const minutes = 480; // 8:00 AM
|
||||
const expectedIndex = 2; // (480 - 420) / 30 = 2
|
||||
const result = convertMinutesToIndex(minutes);
|
||||
expect(result).toBe(expectedIndex);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import { CalendarCourseCellProps } from 'src/views/components/calendar/CalendarCourseCell/CalendarCourseCell';
|
||||
import type { CalendarCourseCellProps } from 'src/views/components/calendar/CalendarCourseCell/CalendarCourseCell';
|
||||
|
||||
import useSchedules from './useSchedules';
|
||||
|
||||
const dayToNumber: { [day: string]: number } = {
|
||||
@@ -26,7 +27,7 @@ export interface CalendarGridCourse {
|
||||
totalColumns?: number;
|
||||
}
|
||||
|
||||
const convertMinutesToIndex = (minutes: number): number => Math.floor(minutes - 420 / 30);
|
||||
export const convertMinutesToIndex = (minutes: number): number => Math.floor(minutes - 420 / 30);
|
||||
|
||||
/**
|
||||
* Get the active schedule, and convert it to be render-able into a calendar.
|
||||
|
||||
Reference in New Issue
Block a user