-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
173 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |