gecko-dev/browser/components/loop/test/shared/models_test.js
Nicolas Perriault 4ac3aa169a Bug 985596 - Refactored shared assets & tests. r=dmose
--HG--
rename : static/shared/README.md => browser/components/loop/content/shared/README.md
rename : static/shared/css/common.css => browser/components/loop/content/shared/css/common.css
rename : static/shared/css/conversation.css => browser/components/loop/content/shared/css/conversation.css
rename : static/shared/css/panel.css => browser/components/loop/content/shared/css/panel.css
rename : static/shared/css/readme.html => browser/components/loop/content/shared/css/readme.html
rename : static/shared/img/icon_32.png => browser/components/loop/content/shared/img/icon_32.png
rename : static/shared/img/icon_64.png => browser/components/loop/content/shared/img/icon_64.png
rename : static/shared/js/models.js => browser/components/loop/content/shared/js/models.js
rename : static/shared/js/views.js => browser/components/loop/content/shared/js/views.js
rename : static/shared/libs/backbone-1.1.2.js => browser/components/loop/content/shared/libs/backbone-1.1.2.js
rename : static/shared/libs/jquery-2.1.0.js => browser/components/loop/content/shared/libs/jquery-2.1.0.js
rename : static/shared/libs/lodash-2.4.1.js => browser/components/loop/content/shared/libs/lodash-2.4.1.js
rename : static/shared/libs/sdk.js => browser/components/loop/content/shared/libs/sdk.js
rename : static/shared/libs/webl10n-20130617.js => browser/components/loop/content/shared/libs/webl10n-20130617.js
rename : static/css/webapp.css => browser/components/loop/standalone/content/css/webapp.css
rename : static/index.html => browser/components/loop/standalone/content/index.html
rename : static/js/webapp.js => browser/components/loop/standalone/content/js/webapp.js
rename : static/l10n/data.ini => browser/components/loop/standalone/content/l10n/data.ini
rename : test/webapp_test.js => browser/components/loop/test/standalone/webapp_test.js
extra : transplant_source : %DA%D9%3A%E9%C6%E0d%13%84%C1%BEps%C8b%F09o%D7m
2014-05-29 21:20:11 +01:00

124 lines
4.1 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* global loop, sinon */
var expect = chai.expect;
describe("loop.shared.models", function() {
"use strict";
var sharedModels = loop.shared.models,
sandbox, fakeXHR, requests = [];
beforeEach(function() {
sandbox = sinon.sandbox.create();
fakeXHR = sandbox.useFakeXMLHttpRequest();
requests = [];
// https://github.com/cjohansen/Sinon.JS/issues/393
fakeXHR.xhr.onCreate = function(xhr) {
requests.push(xhr);
};
});
afterEach(function() {
sandbox.restore();
});
describe("ConversationModel", function() {
var conversation, fakeSessionData, fakeBaseServerUrl;
beforeEach(function() {
conversation = new sharedModels.ConversationModel();
fakeSessionData = {
sessionId: "sessionId",
sessionToken: "sessionToken",
apiKey: "apiKey"
};
fakeBaseServerUrl = "http://fakeBaseServerUrl";
});
describe("#initiate", function() {
it("should throw an Error if no baseServerUrl argument is passed",
function () {
expect(function() {
conversation.initiate();
}).to.Throw(Error, /baseServerUrl/);
});
it("should prevent launching a conversation when token is missing",
function() {
expect(function() {
conversation.initiate(fakeBaseServerUrl);
}).to.Throw(Error, /missing required attribute loopToken/);
});
it("should make one ajax POST to a correctly constructed URL",
function() {
conversation.set("loopToken", "fakeToken");
conversation.initiate(fakeBaseServerUrl);
expect(requests).to.have.length.of(1);
expect(requests[0].method.toLowerCase()).to.equal("post");
expect(requests[0].url).to.match(
new RegExp("^" + fakeBaseServerUrl + "/calls/fakeToken"));
});
it("should update conversation session information from server data",
function() {
conversation.set("loopToken", "fakeToken");
conversation.initiate(fakeBaseServerUrl);
requests[0].respond(200, {"Content-Type": "application/json"},
JSON.stringify(fakeSessionData));
expect(conversation.get("sessionId")).eql("sessionId");
expect(conversation.get("sessionToken")).eql("sessionToken");
expect(conversation.get("apiKey")).eql("apiKey");
});
it("should trigger session:ready without fetching session data over "+
"HTTP when already set", function(done) {
sandbox.stub(jQuery, "ajax");
conversation.set("loopToken", "fakeToken");
conversation.set(fakeSessionData);
conversation.on("session:ready", function() {
sinon.assert.notCalled($.ajax);
done();
}).initiate(fakeBaseServerUrl);
});
it("should trigger a `session:error` on failure", function(done) {
conversation.set("loopToken", "fakeToken");
conversation.initiate(fakeBaseServerUrl);
conversation.on("session:error", function(err) {
expect(err.message).to.match(/failed: HTTP 400 Bad Request; fake/);
done();
});
requests[0].respond(400, {"Content-Type": "application/json"},
JSON.stringify({error: "fake"}));
});
});
describe("#setReady", function() {
it("should update conversation session information", function() {
conversation.setReady(fakeSessionData);
expect(conversation.get("sessionId")).eql("sessionId");
expect(conversation.get("sessionToken")).eql("sessionToken");
expect(conversation.get("apiKey")).eql("apiKey");
});
it("should trigger a `session:ready` event", function(done) {
conversation.on("session:ready", function() {
done();
}).setReady(fakeSessionData);
});
});
});
});