Skip to content

Commit

Permalink
test(utils): add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Dec 29, 2023
1 parent f23e6e1 commit eefa59d
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 44 deletions.
18 changes: 18 additions & 0 deletions packages/utils/tests/console/formatMs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, it } from 'vitest'
import { formatMs } from '../../src/index.js'

const testCases: [source: number, expected: string][] = [
[0, '0ms'],
[999, '999ms'],
[1000, '1.00s'],
[1001, '1.00s'],
[1999, '2.00s'],
[2000, '2.00s'],
[2001, '2.00s'],
]

testCases.forEach(([source, expected]) => {
it(`${source} -> ${expected}`, () => {
expect(formatMs(source)).toBe(expected)
})
})
35 changes: 15 additions & 20 deletions packages/utils/tests/console/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it, vi } from 'vitest'
import { expect, it, vi } from 'vitest'
import { logger } from '../../src/index.js'

const methods = [
Expand All @@ -9,29 +9,24 @@ const methods = [
['error', 'error'],
]

describe('utils > logger', () => {
methods.forEach(([method, innerMethod]) => {
it(method, () => {
const stored = console[innerMethod]
console[innerMethod] = vi.fn()
methods.forEach(([method, innerMethod]) => {
it(method, () => {
const stored = console[innerMethod]
console[innerMethod] = vi.fn()

logger[method]('foo')
expect(console[innerMethod]).toHaveBeenCalledWith(
expect.any(String),
'foo',
)
logger[method]('foo')
expect(console[innerMethod]).toHaveBeenCalledWith(expect.any(String), 'foo')

console[innerMethod] = stored
})
console[innerMethod] = stored
})
})

it('creteError', () => {
const stored = console.error
console.error = vi.fn()
it('creteError', () => {
const stored = console.error
console.error = vi.fn()

expect(logger.createError()).toBeInstanceOf(Error)
expect(console.error).toHaveBeenCalled()
expect(logger.createError()).toBeInstanceOf(Error)
expect(console.error).toHaveBeenCalled()

console.error = stored
})
console.error = stored
})
68 changes: 68 additions & 0 deletions packages/utils/tests/console/withSpinner.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { beforeEach, expect, it, vi } from 'vitest'
import { withSpinner } from '../../src/index.js'

const mocks = vi.hoisted(() => {
const start = vi.fn()
const succeed = vi.fn()
const fail = vi.fn()
const ora = vi.fn().mockReturnValue({
start,
succeed,
fail,
})

return {
ora,
start,
succeed,
fail,
}
})

vi.mock('ora', () => ({
default: mocks.ora,
}))

beforeEach(() => {
Object.values(mocks).forEach((mock) => mock.mockClear())
})

it('should skip spinner if DEBUG env is set', async () => {
process.env.DEBUG = 'msg'

const target = vi.fn().mockResolvedValue('result')

const result = await withSpinner('msg')(target)

expect(result).toBe('result')
expect(target).toHaveBeenCalledTimes(1)
expect(mocks.ora).toHaveBeenCalledTimes(0)

delete process.env.DEBUG
})

it('should call target with spinner.succeed', async () => {
const target = vi.fn().mockResolvedValue('result')

const result = await withSpinner('msg')(target)

expect(result).toBe('result')
expect(target).toHaveBeenCalledTimes(1)
expect(mocks.ora).toHaveBeenCalledTimes(1)
expect(mocks.start).toHaveBeenCalledWith('msg')
expect(mocks.succeed).toHaveBeenCalledTimes(1)
expect(mocks.fail).toHaveBeenCalledTimes(0)
})

it('should call target with spinner.fail', async () => {
const error = new Error('mock error')
const target = vi.fn().mockRejectedValue(error)

await expect(() => withSpinner('msg')(target)).rejects.toThrowError(error)

expect(target).toHaveBeenCalledTimes(1)
expect(mocks.ora).toHaveBeenCalledTimes(1)
expect(mocks.start).toHaveBeenCalledWith('msg')
expect(mocks.succeed).toHaveBeenCalledTimes(0)
expect(mocks.fail).toHaveBeenCalledTimes(1)
})
12 changes: 4 additions & 8 deletions packages/utils/tests/module/isChildPath.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'
import { expect, it } from 'vitest'
import { isChildPath } from '../../src/index.js'

const testCases: [[string, string], boolean][] = [
Expand All @@ -22,12 +22,8 @@ const testCases: [[string, string], boolean][] = [
[['foo-bar', 'foo'], false],
]

describe('utils > isChildPath', () => {
describe('should check child path correctly', () => {
testCases.forEach(([source, expected]) => {
it(JSON.stringify(source), () => {
expect(isChildPath(...source)).toBe(expected)
})
})
testCases.forEach(([source, expected]) => {
it(`${JSON.stringify(source)} -> ${expected}`, () => {
expect(isChildPath(...source)).toBe(expected)
})
})
12 changes: 4 additions & 8 deletions packages/utils/tests/ssr/renderHead.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { HeadConfig } from '@vuepress/shared'
import { describe, expect, it } from 'vitest'
import { expect, it } from 'vitest'
import { renderHead } from '../../src/index.js'

const testCases: [HeadConfig, string][] = [
Expand Down Expand Up @@ -70,12 +70,8 @@ const testCases: [HeadConfig, string][] = [
],
]

describe('utils > renderHead', () => {
describe('should render head config correctly', () => {
testCases.forEach(([source, expected]) => {
it(expected, () => {
expect(renderHead(source)).toBe(expected)
})
})
testCases.forEach(([source, expected]) => {
it(expected, () => {
expect(renderHead(source)).toBe(expected)
})
})
12 changes: 4 additions & 8 deletions packages/utils/tests/ssr/renderHeadAttrs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { HeadAttrsConfig } from '@vuepress/shared'
import { describe, expect, it } from 'vitest'
import { expect, it } from 'vitest'
import { renderHeadAttrs } from '../../src/index.js'

const testCases: [HeadAttrsConfig, string][] = [
Expand All @@ -18,12 +18,8 @@ const testCases: [HeadAttrsConfig, string][] = [
],
]

describe('utils > renderHeadAttrs', () => {
describe('should render head attrs config correctly', () => {
testCases.forEach(([source, expected]) => {
it(JSON.stringify(source), () => {
expect(renderHeadAttrs(source)).toBe(expected)
})
})
testCases.forEach(([source, expected]) => {
it(JSON.stringify(source), () => {
expect(renderHeadAttrs(source)).toBe(expected)
})
})
60 changes: 60 additions & 0 deletions packages/utils/tests/ssr/templateRenderer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect, it } from 'vitest'
import type { TemplateRendererContext } from '../../src/index.js'
import { templateRenderer } from '../../src/index.js'

const template = `\
<!DOCTYPE html>
<html lang="{{ lang }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="VuePress {{ version }}">
<!--vuepress-ssr-head-->
<!--vuepress-ssr-styles-->
<!--vuepress-ssr-preload-->
<!--vuepress-ssr-prefetch-->
</head>
<body>
<div id="app"><!--vuepress-ssr-content--></div>
<!--vuepress-ssr-scripts-->
</body>
</html>
`

const context: TemplateRendererContext = {
content: '#content#',
head: '#head#',
lang: '#lang#',
prefetch: '#prefetch#',
preload: '#preload#',
scripts: '#scripts#',
styles: '#styles#',
version: '#version#',
}

const expected = `\
<!DOCTYPE html>
<html lang="#lang#">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="VuePress #version#">
#head#
#styles#
#preload#
#prefetch#
</head>
<body>
<div id="app">#content#</div>
#scripts#
</body>
</html>
`

it('should fill template outlets correctly', () => {
const result = templateRenderer(template, context)

expect(result).toBe(expected)
})

0 comments on commit eefa59d

Please sign in to comment.