Skip to content

Commit

Permalink
feat: unit tests with jest (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace authored Sep 15, 2024
1 parent 3dffc23 commit ad451c2
Show file tree
Hide file tree
Showing 13 changed files with 10,023 additions and 4,712 deletions.
10 changes: 9 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{
"extends": ["eslint:recommended", "next", "prettier"]
"extends": ["eslint:recommended", "next", "prettier"],
"overrides": [
{
"files": ["**/__tests__/**/*"],
"env": {
"jest": true
}
}
]
}
30 changes: 30 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Unit Tests

on:
push:
branches:
- '*'
pull_request:
branches:
- '*'

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x, 22.x]

steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'

- run: npm ci

- name: Run Unit Tests
run: npm run test:unit
16 changes: 16 additions & 0 deletions __tests__/lib/cn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { cn } from "@/lib/utils"

describe('check classnames (twMerge)', () => {
it('normal checks', () => {
expect(cn("w-full", "h-full")).toBe("w-full h-full");
expect(cn("md:max-h-[1vw]", "md:hover:max-h-[10vw]")).toBe(
"md:max-h-[1vw] md:hover:max-h-[10vw]"
);
})

it('whitespace check', () => {
expect(cn(" w-full", "h-full ")).toBe(
"w-full h-full"
);
})
})
17 changes: 17 additions & 0 deletions __tests__/lib/escape-text.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { escapeText } from "@/lib/utils"

describe('escape text', () => {
it('should return lower case of plainText', () => {
expect(escapeText('plainText')).toBe('plaintext');
})

it('should return sluggified version of text', () => {
expect(escapeText('Hello World')).toBe('hello-world');
expect(escapeText('Moonlit@grace')).toBe('moonlit-grace');
})

it('should handle multiplte special characters', () => {
expect(escapeText('hello-----world')).toBe('hello-world');
expect(escapeText('moon&&&lit***grace')).toBe('moon-lit-grace')
})
})
13 changes: 13 additions & 0 deletions __tests__/lib/extract-pagragraphs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { extractParagraphs } from "@/lib/utils"

describe('extract paragraphs only from markdown', () => {
it('should return paragraph', () => {
const mockMarkdown = `
# Moonlitgrace
> WHOAMI
Moonlitgrace is a good bwoy!.`
expect(extractParagraphs(mockMarkdown)).toBe('Moonlitgrace is a good bwoy!.')
})
})
6 changes: 6 additions & 0 deletions __tests__/lib/format-date.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { formatDate } from "@/lib/utils";

test('check formated date', () => {
const date = new Date("2023-03-11T02:37:40.790Z");
expect(formatDate(date)).toBe("Mar 11, 2023");
})
12 changes: 12 additions & 0 deletions __tests__/lib/strip-html-tags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { stripHtmlTags } from "@/lib/utils"

describe('stripe html tags', () => {
it('should return plaintext itself', () => {
expect(stripHtmlTags('plain-text')).toBe('plain-text')
})

it('should return content inside', () => {
expect(stripHtmlTags('<strong>Moonlitgrace</strong>')).toBe('Moonlitgrace')
expect(stripHtmlTags('<h1><italic>Hello World</italic></h1>')).toBe('Hello World')
})
})
13 changes: 13 additions & 0 deletions __tests__/lib/truncate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { truncate } from "@/lib/utils"

describe('truncate char', () => {
const exampleStr = 'Step into Moonlitgrace'

it('shouldn"t truncate char shorter than n', () => {
expect(truncate(exampleStr, 25)).toBe(exampleStr)
})

it('shouldn truncate char larger than n', () => {
expect(truncate(exampleStr, 20)).toBe('Step into Moonlit...')
})
})
27 changes: 27 additions & 0 deletions __tests__/lib/validate-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { validateFile } from "@/lib/utils";

describe('check File validation', () => {
it('should return true if File is valid', () => {
const file = new File(['content'], 'test.txt', { type: 'text/plain' });
expect(validateFile(file)).toBeTruthy();
})

it('should return false if File is invalid', () => {
const file = {} as File;
expect(validateFile(file)).toBeFalsy();
})

it('should return false if File name is invalid', () => {
const file = new File(['content'], '', { type: 'text/plain' });
expect(validateFile(file)).toBeFalsy();
})

it('should return false if File size is invalid', () => {
const file = new File(['content'], 'test.txt', { type: 'text/plain' });
// force set size property
Object.defineProperty(file, 'size', {
value: 0
})
expect(validateFile(file)).toBeFalsy();
})
})
22 changes: 22 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Config } from 'jest'
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
const config: Config = {
coverageProvider: 'v8',
testEnvironment: 'jsdom',
// Add more setup options before each test is run
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
// Path alias
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/$1',
}
}

// 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 jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom';
Loading

0 comments on commit ad451c2

Please sign in to comment.