Commit d9558bb5 by Clinton Blackburn

Added additional JavaScript tests

- Increased coverage of course model
- Removed unused karma plugin: jasmine-jquery

XCOM-523
parent 8ec43828
...@@ -5,6 +5,7 @@ define([ ...@@ -5,6 +5,7 @@ define([
'backbone.relational', 'backbone.relational',
'backbone.super', 'backbone.super',
'backbone.validation', 'backbone.validation',
'jquery',
'jquery-cookie', 'jquery-cookie',
'moment', 'moment',
'underscore', 'underscore',
...@@ -18,6 +19,7 @@ define([ ...@@ -18,6 +19,7 @@ define([
BackboneRelational, BackboneRelational,
BackboneSuper, BackboneSuper,
BackboneValidation, BackboneValidation,
$,
$cookie, $cookie,
moment, moment,
_, _,
......
define([
'collections/course_collection'
],
function (CourseCollection) {
'use strict';
var collection,
response = {
count: 1,
next: null,
previous: null,
results: [
{
id: 'edX/DemoX/Demo_Course',
url: 'http://ecommerce.local:8002/api/v2/courses/edX/DemoX/Demo_Course/',
name: 'edX Demonstration Course',
verification_deadline: null,
type: 'credit',
products_url: 'http://ecommerce.local:8002/api/v2/courses/edX/DemoX/Demo_Course/products/',
last_edited: '2015-07-28T18:08:15Z'
}
]
};
beforeEach(function () {
collection = new CourseCollection();
});
describe('parse', function () {
it('should return the results list in the response', function () {
expect(collection.parse(response)).toEqual(response.results);
});
it('should fetch the next page of results', function () {
spyOn(collection, 'fetch').and.returnValue(null);
response.next = '/api/v2/courses/?page=2';
collection.parse(response);
expect(collection.url).toEqual(response.next);
expect(collection.fetch).toHaveBeenCalledWith({remove: false});
});
});
}
);
define([ define([
'jquery',
'moment',
'underscore', 'underscore',
'models/course_model' 'models/course_model',
'models/course_seats/professional_seat',
'jquery-cookie'
], ],
function (_, function ($,
Course) { moment,
_,
Course,
ProfessionalSeat) {
'use strict'; 'use strict';
var model, var model,
honorSeat = {
id: 9,
url: 'http://ecommerce.local:8002/api/v2/products/9/',
structure: 'child',
product_class: 'Seat',
title: 'Seat in edX Demonstration Course with honor certificate',
price: '0.00',
expires: null,
attribute_values: [
{
name: 'certificate_type',
value: 'honor'
},
{
name: 'course_key',
value: 'edX/DemoX/Demo_Course'
},
{
name: 'id_verification_required',
value: false
}
],
is_available_to_buy: true
},
verifiedSeat = {
id: 8,
url: 'http://ecommerce.local:8002/api/v2/products/8/',
structure: 'child',
product_class: 'Seat',
title: 'Seat in edX Demonstration Course with verified certificate (and ID verification)',
price: '15.00',
expires: null,
attribute_values: [
{
name: 'certificate_type',
value: 'verified'
},
{
name: 'course_key',
value: 'edX/DemoX/Demo_Course'
},
{
name: 'id_verification_required',
value: true
}
],
is_available_to_buy: true
},
data = { data = {
id: 'edX/DemoX/Demo_Course', id: 'edX/DemoX/Demo_Course',
url: 'http://ecommerce.local:8002/api/v2/courses/edX/DemoX/Demo_Course/', url: 'http://ecommerce.local:8002/api/v2/courses/edX/DemoX/Demo_Course/',
name: 'edX Demonstration Course', name: 'edX Demonstration Course',
verification_deadline: null, verification_deadline: '2015-10-01T00:00:00Z',
type: 'verified', type: 'verified',
products_url: 'http://ecommerce.local:8002/api/v2/courses/edX/DemoX/Demo_Course/products/', products_url: 'http://ecommerce.local:8002/api/v2/courses/edX/DemoX/Demo_Course/products/',
last_edited: '2015-07-27T00:27:23Z', last_edited: '2015-07-27T00:27:23Z',
products: [ products: [
{ honorSeat,
id: 9, verifiedSeat,
url: 'http://ecommerce.local:8002/api/v2/products/9/',
structure: 'child',
product_class: 'Seat',
title: 'Seat in edX Demonstration Course with honor certificate',
price: '0.00',
expires: null,
attribute_values: [
{
name: 'certificate_type',
value: 'honor'
},
{
name: 'course_key',
value: 'edX/DemoX/Demo_Course'
},
{
name: 'id_verification_required',
value: false
}
],
is_available_to_buy: true
},
{
id: 8,
url: 'http://ecommerce.local:8002/api/v2/products/8/',
structure: 'child',
product_class: 'Seat',
title: 'Seat in edX Demonstration Course with verified certificate (and ID verification)',
price: '15.00',
expires: null,
attribute_values: [
{
name: 'certificate_type',
value: 'verified'
},
{
name: 'course_key',
value: 'edX/DemoX/Demo_Course'
},
{
name: 'id_verification_required',
value: true
}
],
is_available_to_buy: true
},
{ {
id: 7, id: 7,
url: 'http://ecommerce.local:8002/api/v2/products/7/', url: 'http://ecommerce.local:8002/api/v2/products/7/',
...@@ -127,5 +136,78 @@ define([ ...@@ -127,5 +136,78 @@ define([
expect(json.verification_deadline).toEqual(deadline + '+00:00'); expect(json.verification_deadline).toEqual(deadline + '+00:00');
}); });
}); });
describe('save', function () {
var expectedAjaxData = function (data) {
var products,
expected = {
id: data.id,
name: data.name,
verification_deadline: moment.utc(data.verification_deadline).format()
};
products = _.filter(data.products, function (product) {
return product.structure === 'child';
});
expected.products = _.map(products, function (product) {
return product;
});
return expected;
};
it('should POST to the publication endpoint', function () {
var args,
cookie = 'save-test';
spyOn($, 'ajax');
$.cookie('ecommerce_csrftoken', cookie);
model.save();
// $.ajax should have been called
expect($.ajax).toHaveBeenCalled();
// Ensure the data was POSTed to the correct endpoint
args = $.ajax.calls.argsFor(0)[0];
expect(args.type).toEqual('POST');
expect(args.url).toEqual('/api/v2/publication/');
expect(args.contentType).toEqual('application/json');
expect(args.headers).toEqual({'X-CSRFToken': cookie});
expect(JSON.parse(args.data)).toEqual(expectedAjaxData(data));
});
});
describe('getOrCreateSeat', function () {
it('should return existing seats', function () {
var mapping = {
'honor': honorSeat,
'verified': verifiedSeat
};
_.each(mapping, function (expected, seatType) {
expect(model.getOrCreateSeat(seatType).toJSON()).toEqual(expected);
});
});
it('should return null if an audit seat does not already exist', function () {
expect(model.getOrCreateSeat('audit')).toBeUndefined();
});
it('should create a new CourseSeat if one does not exist', function () {
var seat;
// Sanity check to confirm a new seat is created later
expect(model.seats().length).toEqual(2);
// A new seat should be created
seat = model.getOrCreateSeat('professional');
expect(model.seats().length).toEqual(3);
// The new seat's class/type should correspond to the passed in seat type
expect(seat).toEqual(jasmine.any(ProfessionalSeat));
});
});
} }
); );
...@@ -24,8 +24,7 @@ module.exports = function(config) { ...@@ -24,8 +24,7 @@ module.exports = function(config) {
], ],
// list of files to exclude // list of files to exclude
exclude: [ exclude: [],
],
// preprocess matching files before serving them to the browser // preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
...@@ -37,7 +36,6 @@ module.exports = function(config) { ...@@ -37,7 +36,6 @@ module.exports = function(config) {
plugins:[ plugins:[
'karma-jasmine', 'karma-jasmine',
'karma-requirejs', 'karma-requirejs',
'karma-jasmine-jquery',
'karma-firefox-launcher', 'karma-firefox-launcher',
'karma-coverage', 'karma-coverage',
'karma-spec-reporter', 'karma-spec-reporter',
...@@ -57,9 +55,6 @@ module.exports = function(config) { ...@@ -57,9 +55,6 @@ module.exports = function(config) {
// available reporters: https://npmjs.org/browse/keyword/karma-reporter // available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['spec', 'coverage'], reporters: ['spec', 'coverage'],
// frameworks to use
frameworks: ['jasmine-jquery', 'jasmine', 'requirejs'],
// web server port // web server port
port: 9876, port: 9876,
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
"karma-coverage": "^0.4.2", "karma-coverage": "^0.4.2",
"karma-firefox-launcher": "^0.1.6", "karma-firefox-launcher": "^0.1.6",
"karma-jasmine": "^0.3.6", "karma-jasmine": "^0.3.6",
"karma-jasmine-jquery": "^0.1.1",
"karma-requirejs": "^0.2.2", "karma-requirejs": "^0.2.2",
"karma-sinon": "^1.0.4", "karma-sinon": "^1.0.4",
"karma-spec-reporter": "0.0.20", "karma-spec-reporter": "0.0.20",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment