Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
镜像
Coder
code-server
提交
4f6efced
C
code-server
项目概览
镜像
/
Coder
/
code-server
2022-09-21 03:15:05同步失败
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
code-server
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
4f6efced
编写于
2月 03, 2021
作者:
J
Joe Previte
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: add tests for getOptions
上级
323339d1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
92 addition
and
1 deletion
+92
-1
test/package.json
test/package.json
+1
-1
test/util.test.ts
test/util.test.ts
+91
-0
未找到文件。
test/package.json
浏览文件 @
4f6efced
{
"#"
:
"the types and generates conflicts with mocha."
,
"#"
:
"
We must put jest in a sub-directory otherwise VS Code somehow picks up
the types and generates conflicts with mocha."
,
"devDependencies"
:
{
"@types/jest"
:
"^26.0.20"
,
"@types/jsdom"
:
"^16.2.6"
,
...
...
test/util.test.ts
浏览文件 @
4f6efced
import
{
JSDOM
}
from
"
jsdom
"
// Note: we need to import logger from the root
// because this is the logger used in logError in ../src/common/util
import
{
logger
}
from
"
../node_modules/@coder/logger
"
import
{
arrayify
,
generateUuid
,
getFirstString
,
getOptions
,
logError
,
normalize
,
plural
,
...
...
@@ -12,6 +15,10 @@ import {
trimSlashes
,
}
from
"
../src/common/util
"
const
dom
=
new
JSDOM
()
global
.
document
=
dom
.
window
.
document
// global.window = (dom.window as unknown) as Window & typeof globalThis
type
LocationLike
=
Pick
<
Location
,
"
pathname
"
|
"
origin
"
>
describe
(
"
util
"
,
()
=>
{
...
...
@@ -49,6 +56,20 @@ describe("util", () => {
})
})
describe
(
"
generateUuid
"
,
()
=>
{
it
(
"
should generate a unique uuid
"
,
()
=>
{
const
uuid
=
generateUuid
()
const
uuid2
=
generateUuid
()
expect
(
uuid
).
toHaveLength
(
24
)
expect
(
typeof
uuid
).
toBe
(
"
string
"
)
expect
(
uuid
).
not
.
toBe
(
uuid2
)
})
it
(
"
should generate a uuid of a specific length
"
,
()
=>
{
const
uuid
=
generateUuid
(
10
)
expect
(
uuid
).
toHaveLength
(
10
)
})
})
describe
(
"
trimSlashes
"
,
()
=>
{
it
(
"
should remove leading slashes
"
,
()
=>
{
expect
(
trimSlashes
(
"
/hello-world
"
)).
toBe
(
"
hello-world
"
)
...
...
@@ -103,6 +124,76 @@ describe("util", () => {
})
})
describe
(
"
getOptions
"
,
()
=>
{
// Things to mock
// logger
// location
// document
beforeEach
(()
=>
{
const
location
:
LocationLike
=
{
pathname
:
"
/healthz
"
,
origin
:
"
http://localhost:8080
"
,
// search: "?environmentId=600e0187-0909d8a00cb0a394720d4dce",
}
// Because resolveBase is not a pure function
// and relies on the global location to be set
// we set it before all the tests
// and tell TS that our location should be looked at
// as Location (even though it's missing some properties)
global
.
location
=
location
as
Location
})
afterEach
(()
=>
{
jest
.
restoreAllMocks
()
})
it
(
"
should return options with base and cssStaticBase even if it doesn't exist
"
,
()
=>
{
expect
(
getOptions
()).
toStrictEqual
({
base
:
""
,
csStaticBase
:
""
,
})
})
it
(
"
should return options when they do exist
"
,
()
=>
{
// Mock getElementById
const
spy
=
jest
.
spyOn
(
document
,
"
getElementById
"
)
// Create a fake element and set the attribute
const
mockElement
=
document
.
createElement
(
"
div
"
)
mockElement
.
setAttribute
(
"
data-settings
"
,
'
{"base":".","csStaticBase":"./static/development/Users/jp/Dev/code-server","logLevel":2,"disableTelemetry":false,"disableUpdateCheck":false}
'
,
)
// Return mockElement from the spy
// this way, when we call "getElementById"
// it returns the element
spy
.
mockImplementation
(()
=>
mockElement
)
expect
(
getOptions
()).
toStrictEqual
({
base
:
""
,
csStaticBase
:
"
/static/development/Users/jp/Dev/code-server
"
,
disableTelemetry
:
false
,
disableUpdateCheck
:
false
,
logLevel
:
2
,
})
})
it
(
"
should include queryOpts
"
,
()
=>
{
// Trying to understand how the implementation works
// 1. It grabs the search params from location.search (i.e. ?)
// 2. it then grabs the "options" param if it exists
// 3. then it creates a new options object
// spreads the original options
// then parses the queryOpts
location
.
search
=
'
?options={"logLevel":2}
'
expect
(
getOptions
()).
toStrictEqual
({
base
:
""
,
csStaticBase
:
""
,
logLevel
:
2
,
})
})
})
describe
(
"
arrayify
"
,
()
=>
{
it
(
"
should return value it's already an array
"
,
()
=>
{
expect
(
arrayify
([
"
hello
"
,
"
world
"
])).
toStrictEqual
([
"
hello
"
,
"
world
"
])
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录