Skip to content

Commit

Permalink
Merge pull request kriskowal#686 from kahnjw/add_noconflict
Browse files Browse the repository at this point in the history
Add Q.noConflict function
  • Loading branch information
kriskowal committed May 9, 2015
2 parents b26cace + 12db37e commit de111f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,18 @@

// <script>
} else if (typeof self !== "undefined") {
// Get the `window` object, save the previous Q global
// and initialize Q as a global.
var previousQ = self.Q;
self.Q = definition();

// Add a noConflict function so Q can be removed from the
// global namespace.
self.Q.noConflict = function () {
self.Q = previousQ;
return this;
};

} else {
throw new Error("This environment was not anticipated by Q. Please file a bug.");
}
Expand Down Expand Up @@ -2022,6 +2032,10 @@ Promise.prototype.nodeify = function (nodeback) {
}
};

Q.noConflict = function() {
throw new Error("Q.noConflict only works when Q is used as a global");
};

// All code before this point will be filtered from stack traces.
var qEndingLine = captureLine();

Expand Down
30 changes: 30 additions & 0 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2491,6 +2491,36 @@ describe("node support", function () {

});

describe("browser support", function () {
var _Q;

beforeEach(function() {
_Q = Q;
});

afterEach(function() {
Q = _Q;
});

it("sets the global Q object to its original value", function() {
if (typeof window !== 'undefined') {
// If window is not undefined, the tests are running in the browser
// assert that Q.noConflict returns window.Q to it's initial value
// In this context the original value of Q is undefined
Q.noConflict();
expect(Q).toEqual(undefined);
}
});

it("throws an error if Q.noConflict is called in node", function () {
if (typeof window === 'undefined') {
// If window is undefined the tests are being run in node, and
// Q.noConflict should throw an error
expect(Q.noConflict).toThrow();
}
});
});

describe("isPromise", function () {
it("returns true if passed a promise", function () {
expect(Q.isPromise(Q(10))).toBe(true);
Expand Down

0 comments on commit de111f1

Please sign in to comment.