Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unit tests for utils functions #92

Merged
merged 9 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/dashboard/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,9 @@ export default config;
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node'
testEnvironment: 'node',
transform: {
'^.+\\.svelte$': 'svelte-jester',
'^.+\\.ts$': 'ts-jest'
}
};
2 changes: 1 addition & 1 deletion apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"start": "node build",
"prepare": "svelte-kit sync",
"test": "jest",
"test:watch": "pnpm run test -- --watch"
"test:watch": "pnpm run test --watch"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.386.0",
Expand Down
12 changes: 11 additions & 1 deletion apps/dashboard/src/lib/utils/functions/course.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import '@testing-library/jest-dom';
import { isCourseFree } from './course';

describe('course.ts', () => {
test('isCourseFree', () => {
test('Should return True when cost is 0', () => {
const result = isCourseFree(0);
expect(result).toBeTruthy();
});

test('Should return true when cost is negative', () => {
const result = isCourseFree(-10);
expect(result).toBeTruthy();
});

test('Should return True when cost is Not A Number', () => {
const result = isCourseFree(NaN);
expect(result).toBeTruthy();
});
});
37 changes: 37 additions & 0 deletions apps/dashboard/src/lib/utils/functions/date.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import '@testing-library/jest-dom';
Toyin5 marked this conversation as resolved.
Show resolved Hide resolved
import { calDateDiff, getGreeting } from './date';

describe('calDateDiff', () => {
test('should return the correct time difference when the input date is in the past', () => {
const inputDate = new Date('2022-01-01');
const expectedOutput = '2 years ago';

const result = calDateDiff(inputDate);

expect(result).toBe(expectedOutput);
});

test('should return the correct time difference when the input date is a number', () => {
const inputDate = 1640995200000; // January 1, 2022
const expectedOutput = '2 years ago';

const result = calDateDiff(inputDate);

Toyin5 marked this conversation as resolved.
Show resolved Hide resolved
expect(result).toBe(expectedOutput);
});
});

describe('getGreeting', () => {
test("should return 'Good Morning' when the current time is before 12pm", () => {
jest.useFakeTimers().setSystemTime(new Date('2023-12-10T00:21:01.691Z'));
expect(getGreeting()).toBe('Good Morning');
});
test("should return 'Good Afternoon' when the current time is after 12pm", () => {
jest.useFakeTimers().setSystemTime(new Date('2023-12-10T14:21:01.691Z'));
expect(getGreeting()).toBe('Good Afternoon');
});
test("should return 'Good Evening' when the current time is after 6pm", () => {
jest.useFakeTimers().setSystemTime(new Date('2023-12-10T19:21:01.691Z'));
expect(getGreeting()).toBe('Good Evening');
});
});
47 changes: 47 additions & 0 deletions apps/dashboard/src/lib/utils/functions/formatYoutubeVideo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import '@testing-library/jest-dom';
import { formatYoutubeVideo, getEmbedId } from './formatYoutubeVideo';

describe('format youtube video', () => {
test('should return the same url when given a valid youtube embed url', () => {
const url = 'https://www.youtube.com/embed/qajK1J1neAM';
const errors = { video: '' };
const result = formatYoutubeVideo(url, errors);
expect(result).toBe(url);
});

test('should return the correct embed url when given a youtube url with a start time parameter', () => {
const url = 'https://www.youtube.com/watch?v=qajK1J1neAM&start=123';
const errors = { video: '' };
const result = formatYoutubeVideo(url, errors);
expect(result).toBe('https://www.youtube.com/embed/qajK1J1neAM');
});
});

describe('getEmbedId', () => {
test('should return the correct embed id when the url contains "embed"', () => {
const url = 'https://www.youtube.com/embed/qajK1J1neAM';
const expectedEmbedId = 'qajK1J1neAM';

const result = getEmbedId(url);

expect(result).toBe(expectedEmbedId);
});

test('should return an empty string when the url does not contain "watch" or "embed" and is not in the format "youtu.be/"', () => {
const url = 'https://www.youtube.com/invalid-url';
const expectedEmbedId = '';

const result = getEmbedId(url);

expect(result).toBe(expectedEmbedId);
});

test('should return an empty string when the url is an empty string', () => {
const url = '';
const expectedEmbedId = '';

const result = getEmbedId(url);

expect(result).toBe(expectedEmbedId);
});
});
20 changes: 20 additions & 0 deletions apps/dashboard/src/lib/utils/functions/generateSlug.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import '@testing-library/jest-dom';
import generateSlug from './generateSlug';

describe('generateSlug', () => {
// Returns a string with lowercase letters and hyphens
test('should return a string with lowercase letters and hyphens when given a valid title', () => {
const title = 'This is a Valid Title';
const expected = 'this-is-a-valid-title';
const result = generateSlug(title);
expect(result).toBe(expected);
});

// Includes the current timestamp at the end of the string
test('should include the current timestamp at the end of the string', () => {
const title = 'This is a Valid Title';
const result = generateSlug(title);
const timestamp = result.split('-').pop();
expect(Number(timestamp)).toBeGreaterThan(0);
});
});
9 changes: 8 additions & 1 deletion apps/dashboard/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ const config = {
preprocess: [vitePreprocess({})],

kit: {
adapter: vercelAdapter()
adapter: vercelAdapter(),
files: {
// Add this line to include test files
template: {
// ...
transform: 'svelte-jester'
}
}
}
};

Expand Down
Loading