1
- import { normalize } from "../src/common/util"
1
+ import { logger as l } from "@coder/logger"
2
+ import { arrayify , getFirstString , normalize , plural , resolveBase , split , trimSlashes } from "../src/common/util"
3
+
4
+ type LocationLike = Pick < Location , "pathname" | "origin" >
2
5
3
6
describe ( "util" , ( ) => {
4
7
describe ( "normalize" , ( ) => {
@@ -15,4 +18,106 @@ describe("util", () => {
15
18
expect ( normalize ( "qux" , true ) ) . toBe ( "qux" )
16
19
} )
17
20
} )
21
+
22
+ describe ( "split" , ( ) => {
23
+ it ( "should split at a comma" , ( ) => {
24
+ expect ( split ( "Hello,world" , "," ) ) . toStrictEqual ( [ "Hello" , "world" ] )
25
+ } )
26
+
27
+ it ( "shouldn't split if the delimiter doesn't exist" , ( ) => {
28
+ expect ( split ( "Hello world" , "," ) ) . toStrictEqual ( [ "Hello world" , "" ] )
29
+ } )
30
+ } )
31
+
32
+ describe ( "plural" , ( ) => {
33
+ it ( "should add an s if count is greater than 1" , ( ) => {
34
+ expect ( plural ( 2 , "dog" ) ) . toBe ( "dogs" )
35
+ } )
36
+ it ( "should NOT add an s if the count is 1" , ( ) => {
37
+ expect ( plural ( 1 , "dog" ) ) . toBe ( "dog" )
38
+ } )
39
+ } )
40
+
41
+ describe ( "trimSlashes" , ( ) => {
42
+ it ( "should remove leading slashes" , ( ) => {
43
+ expect ( trimSlashes ( "/hello-world" ) ) . toBe ( "hello-world" )
44
+ } )
45
+
46
+ it ( "should remove trailing slashes" , ( ) => {
47
+ expect ( trimSlashes ( "hello-world/" ) ) . toBe ( "hello-world" )
48
+ } )
49
+
50
+ it ( "should remove both leading and trailing slashes" , ( ) => {
51
+ expect ( trimSlashes ( "/hello-world/" ) ) . toBe ( "hello-world" )
52
+ } )
53
+
54
+ it ( "should remove multiple leading and trailing slashes" , ( ) => {
55
+ expect ( trimSlashes ( "///hello-world////" ) ) . toBe ( "hello-world" )
56
+ } )
57
+ } )
58
+
59
+ describe ( "resolveBase" , ( ) => {
60
+ beforeEach ( ( ) => {
61
+ const location : LocationLike = {
62
+ pathname : "/healthz" ,
63
+ origin : "http://localhost:8080" ,
64
+ }
65
+
66
+ // Because resolveBase is not a pure function
67
+ // and relies on the global location to be set
68
+ // we set it before all the tests
69
+ // and tell TS that our location should be looked at
70
+ // as Location (even though it's missing some properties)
71
+ global . location = location as Location
72
+ } )
73
+
74
+ it ( "should resolve a base" , ( ) => {
75
+ expect ( resolveBase ( "localhost:8080" ) ) . toBe ( "/localhost:8080" )
76
+ } )
77
+
78
+ it ( "should resolve a base with a forward slash at the beginning" , ( ) => {
79
+ expect ( resolveBase ( "/localhost:8080" ) ) . toBe ( "/localhost:8080" )
80
+ } )
81
+
82
+ it ( "should resolve a base with query params" , ( ) => {
83
+ expect ( resolveBase ( "localhost:8080?folder=hello-world" ) ) . toBe ( "/localhost:8080" )
84
+ } )
85
+
86
+ it ( "should resolve a base with a path" , ( ) => {
87
+ expect ( resolveBase ( "localhost:8080/hello/world" ) ) . toBe ( "/localhost:8080/hello/world" )
88
+ } )
89
+
90
+ it ( "should resolve a base to an empty string when not provided" , ( ) => {
91
+ expect ( resolveBase ( ) ) . toBe ( "" )
92
+ } )
93
+ } )
94
+
95
+ describe ( "arrayify" , ( ) => {
96
+ it ( "should return value it's already an array" , ( ) => {
97
+ expect ( arrayify ( [ "hello" , "world" ] ) ) . toStrictEqual ( [ "hello" , "world" ] )
98
+ } )
99
+ it ( "should wrap the value in an array if not an array" , ( ) => {
100
+ expect (
101
+ arrayify ( {
102
+ name : "Coder" ,
103
+ version : "3.8" ,
104
+ } ) ,
105
+ ) . toStrictEqual ( [ { name : "Coder" , version : "3.8" } ] )
106
+ } )
107
+ it ( "should return an empty array if the value is undefined" , ( ) => {
108
+ expect ( arrayify ( undefined ) ) . toStrictEqual ( [ ] )
109
+ } )
110
+ } )
111
+
112
+ describe ( "getFirstString" , ( ) => {
113
+ it ( "should return the string if passed a string" , ( ) => {
114
+ expect ( getFirstString ( "Hello world!" ) ) . toBe ( "Hello world!" )
115
+ } )
116
+ it ( "should get the first string from an array" , ( ) => {
117
+ expect ( getFirstString ( [ "Hello" , "World" ] ) ) . toBe ( "Hello" )
118
+ } )
119
+ it ( "should return undefined if the value isn't an array or a string" , ( ) => {
120
+ expect ( getFirstString ( { name : "Coder" } ) ) . toBe ( undefined )
121
+ } )
122
+ } )
18
123
} )
0 commit comments