1
- import { logger as l } from "@coder/logger"
2
- import { arrayify , getFirstString , normalize , plural , resolveBase , split , trimSlashes } from "../src/common/util"
1
+ // Note: we need to import logger from the root
2
+ // because this is the logger used in logError in ../src/common/util
3
+ import { logger } from "../node_modules/@coder/logger"
4
+ import {
5
+ arrayify ,
6
+ getFirstString ,
7
+ logError ,
8
+ normalize ,
9
+ plural ,
10
+ resolveBase ,
11
+ split ,
12
+ trimSlashes ,
13
+ } from "../src/common/util"
3
14
4
15
type LocationLike = Pick < Location , "pathname" | "origin" >
5
16
@@ -96,6 +107,7 @@ describe("util", () => {
96
107
it ( "should return value it's already an array" , ( ) => {
97
108
expect ( arrayify ( [ "hello" , "world" ] ) ) . toStrictEqual ( [ "hello" , "world" ] )
98
109
} )
110
+
99
111
it ( "should wrap the value in an array if not an array" , ( ) => {
100
112
expect (
101
113
arrayify ( {
@@ -104,6 +116,7 @@ describe("util", () => {
104
116
} ) ,
105
117
) . toStrictEqual ( [ { name : "Coder" , version : "3.8" } ] )
106
118
} )
119
+
107
120
it ( "should return an empty array if the value is undefined" , ( ) => {
108
121
expect ( arrayify ( undefined ) ) . toStrictEqual ( [ ] )
109
122
} )
@@ -113,11 +126,46 @@ describe("util", () => {
113
126
it ( "should return the string if passed a string" , ( ) => {
114
127
expect ( getFirstString ( "Hello world!" ) ) . toBe ( "Hello world!" )
115
128
} )
129
+
116
130
it ( "should get the first string from an array" , ( ) => {
117
131
expect ( getFirstString ( [ "Hello" , "World" ] ) ) . toBe ( "Hello" )
118
132
} )
133
+
119
134
it ( "should return undefined if the value isn't an array or a string" , ( ) => {
120
135
expect ( getFirstString ( { name : "Coder" } ) ) . toBe ( undefined )
121
136
} )
122
137
} )
138
+
139
+ describe ( "logError" , ( ) => {
140
+ let spy : jest . SpyInstance
141
+
142
+ beforeEach ( ( ) => {
143
+ spy = jest . spyOn ( logger , "error" )
144
+ } )
145
+
146
+ afterEach ( ( ) => {
147
+ jest . clearAllMocks ( )
148
+ } )
149
+
150
+ afterAll ( ( ) => {
151
+ jest . restoreAllMocks ( )
152
+ } )
153
+
154
+ it ( "should log an error with the message and stack trace" , ( ) => {
155
+ const message = "You don't have access to that folder."
156
+ const error = new Error ( message )
157
+
158
+ logError ( "ui" , error )
159
+
160
+ expect ( spy ) . toHaveBeenCalled ( )
161
+ expect ( spy ) . toHaveBeenCalledWith ( `ui: ${ error . message } ${ error . stack } ` )
162
+ } )
163
+
164
+ it ( "should log an error, even if not an instance of error" , ( ) => {
165
+ logError ( "api" , "oh no" )
166
+
167
+ expect ( spy ) . toHaveBeenCalled ( )
168
+ expect ( spy ) . toHaveBeenCalledWith ( "api: oh no" )
169
+ } )
170
+ } )
123
171
} )
0 commit comments