File tree Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Expand file tree Collapse file tree 2 files changed +24
-4
lines changed Original file line number Diff line number Diff line change @@ -138,9 +138,13 @@ export function convertKeyNames(object, nameMap) {
138
138
* @returns {Object }
139
139
*/
140
140
export function parseURL ( url ) {
141
- const parser = document ?. createElement ( 'a' ) ;
142
- parser . href = url ;
143
- return parser ;
141
+ if ( typeof document !== 'undefined' ) {
142
+ const parser = document . createElement ( 'a' ) ;
143
+ parser . href = url ;
144
+ return parser ;
145
+ }
146
+
147
+ return { } ;
144
148
}
145
149
146
150
/**
@@ -151,7 +155,7 @@ export function parseURL(url) {
151
155
* @returns {string }
152
156
*/
153
157
export function getPath ( url ) {
154
- return parseURL ( url ) . pathname ;
158
+ return typeof document !== 'undefined' ? parseURL ( url ) ? .pathname : '' ;
155
159
}
156
160
157
161
/**
Original file line number Diff line number Diff line change @@ -119,6 +119,16 @@ describe('getQueryParameters', () => {
119
119
describe ( 'ParseURL' , ( ) => {
120
120
const testURL = 'http://example.com:3000/pathname/?search=test#hash' ;
121
121
const parsedURL = parseURL ( testURL ) ;
122
+ let originalDocument ;
123
+
124
+ beforeEach ( ( ) => {
125
+ originalDocument = global . document ;
126
+ } ) ;
127
+
128
+ afterEach ( ( ) => {
129
+ global . document = originalDocument ;
130
+ } ) ;
131
+
122
132
it ( 'String URL is correctly parsed' , ( ) => {
123
133
expect ( parsedURL . toString ( ) ) . toEqual ( testURL ) ;
124
134
expect ( parsedURL . href ) . toEqual ( testURL ) ;
@@ -152,6 +162,12 @@ describe('ParseURL', () => {
152
162
it ( 'should return host from URL' , ( ) => {
153
163
expect ( parsedURL . host ) . toEqual ( 'example.com:3000' ) ;
154
164
} ) ;
165
+
166
+ it ( 'should return empty object in case of document being undefined' , ( ) => {
167
+ delete global . document ;
168
+
169
+ expect ( parseURL ( testURL ) ) . toEqual ( { } ) ;
170
+ } ) ;
155
171
} ) ;
156
172
157
173
describe ( 'getPath' , ( ) => {
You can’t perform that action at this time.
0 commit comments