Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
thalia
ThaliApp
Commits
c44efdb6
Commit
c44efdb6
authored
Oct 08, 2018
by
Gijs Hendriksen
Committed by
Gijs Hendriksen
Oct 12, 2018
Browse files
Added tests for the url helper
parent
9da4866d
Changes
2
Hide whitespace changes
Inline
Side-by-side
__tests__/utils/url.spec.js
View file @
c44efdb6
import
*
as
reactNativeLocaleDetector
from
'
react-native-locale-detector
'
;
import
{
apiRequest
,
apiUrl
,
defaultProfileImage
,
termsAndConditionsUrl
,
ServerError
,
url
,
TokenInvalidError
,
}
from
'
../../app/utils/url
'
;
const
fetchPromiseResult
=
{
...
...
@@ -12,19 +15,25 @@ const fetchPromiseResult = {
clone
:
global
.
fetch
,
};
global
.
fetch
=
jest
.
fn
().
mockReturnValue
(
Promise
.
resolve
(
fetchPromiseResult
));
Promise
.
resolve
(
fetchPromiseResult
),
);
fetchPromiseResult
.
clone
=
global
.
fetch
;
jest
.
mock
(
'
react-native-locale-detector
'
,
()
=>
'
en
'
);
jest
.
mock
(
'
react-native-locale-detector
'
,
()
=>
({
__esModule
:
true
,
default
:
'
nl
'
,
}));
describe
(
'
url helper
'
,
()
=>
{
beforeEach
(()
=>
{
});
it
(
'
should expose the constants
'
,
()
=>
{
const
{
url
}
=
require
(
'
../../app/utils/url
'
);
expect
(
url
).
toEqual
(
'
http://localhost:8000
'
);
expect
(
apiUrl
).
toEqual
(
'
http://localhost:8000/api/v1
'
);
expect
(
defaultProfileImage
).
toEqual
(
'
http://localhost:8000/static/members/images/default-avatar.jpg
'
);
expect
(
termsAndConditionsUrl
).
toEqual
(
'
http://localhost:8000/event-registration-terms/
'
);
});
it
(
'
should do a fetch request
'
,
()
=>
{
...
...
@@ -32,7 +41,7 @@ describe('url helper', () => {
return
apiRequest
(
'
route
'
,
{},
null
)
.
then
((
response
)
=>
{
expect
(
global
.
fetch
).
toBeCalledWith
(
`
${
apiUrl
}
/route/`
,
{
headers
:
{
'
Accept-Language
'
:
'
e
n
'
}
});
{
headers
:
{
'
Accept-Language
'
:
'
n
l
'
}
});
expect
(
response
).
toEqual
(
'
responseJson
'
);
});
});
...
...
@@ -43,7 +52,7 @@ describe('url helper', () => {
params
:
'
value
'
,
}).
then
(()
=>
{
expect
(
global
.
fetch
).
toBeCalledWith
(
`
${
apiUrl
}
/route/?params=value`
,
{
headers
:
{
'
Accept-Language
'
:
'
e
n
'
}
});
{
headers
:
{
'
Accept-Language
'
:
'
n
l
'
}
});
});
});
...
...
@@ -51,7 +60,7 @@ describe('url helper', () => {
expect
.
assertions
(
1
);
return
apiRequest
(
'
route
'
,
{
headers
:
{
Authorization
:
'
Token abc
'
}
},
null
).
then
(()
=>
{
expect
(
global
.
fetch
).
toBeCalledWith
(
`
${
apiUrl
}
/route/`
,
{
headers
:
{
'
Accept-Language
'
:
'
e
n
'
,
Authorization
:
'
Token abc
'
}
});
{
headers
:
{
'
Accept-Language
'
:
'
n
l
'
,
Authorization
:
'
Token abc
'
}
});
});
});
...
...
@@ -60,7 +69,7 @@ describe('url helper', () => {
return
apiRequest
(
'
route
'
,
{},
null
)
.
then
((
response
)
=>
{
expect
(
global
.
fetch
).
toBeCalledWith
(
`
${
apiUrl
}
/route/`
,
{
headers
:
{
'
Accept-Language
'
:
'
e
n
'
}
});
{
headers
:
{
'
Accept-Language
'
:
'
n
l
'
}
});
expect
(
response
).
toEqual
(
'
responseJson
'
);
});
});
...
...
@@ -88,4 +97,60 @@ describe('url helper', () => {
return
apiRequest
(
'
route
'
,
{},
null
)
.
then
(
res
=>
expect
(
res
).
toEqual
({}));
});
it
(
'
should detect an invalid token in English
'
,
()
=>
{
expect
.
assertions
(
1
);
const
response
=
{
status
:
403
,
headers
:
{
get
:
key
=>
(
key
===
'
content-language
'
?
'
en
'
:
'
nl
'
)
},
json
:
()
=>
Promise
.
resolve
({
detail
:
'
Invalid token.
'
}),
clone
:
()
=>
'
responseCopy
'
,
};
global
.
fetch
.
mockReturnValue
(
Promise
.
resolve
(
response
));
return
apiRequest
(
'
route
'
,
{},
null
)
.
catch
(
e
=>
expect
(
e
).
toEqual
(
new
TokenInvalidError
(
'
responseCopy
'
)));
});
it
(
'
should detect an invalid token in Dutch
'
,
()
=>
{
expect
.
assertions
(
1
);
const
response
=
{
status
:
403
,
headers
:
{
get
:
key
=>
(
key
===
'
content-language
'
?
'
nl
'
:
'
en
'
)
},
json
:
()
=>
Promise
.
resolve
({
detail
:
'
Ongeldige token.
'
}),
clone
:
()
=>
'
responseCopy
'
,
};
global
.
fetch
.
mockReturnValue
(
Promise
.
resolve
(
response
));
return
apiRequest
(
'
route
'
,
{},
null
)
.
catch
(
e
=>
expect
(
e
).
toEqual
(
new
TokenInvalidError
(
'
responseCopy
'
)));
});
it
(
'
should not falsely claim the token is incorrect
'
,
()
=>
{
expect
.
assertions
(
1
);
const
response
=
{
status
:
403
,
headers
:
{
get
:
key
=>
(
key
===
'
content-language
'
?
'
en
'
:
'
nl
'
)
},
json
:
()
=>
Promise
.
resolve
({
detail
:
'
Not authorized.
'
}),
clone
:
()
=>
({
json
:
()
=>
'
jsonResult
'
}),
};
global
.
fetch
.
mockReturnValue
(
Promise
.
resolve
(
response
));
return
apiRequest
(
'
route
'
,
{},
null
)
.
then
(
res
=>
expect
(
res
).
toEqual
(
'
jsonResult
'
));
});
it
(
'
should default to an English locales
'
,
()
=>
{
reactNativeLocaleDetector
.
default
=
'
fr
'
;
expect
.
assertions
(
1
);
return
apiRequest
(
'
route
'
,
{},
null
)
.
then
(()
=>
{
expect
(
global
.
fetch
).
toBeCalledWith
(
`
${
apiUrl
}
/route/`
,
{
headers
:
{
'
Accept-Language
'
:
'
en
'
}
});
});
});
it
(
'
should use the correct url when in production mode
'
,
()
=>
{
jest
.
resetModules
();
global
.
__DEV__
=
false
;
const
{
url
}
=
require
(
'
../../app/utils/url
'
);
expect
(
url
).
toEqual
(
'
https://thalia.nu
'
);
});
});
app/utils/url.js
View file @
c44efdb6
...
...
@@ -32,12 +32,10 @@ const detectInvalidToken = (response) => {
if
(
response
.
status
===
403
)
{
return
response
.
json
().
then
((
json
)
=>
{
if
(
response
.
status
===
403
)
{
const
contentLang
=
response
.
headers
.
get
(
'
content-language
'
);
if
((
contentLang
===
'
en
'
&&
json
.
detail
===
'
Invalid token.
'
)
||
(
contentLang
===
'
nl
'
&&
json
.
detail
===
'
Ongeldige token.
'
))
{
throw
new
TokenInvalidError
(
responseCopy
);
}
const
contentLang
=
response
.
headers
.
get
(
'
content-language
'
);
if
((
contentLang
===
'
en
'
&&
json
.
detail
===
'
Invalid token.
'
)
||
(
contentLang
===
'
nl
'
&&
json
.
detail
===
'
Ongeldige token.
'
))
{
throw
new
TokenInvalidError
(
responseCopy
);
}
return
responseCopy
;
});
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment