Skip to content

test(utils): add vitest for utils package #2810

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

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type {
} from '../file-upload'

import { extend } from '@mobile-root/utils/object'
import { xss, log } from '@mobile-root/utils/xss'
import { xss, log } from '@mobile-root/utils'
import uploadAjax from '@mobile-root/utils/deps/upload-ajax'
import { isObject } from '@mobile-root/utils/type'
import { isEmptyObject } from '@mobile-root/utils/type'
Expand Down Expand Up @@ -577,7 +577,7 @@ export const handleStart =
state,
vm
}: Pick<IFileUploadRenderlessParams, 'api' | 'constants' | 'props' | 'state' | 'vm'>) =>
(rawFiles: IFileUploadFile[], updateId: string, reUpload: boolean = false) => {
(rawFiles: IFileUploadFile[], updateId: string, reUpload = false) => {
if (state.isHwh5) {
rawFiles = handleHwh5Files(rawFiles, props.hwh5)
}
Expand Down Expand Up @@ -921,7 +921,7 @@ export const abort =

export const abortDownload =
({ state }: Pick<IFileUploadRenderlessParams, 'state'>) =>
(file: IFileUploadFile, batch: boolean = false) => {
(file: IFileUploadFile, batch = false) => {
const cancel = (docId) => {
if (!docId) return
const cancels = state.downloadCancelToken[docId]
Expand Down Expand Up @@ -2246,7 +2246,7 @@ export const getToken =

export const previewFile =
({ api, props }: Pick<IFileUploadRenderlessParams, 'api' | 'props'>) =>
(file: IFileUploadFile, open: boolean = false) => {
(file: IFileUploadFile, open = false) => {
return new Promise((resolve, reject) => {
try {
const tokenParams = { isOnlinePreview: true, file, type: 'preview', token: props.edm.preview.token }
Expand Down
6 changes: 1 addition & 5 deletions packages/renderless/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
*/

import { log as uLog, xss } from '@opentiny/utils'
import { xss } from '@opentiny/utils'

export const KEY_CODE = {
Backspace: 8,
Expand Down Expand Up @@ -264,8 +264,4 @@ export const CASCADER = {

export const version = process.env.RUNTIME_VERSION

export const log = (data, type = 'log') => {
uLog.logger[type](data)
}

export { xss }
2 changes: 1 addition & 1 deletion packages/renderless/src/tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { on, off } from '../common/deps/dom'
import { getDataset } from '../common/dataset'
import { copyArray } from '../common/object'

import { log } from '../common'
import { log } from '@opentiny/utils'

export const setChildren = (props) => (data) => (props.data = data)

Expand Down
6 changes: 5 additions & 1 deletion packages/utils/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# @opentiny/utils
## 安装

```bash
npm install --save @opentiny/utils
```
8 changes: 5 additions & 3 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@opentiny/utils",
"type": "module",
"version": "1.0.0",
"description": "nanoid console xss",
"author": "",
"license": "ISC",
"type": "module",
"repository": {
"type": "git",
"url": "git@github.com:opentiny/tiny-vue.git"
Expand All @@ -16,14 +16,16 @@
],
"scripts": {
"build": "vite build",
"pub": "pnpm publish --no-git-checks --access=public"
"pub": "pnpm publish --no-git-checks --access=public",
"test": "vitest"
},
"dependencies": {
"xss": "1.0.14"
},
"devDependencies": {
"typescript": "catalog:",
"vite": "catalog:",
"vite-plugin-dts": "~4.3.0",
"vite": "catalog:"
"vitest": "catalog:"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`测试sha256 1`] = `"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"`;
7 changes: 7 additions & 0 deletions packages/utils/src/crypt/__test__/crypt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test, expect } from 'vitest'
import { sha256 } from '../index'

test('测试sha256', async () => {
// 简单记录加密的结果,测试用来保证sha256算法不变化
expect(await sha256('hello world')).toMatchSnapshot()
})
Comment on lines +4 to +7
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test coverage and clarity.

A few suggestions to improve the test suite:

  1. Consider using English for test descriptions to maintain consistency with code
  2. Instead of snapshot testing, explicitly verify the hash value for 'hello world'
  3. Add test cases for error scenarios (empty input, non-string input, etc.)

Example implementation:

test('sha256 should generate correct hash for known input', async () => {
  const result = await sha256('hello world')
  expect(result).toBe('b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9')
})

test('sha256 should handle empty string', async () => {
  const result = await sha256('')
  expect(result).toBe('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')
})

test('sha256 should reject non-string input', async () => {
  await expect(sha256(123 as any)).rejects.toThrow()
})

Loading
Loading