Skip to content

Instantly share code, notes, and snippets.

@beckettkev
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beckettkev/792b74c358dfcf69d652 to your computer and use it in GitHub Desktop.
Save beckettkev/792b74c358dfcf69d652 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Jasmine + Ajax + Async">
<meta charset="utf-8">
<title>Jasmine Spec Runner v2.3.4</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.3.4/jasmine_favicon.png">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.css">
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine-ajax/3.1.1/mock-ajax.min.js"></script>
<script>
/*
Simple Script that updates a vanilla SP List with a new List Item. It will only update one field (title)
and use Jasmine Specs to verify the different scenarios.
First off, let's abstract the script from the namespaces and data
source...
*/
var Example = {
Component: {}
};
Example.Component.MyNameIs = 'Brian Adams';
/*
The script that writes to the SP List
*/
Example.Component.WriteMyVisit = function() {
var _writeState,
_config = {
source: 'Example.Component.WriteMyVisit',
};
// these functions are PRIVATE
// data comes from the AJAX callback
var succeedCallback = function(data) {
if (data === null || data === undefined || data.value === undefined || data.value === null) {
_writeState = "no no no no no.";
return;
}
_writeState = data.value;
};
var errorCallback = function(err) {
_writeState = err;
};
function init() {
if (Example.Component.MyNameIs != null) {
var _rightNow = new Date().toLocaleString();
var _postBody = JSON.stringify({ '__metadata': { 'type': 'SP.Data.TestListItem' }, 'Title': 'Who? ' + Example.Component.MyNameIs + ', When? ' + _rightNow});
//When run outside the confines of SP, this URL never gets called (look at the Jasmine Specs)
$.ajax({
url: "https://mycompany.sharepoint.com/_api/web/lists/GetByTitle(‘When You Visited Last')/items",
type: "POST",
data: _postBody,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"content-length": _postBody.length,
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
})
.done(function(data) {
if (console && console.log)
console.log("18 til I die - gonna be 18 til I die");
succeedCallback(data);
})
.fail(function(data) {
if (console && console.log)
console.log("eRoRrOr: " + data.value + ", Status: " + data.status);
errorCallback(data);
});
} else {
_writeState = "Dead.";
}
}
init();
return _writeState;
};
/*
Enough of the script writing. Now for some data...
The mock data response, which we use in our
fake Async Request.
*/
var mockResponse = {
success: {
status: 200,
responseText: {
value: "Ya it sure feels good to be alive"
}
},
error: {
status: 500,
responseText: {
value: "Error creating the list item"
}
}
};
/*
Now we have everything we need to start testing.
First of all, we will write a Jasmine Suite with some specs
*/
describe('Write My Visit - Pre-flight Checks', function() {
var request, wmv;
beforeEach(function() {
jasmine.Ajax.install();
wmv = Example.Component.WriteMyVisit();
request = jasmine.Ajax.requests.mostRecent();
});
afterEach(function() {
jasmine.Ajax.uninstall();
});
//mock data verifications
it("Should use the correct url", function() {
expect(request.url).toBe("https://mycompany.sharepoint.com/_api/web/lists/GetByTitle(‘When You Visited Last')/items");
});
it("Verify the success response payload", function() {
request.respondWith(mockResponse.success);
expect(request.responseText.value).toBe("Ya it sure feels good to be alive");
});
it("Verify the error response payload", function() {
request.respondWith(mockResponse.error);
expect(request.responseText.value).toBe("Error creating the list item");
});
});
/*
Now the Suite containing the Specs that will test our logic
*/
describe('Write My Visit - Serious Spectacles', function() {
var d, wmv;
beforeEach(function() {
d = $.Deferred();
spyOn($, 'ajax').and.returnValue(d.promise());
});
it("Should fail on creation, if we don't have a name", function() {
Example.Component.MyNameIs = null;
d.resolve(mockResponse.success.responseText);
wmv = Example.Component.WriteMyVisit();
expect(wmv).not.toBe("Ya it sure feels good to be alive");
});
it("Should succeed on creation, if we have a name (like Bryan Adams)", function() {
Example.Component.MyNameIs = null;
d.resolve(mockResponse.success.responseText);
wmv = Example.Component.WriteMyVisit();
expect(wmv).not.toBe("Ya it sure feels good to be alive");
});
});
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment