Skip to content

Commit

Permalink
feat: add unitest mock css frame work
Browse files Browse the repository at this point in the history
  • Loading branch information
khoilen committed Dec 24, 2024
1 parent ab08a15 commit 55c02fa
Show file tree
Hide file tree
Showing 6 changed files with 697 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/publish-nt-css.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ jobs:
run: pnpm install
working-directory: apps/nt-stylesheet

- name: Test CSS framework
run: pnpm run test
working-directory: apps/nt-stylesheet

- name: Build CSS framework
run: pnpm run build
working-directory: apps/nt-stylesheet
Expand Down
8 changes: 4 additions & 4 deletions apps/nt-stylesheet/bin/init-tailwind.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs'
import { exec } from 'child_process'
import readline from 'readline'

const installPackage = (packageName) => {
export const installPackage = (packageName) => {
return new Promise((resolve, reject) => {
exec(`npm install ${packageName}`, (error) => {
if (error) {
Expand All @@ -20,7 +20,7 @@ const installPackage = (packageName) => {
})
}

const createTailwindConfig = () => {
export const createTailwindConfig = () => {
const tailwindConfigContent = `/** @type {import('tailwindcss').Config} */
const ntTheme = require('nt-stylesheet/dist/theme.cjs');
module.exports = {
Expand Down Expand Up @@ -48,7 +48,7 @@ module.exports = {
)
}

const createPostCSSConfig = () => {
export const createPostCSSConfig = () => {
const postcssConfigContent = `module.exports = {
plugins: {
'postcss-import': {},
Expand Down Expand Up @@ -76,7 +76,7 @@ const createPostCSSConfig = () => {
)
}

const initialize = async () => {
export const initialize = async () => {
try {
await installPackage('nt-stylesheet')
const rl = readline.createInterface({
Expand Down
97 changes: 97 additions & 0 deletions apps/nt-stylesheet/bin/init-tailwind.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, it, expect, vi } from 'vitest'
import fs from 'fs'
import { exec } from 'child_process'
import readline from 'readline'
import {
installPackage,
createTailwindConfig,
createPostCSSConfig,
initialize,
} from './init-tailwind.js'

vi.mock('fs')
vi.mock('child_process')
vi.mock('readline')

describe('init-tailwind.js', () => {
describe('installPackage', () => {
it('should install package successfully', async () => {
exec.mockImplementation((cmd, callback) => callback(null))
await expect(
installPackage('nt-stylesheet'),
).toStrictEqual(Promise.resolve())
expect(exec).toHaveBeenCalledWith(
'npm install nt-stylesheet',
expect.any(Function),
)
})

it('should fail to install package', async () => {
const error = new Error('Installation failed')
exec.mockImplementation((cmd, callback) =>
callback(error),
)
await expect(
installPackage('nt-stylesheet'),
).rejects.toThrow('Installation failed')
})
})

describe('createTailwindConfig', () => {
it('should create tailwind.config.js file', () => {
fs.writeFile.mockImplementation(
(path, content, callback) => callback(null),
)
createTailwindConfig()
expect(fs.writeFile).toHaveBeenCalledWith(
'tailwind.config.js',
expect.stringContaining('module.exports = {'),
expect.any(Function),
)
})
})

describe('createPostCSSConfig', () => {
it('should create postcss.config.js file', () => {
fs.writeFile.mockImplementation(
(path, content, callback) => callback(null),
)
createPostCSSConfig()
expect(fs.writeFile).toHaveBeenCalledWith(
'postcss.config.js',
expect.stringContaining('module.exports = {'),
expect.any(Function),
)
})
})

describe('initialize', () => {
it('should initialize successfully', async () => {
exec.mockImplementation((cmd, callback) => callback(null))
readline.createInterface.mockReturnValue({
question: (query, callback) => callback('y'),
close: vi.fn(),
})
fs.writeFile.mockImplementation(
(path, content, callback) => callback(null),
)

await initialize()

expect(exec).toHaveBeenCalledWith(
'npm install nt-stylesheet',
expect.any(Function),
)
expect(fs.writeFile).toHaveBeenCalledWith(
'tailwind.config.js',
expect.stringContaining('module.exports = {'),
expect.any(Function),
)
expect(fs.writeFile).toHaveBeenCalledWith(
'postcss.config.js',
expect.stringContaining('module.exports = {'),
expect.any(Function),
)
})
})
})
6 changes: 4 additions & 2 deletions apps/nt-stylesheet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Style sheet for the NT Design System",
"packageManager": "[email protected]",
"scripts": {
"build": "pnpm vite build"
"build": "pnpm vite build",
"test": "npx vitest"
},
"bin": {
"nt-stylesheet": "bin/init-tailwind.js"
Expand Down Expand Up @@ -40,7 +41,8 @@
"nx": "^20.2.2",
"typescript": "^5.7.2",
"vite": "^6.0.3",
"vite-plugin-sass-dts": "^1.3.29"
"vite-plugin-sass-dts": "^1.3.29",
"vitest": "^2.1.8"
},
"dependencies": {
"autoprefixer": "^10.4.20",
Expand Down
Loading

0 comments on commit 55c02fa

Please sign in to comment.