Skip to content
GitLab
Menu
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
bc65edbe
Commit
bc65edbe
authored
Feb 15, 2018
by
Wietse Kuipers
Browse files
Merge branch 'test-url-code' into 'master'
Add test for url helper See merge request
!146
parents
1226e4c4
195af211
Changes
2
Hide whitespace changes
Inline
Side-by-side
__tests__/url.spec.js
0 → 100644
View file @
bc65edbe
import
{
url
,
apiUrl
,
defaultProfileImage
,
tokenSelector
,
loggedInSelector
,
apiRequest
,
ServerError
,
}
from
'
../app/url
'
;
global
.
fetch
=
jest
.
fn
().
mockReturnValue
(
Promise
.
resolve
({
status
:
200
,
json
:
()
=>
'
responseJson
'
}));
describe
(
'
url helper
'
,
()
=>
{
beforeEach
(()
=>
{
});
it
(
'
should expose the constants
'
,
()
=>
{
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
'
);
});
it
(
'
should expose the selectors
'
,
()
=>
{
expect
(
tokenSelector
({
session
:
{
token
:
'
abc123
'
}
})).
toEqual
(
'
abc123
'
);
expect
(
loggedInSelector
({
navigation
:
{
loggedIn
:
true
}
})).
toEqual
(
true
);
});
it
(
'
should do a fetch request
'
,
()
=>
{
expect
.
assertions
(
2
);
return
apiRequest
(
'
route
'
,
'
fetchOpts
'
,
null
)
.
then
((
response
)
=>
{
expect
(
global
.
fetch
).
toBeCalledWith
(
`
${
apiUrl
}
/route/`
,
'
fetchOpts
'
);
expect
(
response
).
toEqual
(
'
responseJson
'
);
});
});
it
(
'
should do a fetch request with params
'
,
()
=>
{
expect
.
assertions
(
1
);
return
apiRequest
(
'
route
'
,
'
fetchOpts
'
,
{
params
:
'
value
'
,
}).
then
(()
=>
{
expect
(
global
.
fetch
).
toBeCalledWith
(
`
${
apiUrl
}
/route/?params=value`
,
'
fetchOpts
'
);
});
});
it
(
'
should generate the url parameters
'
,
()
=>
{
expect
.
assertions
(
2
);
return
apiRequest
(
'
route
'
,
'
fetchOpts
'
,
null
)
.
then
((
response
)
=>
{
expect
(
global
.
fetch
).
toBeCalledWith
(
`
${
apiUrl
}
/route/`
,
'
fetchOpts
'
);
expect
(
response
).
toEqual
(
'
responseJson
'
);
});
});
it
(
'
should throw a server error
'
,
()
=>
{
expect
.
assertions
(
1
);
const
response
=
{
status
:
404
,
json
:
()
=>
'
responseJson
'
};
global
.
fetch
.
mockReturnValue
(
Promise
.
resolve
(
response
));
return
apiRequest
(
'
route
'
,
'
fetchOpts
'
,
null
)
.
catch
(
e
=>
expect
(
e
).
toEqual
(
new
ServerError
(
'
Invalid status code: 404
'
,
response
)));
});
it
(
'
should return an empty response on status 204
'
,
()
=>
{
expect
.
assertions
(
1
);
const
response
=
{
status
:
204
,
json
:
()
=>
'
responseJson
'
};
global
.
fetch
.
mockReturnValue
(
Promise
.
resolve
(
response
));
return
apiRequest
(
'
route
'
,
'
fetchOpts
'
,
null
)
.
then
(
res
=>
expect
(
res
).
toEqual
({}));
});
});
app/url.js
View file @
bc65edbe
let
server
=
'
https://thalia.nu
'
;
/* istanbul ignore next line */
if
(
__DEV__
)
{
// eslint-disable-line no-undef
server
=
'
http://localhost:8000
'
;
}
export
const
url
=
server
;
export
const
apiUrl
=
`
${
server
}
/api/v1`
;
export
const
pizzaUrl
=
'
https://pizza.thalia.nu
'
;
export
const
defaultProfileImage
=
`
${
server
}
/static/members/images/default-avatar.jpg`
;
export
const
tokenSelector
=
state
=>
state
.
session
.
token
;
export
const
loggedInSelector
=
state
=>
state
.
navigation
.
loggedIn
;
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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