Skip to content

Commit

Permalink
refactor(test/ui): add more ui api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Dec 28, 2023
1 parent cf3f9b1 commit a5b428d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
3 changes: 3 additions & 0 deletions docs/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"markdown-it-for-inline": "^2.0.1",
"sass": "^1.69.5",
"vitepress": "1.0.0-rc.32"
},
"dependencies": {
"vue": "^3.3.12"
}
}
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 18 additions & 23 deletions ui/artalk/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import type { ApiOptions } from './api/_options'
import { mergeDeep } from './lib/merge-deep'
import Defaults from './defaults'


/**
* Handle the custom config which is provided by the user
*
* @param customConf - The custom config object which is provided by the user
* @param full - If `full` is `true`, the return value will be the complete config for Artalk instance creation
* @returns The config for Artalk instance creation
*/
export function handelCustomConf(customConf: Partial<ArtalkConfig>, mergeDefault: true): ArtalkConfig
export function handelCustomConf(customConf: Partial<ArtalkConfig>, mergeDefault?: false): Partial<ArtalkConfig>
export function handelCustomConf(customConf: Partial<ArtalkConfig>, mergeDefault = false) {
export function handelCustomConf(customConf: Partial<ArtalkConfig>, full: true): ArtalkConfig
export function handelCustomConf(customConf: Partial<ArtalkConfig>, full?: false): Partial<ArtalkConfig>
export function handelCustomConf(customConf: Partial<ArtalkConfig>, full = false) {
// 合并默认配置
const conf: Partial<ArtalkConfig> = mergeDefault ? mergeDeep(Defaults, customConf) : customConf
const conf: Partial<ArtalkConfig> = full ? mergeDeep(Defaults, customConf) : customConf

// 绑定元素
if (typeof conf.el === 'string' && !!conf.el) {
if (conf.el && typeof conf.el === 'string') {
try {
const findEl = document.querySelector<HTMLElement>(conf.el)
if (!findEl) throw Error(`Target element "${conf.el}" was not found.`)
Expand All @@ -28,35 +28,30 @@ export function handelCustomConf(customConf: Partial<ArtalkConfig>, mergeDefault
}
}

// 服务器配置
if (conf.server) {
conf.server = conf.server.replace(/\/$/, '').replace(/\/api\/?$/, '')
}

// 默认 pageKey
if (!conf.pageKey) {
// @link http://bl.ocks.org/abernier/3070589
conf.pageKey = `${window.location.pathname}`
}
if (conf.pageKey === '')
conf.pageKey = `${window.location.pathname}` // @link http://bl.ocks.org/abernier/3070589

// 默认 pageTitle
if (!conf.pageTitle) {
if (conf.pageTitle === '')
conf.pageTitle = `${document.title}`
}

// 服务器配置
if (conf.server)
conf.server = conf.server.replace(/\/$/, '').replace(/\/api\/?$/, '')

// 自适应语言
if (conf.locale === 'auto') {
if (conf.locale === 'auto')
conf.locale = navigator.language
}

// flatMode
if (conf.flatMode === true || Number(conf.nestMax) <= 1)
conf.flatMode = true

// 自动判断启用平铺模式
if (conf.flatMode === 'auto')
conf.flatMode = window.matchMedia("(max-width: 768px)").matches

// flatMode
if (typeof conf.nestMax === 'number' && Number(conf.nestMax) <= 1)
conf.flatMode = true

return conf
}

Expand Down
17 changes: 12 additions & 5 deletions ui/artalk/tests/ui-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, it, expect, vi, beforeAll } from 'vitest'
import Artalk from '@/artalk'

const InitConf = {
pageTitle: 'Artalk DEMO',
pageKey: '/unit_test_page.html?test=1',
server: 'http://localhost:3000/api',
site: 'Unit Test Page',
Expand Down Expand Up @@ -65,18 +66,16 @@ describe('Artalk instance', () => {
document.body.appendChild(el)

artalk = Artalk.init({
...InitConf,
el,
pageKey: InitConf.pageKey,
server: InitConf.server,
site: InitConf.site,
darkMode: InitConf.darkMode,

immediateFetch: false, // for testing
})

expect(artalk).toBeInstanceOf(Artalk)
})

let confCopy: any

it('should have correct config (artalk.getConf, artalk.getEl)', () => {
const conf = artalk.getConf()
expect(conf.pageKey).toBe(InitConf.pageKey)
Expand All @@ -85,6 +84,8 @@ describe('Artalk instance', () => {
expect(conf.darkMode).toBe(InitConf.darkMode)

expect(artalk.getEl().classList.contains('atk-dark-mode')).toBe(true)

confCopy = JSON.parse(JSON.stringify(conf))
})

it('should can listen to events and the conf-remoter works (artalk.trigger, artalk.on, conf-remoter)', async () => {
Expand All @@ -108,6 +109,12 @@ describe('Artalk instance', () => {
timeout: 1000
})

it('should other config not changed after conf-remoter loaded (conf-remoter, artalk.update)', () => {
const confNew = JSON.parse(JSON.stringify(artalk.getConf()))
confCopy.gravatar = confNew.gravatar // exclude
expect(confCopy).toMatchObject(confNew)
})

it('should can update config (artalk.update)', () => {
const Placeholder = 'Test Placeholder'
artalk.update({ placeholder: Placeholder })
Expand Down

0 comments on commit a5b428d

Please sign in to comment.