I'm working on a legacy AngularJS application that runs on SP. In its services, it's heavily using jQuery.SPServices to fetch/save data. This has made the services untestable. Is there an easy way to isolate these SP bound accesses from the rest of the application? I was thinking of mocking the services out and unit test the controllers only, but that leaves out the filter logic and whatnot built into services. Any pointers?
1 Answer
You could use axios to call SharePoint Rest api directly.
var endPointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbyTitle('siteAdmins')/items";
var headers = {
"accept": "application/json;odata=verbose"
};
axios.get(endPointUrl).then(response => {
console.log(response.data.value);
});
-
Thanks, but I read on thier page "axios is heavily inspired by the $http service provided in Angular. Ultimately axios is an effort to provide a standalone $http-like service for use outside of Angular." So I believe I'm better off with what Angular provides if I'm going to use the rest api. Don't you think?Ε Г И І И О– Ε Г И І И О2020-09-30 03:06:08 +00:00Commented Sep 30, 2020 at 3:06
-
axios is lighter than spservice.Of course,you could choose methods provided by Angular.Amos– Amos2020-09-30 03:13:45 +00:00Commented Sep 30, 2020 at 3:13
-
Hmm but that involves changing all my legacy code which uses SPServices to use the rest api. That's a massive change and a pretty risky one IMO given that I don't have any tests to verify the functionality. I wish there is a way to keep SPServices but somehow intercept and replace it with test doubles.Ε Г И І И О– Ε Г И І И О2020-10-01 11:17:59 +00:00Commented Oct 1, 2020 at 11:17