Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
b1850676
Commit
b1850676
authored
Jul 20, 2017
by
Luke "Jared" Bennett
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tidied up repo_service and added tests
parent
7728412d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
150 additions
and
21 deletions
+150
-21
app/assets/javascripts/repo/repo_service.js
app/assets/javascripts/repo/repo_service.js
+29
-21
spec/javascripts/repo/repo_service_spec.js
spec/javascripts/repo/repo_service_spec.js
+121
-0
No files found.
app/assets/javascripts/repo/repo_service.js
View file @
b1850676
...
@@ -2,38 +2,46 @@ import axios from 'axios';
...
@@ -2,38 +2,46 @@ import axios from 'axios';
const
RepoService
=
{
const
RepoService
=
{
url
:
''
,
url
:
''
,
param
s
:
{
option
s
:
{
params
:
{
params
:
{
format
:
'
json
'
,
format
:
'
json
'
,
},
},
},
},
richExtensionRegExp
:
/md/
,
setUrl
(
url
)
{
buildParams
(
url
=
this
.
url
)
{
this
.
url
=
url
;
// shallow clone object without reference
},
const
params
=
Object
.
assign
({},
this
.
options
.
params
);
if
(
this
.
urlIsRichBlob
(
url
))
params
.
viewer
=
'
rich
'
;
paramsWithRich
(
url
)
{
// copy the obj so we don't modify perm.
const
params
=
JSON
.
parse
(
JSON
.
stringify
(
this
.
params
));
if
(
url
.
substr
(
url
.
length
-
2
)
===
'
md
'
)
{
params
.
params
.
viewer
=
'
rich
'
;
}
return
params
;
return
params
;
},
},
getContent
(
url
)
{
urlIsRichBlob
(
url
=
this
.
url
)
{
if
(
url
)
{
const
extension
=
url
.
split
(
'
.
'
).
pop
();
return
axios
.
get
(
url
,
this
.
paramsWithRich
(
url
,
this
.
params
));
}
return
this
.
richExtensionRegExp
.
test
(
extension
);
return
axios
.
get
(
this
.
url
,
this
.
paramsWithRich
(
this
.
url
,
this
.
params
));
},
getContent
(
url
=
this
.
url
)
{
const
params
=
this
.
buildParams
(
url
);
return
axios
.
get
(
url
,
{
params
,
});
},
getBase64Content
(
url
=
this
.
url
)
{
const
request
=
axios
.
get
(
url
,
{
responseType
:
'
arraybuffer
'
,
});
return
request
.
then
(
response
=>
this
.
bufferToBase64
(
response
.
data
));
},
},
getBase64Content
(
url
)
{
bufferToBase64
(
data
)
{
return
axios
return
new
Buffer
(
data
,
'
binary
'
).
toString
(
'
base64
'
);
.
get
(
url
,
{
responseType
:
'
arraybuffer
'
,
})
.
then
(
response
=>
new
Buffer
(
response
.
data
,
'
binary
'
).
toString
(
'
base64
'
));
},
},
};
};
...
...
spec/javascripts/repo/repo_service_spec.js
0 → 100644
View file @
b1850676
import
axios
from
'
axios
'
;
import
RepoService
from
'
~/repo/repo_service
'
;
describe
(
'
RepoService
'
,
()
=>
{
it
(
'
has default json format param
'
,
()
=>
{
expect
(
RepoService
.
options
.
params
.
format
).
toBe
(
'
json
'
);
});
describe
(
'
buildParams
'
,
()
=>
{
let
newParams
;
const
url
=
'
url
'
;
beforeEach
(()
=>
{
newParams
=
{};
spyOn
(
Object
,
'
assign
'
).
and
.
returnValue
(
newParams
);
});
it
(
'
clones params
'
,
()
=>
{
const
params
=
RepoService
.
buildParams
(
url
);
expect
(
Object
.
assign
).
toHaveBeenCalledWith
({},
RepoService
.
options
.
params
);
expect
(
params
).
toBe
(
newParams
);
});
it
(
'
sets and returns viewer params to richif urlIsRichBlob is true
'
,
()
=>
{
spyOn
(
RepoService
,
'
urlIsRichBlob
'
).
and
.
returnValue
(
true
);
const
params
=
RepoService
.
buildParams
(
url
);
expect
(
params
.
viewer
).
toEqual
(
'
rich
'
);
});
it
(
'
returns params urlIsRichBlob is false
'
,
()
=>
{
spyOn
(
RepoService
,
'
urlIsRichBlob
'
).
and
.
returnValue
(
false
);
const
params
=
RepoService
.
buildParams
(
url
);
expect
(
params
.
viewer
).
toBeUndefined
();
});
it
(
'
calls urlIsRichBlob with the objects url prop if no url arg is provided
'
,
()
=>
{
spyOn
(
RepoService
,
'
urlIsRichBlob
'
);
RepoService
.
url
=
url
;
RepoService
.
buildParams
();
expect
(
RepoService
.
urlIsRichBlob
).
toHaveBeenCalledWith
(
url
);
});
});
describe
(
'
urlIsRichBlob
'
,
()
=>
{
it
(
'
returns true for md extension
'
,
()
=>
{
const
isRichBlob
=
RepoService
.
urlIsRichBlob
(
'
url.md
'
);
expect
(
isRichBlob
).
toBeTruthy
();
});
it
(
'
returns false for js extension
'
,
()
=>
{
const
isRichBlob
=
RepoService
.
urlIsRichBlob
(
'
url.js
'
);
expect
(
isRichBlob
).
toBeFalsy
();
});
});
describe
(
'
getContent
'
,
()
=>
{
const
params
=
{};
const
url
=
'
url
'
;
const
requestPromise
=
Promise
.
resolve
();
beforeEach
(()
=>
{
spyOn
(
RepoService
,
'
buildParams
'
).
and
.
returnValue
(
params
);
spyOn
(
axios
,
'
get
'
).
and
.
returnValue
(
requestPromise
);
});
it
(
'
calls buildParams and axios.get
'
,
()
=>
{
const
request
=
RepoService
.
getContent
(
url
);
expect
(
RepoService
.
buildParams
).
toHaveBeenCalledWith
(
url
);
expect
(
axios
.
get
).
toHaveBeenCalledWith
(
url
,
{
params
,
});
expect
(
request
).
toBe
(
requestPromise
);
});
it
(
'
uses object url prop if no url arg is provided
'
,
()
=>
{
RepoService
.
url
=
url
;
RepoService
.
getContent
();
expect
(
axios
.
get
).
toHaveBeenCalledWith
(
url
,
{
params
,
});
});
});
describe
(
'
getBase64Content
'
,
()
=>
{
const
url
=
'
url
'
;
const
response
=
{
data
:
'
data
'
};
beforeEach
(()
=>
{
spyOn
(
RepoService
,
'
bufferToBase64
'
);
spyOn
(
axios
,
'
get
'
).
and
.
returnValue
(
Promise
.
resolve
(
response
));
});
it
(
'
calls axios.get and bufferToBase64 on completion
'
,
(
done
)
=>
{
const
request
=
RepoService
.
getBase64Content
(
url
);
expect
(
axios
.
get
).
toHaveBeenCalledWith
(
url
,
{
responseType
:
'
arraybuffer
'
,
});
expect
(
request
).
toEqual
(
jasmine
.
any
(
Promise
));
request
.
then
(()
=>
{
expect
(
RepoService
.
bufferToBase64
).
toHaveBeenCalledWith
(
response
.
data
);
done
();
}).
catch
(
done
.
fail
);
});
});
});
Write
Preview
Markdown
is supported
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