From 1b754057c464cba5babec6671545cd5179640fda Mon Sep 17 00:00:00 2001 From: Maxime LUCE Date: Thu, 5 Nov 2020 19:26:08 +0100 Subject: [PATCH] test: remove polyfill tests --- tests/testpoly/abstract.ts | 347 ------------------------- tests/testpoly/build.ts | 21 -- tests/testpoly/config.ts | 37 --- tests/testpoly/constructor.ts | 77 ------ tests/testpoly/fakes/fake1.ts | 1 - tests/testpoly/fakes/fake2.ts | 1 - tests/testpoly/helpers/capabilities.ts | 31 --- tests/testpoly/helpers/common.ts | 46 ---- tests/testpoly/helpers/fake-promise.ts | 21 -- tests/testpoly/helpers/reactions.ts | 31 --- tests/testpoly/index.html | 21 -- tests/testpoly/prototype.ts | 108 -------- tests/testpoly/static.ts | 145 ----------- tests/testpoly/tasks.ts | 69 ----- tests/testpoly/tests.d.ts | 5 - 15 files changed, 961 deletions(-) delete mode 100644 tests/testpoly/abstract.ts delete mode 100644 tests/testpoly/build.ts delete mode 100644 tests/testpoly/config.ts delete mode 100644 tests/testpoly/constructor.ts delete mode 100644 tests/testpoly/fakes/fake1.ts delete mode 100644 tests/testpoly/fakes/fake2.ts delete mode 100644 tests/testpoly/helpers/capabilities.ts delete mode 100644 tests/testpoly/helpers/common.ts delete mode 100644 tests/testpoly/helpers/fake-promise.ts delete mode 100644 tests/testpoly/helpers/reactions.ts delete mode 100644 tests/testpoly/index.html delete mode 100644 tests/testpoly/prototype.ts delete mode 100644 tests/testpoly/static.ts delete mode 100644 tests/testpoly/tasks.ts delete mode 100644 tests/testpoly/tests.d.ts diff --git a/tests/testpoly/abstract.ts b/tests/testpoly/abstract.ts deleted file mode 100644 index 85ee320..0000000 --- a/tests/testpoly/abstract.ts +++ /dev/null @@ -1,347 +0,0 @@ -/// - -import * as sinon from "sinon"; -import * as abstract from "polyfill/abstract"; -import * as tasks from "polyfill/tasks"; -import * as commonHelpers from "./helpers/common"; -import * as reactionsHelpers from "./helpers/reactions"; -import Promise = require("polyfill/class"); -import FakePromise = require("./helpers/fake-promise"); - -describe("Promise Abstract Operation", () => { - - describe("createRejectFunction", () => { - - it("should return a function which accepts a single reason argument", () => { - var promise = Object.create(Promise), - rejectFunction = abstract.createRejectFunction(promise); - - rejectFunction.should.be.type("function"); - rejectFunction.length.should.be.equal(1); - }); - - describe("when called", () => { - var triggerPromiseReactionStub: sinon.SinonStub; - beforeEach(() => { triggerPromiseReactionStub = sinon.stub(abstract, "triggerPromiseReaction"); }); - afterEach(() => { triggerPromiseReactionStub.restore(); }); - - it("should set Promise's [[Status]] internal slot to 'has-rejection'", () => { - var promise = new Promise(commonHelpers.noop()), - rejectFunction = abstract.createRejectFunction(promise); - - rejectFunction.call(undefined); - - promise._status.should.equal("has-rejection"); - }); - - it("should set Promise's [[Result]] internal slot to given reason", () => { - var promise = new Promise(commonHelpers.noop()), - rejectFunction = abstract.createRejectFunction(promise), - expectedReason = new Error("some reason"); - - rejectFunction.call(undefined, expectedReason); - - promise._result.should.equal(expectedReason); - }); - - it("should set Promise's [[ResolveReactions]] and [[RejectReactions]] internal slots to undefined", () => { - var promise = new Promise(commonHelpers.noop()), - rejectFunction = abstract.createRejectFunction(promise); - - rejectFunction.call(undefined); - - commonHelpers.isUndefined(promise._resolveReactions).should.be.ok; - commonHelpers.isUndefined(promise._rejectReactions).should.be.ok; - }); - - // Fails since Typescript 1.4 because exported functions are now called internally in functions so stub does not works - //it("should call TriggerPromiseReaction Abstract Operation once with [[RejectReactions]] internal slot and given reason as arguments", () => { - // var promise = new Promise(commonHelpers.noop()), - // rejectFunction = abstract.createRejectFunction(promise), - - // expectedReactions = promise._rejectReactions, - // expectedReason = new Error("some reason"); - - // rejectFunction.call(undefined, expectedReason); - - // sinon.assert.calledOnce(triggerPromiseReactionStub); - // sinon.assert.calledWithExactly(triggerPromiseReactionStub, expectedReactions, expectedReason); - //}); - }); - }); - - describe("createResolveFunction", () => { - - it("should return a function which accepts a single resolution argument", () => { - var promise = Object.create(Promise), - resolveFunction = abstract.createResolveFunction(promise); - - resolveFunction.should.be.type("function"); - resolveFunction.length.should.be.equal(1); - }); - - describe("when called", () => { - var triggerPromiseReactionStub: sinon.SinonStub; - beforeEach(() => { triggerPromiseReactionStub = sinon.stub(abstract, "triggerPromiseReaction"); }); - afterEach(() => { triggerPromiseReactionStub.restore(); }); - - it("should set Promise's [[Status]] internal slot to 'has-resolution'", () => { - var promise = new Promise(commonHelpers.noop()), - resolveFunction = abstract.createResolveFunction(promise); - - resolveFunction.call(undefined); - - promise._status.should.equal("has-resolution"); - }); - - it("should set Promise's [[Result]] internal slot to given resolution", () => { - var promise = new Promise(commonHelpers.noop()), - resolveFunction = abstract.createResolveFunction(promise), - expectedResolution = { value: "Yoopie !" }; - - resolveFunction.call(undefined, expectedResolution); - - promise._result.should.equal(expectedResolution); - }); - - it("should set Promise's [[ResolveReactions]] and [[RejectReactions]] internal slots to undefined", () => { - var promise = new Promise(commonHelpers.noop()), - resolveFunction = abstract.createResolveFunction(promise); - - resolveFunction.call(undefined); - - (typeof promise._resolveReactions === "undefined").should.be.ok; - (typeof promise._rejectReactions === "undefined").should.be.ok; - }); - - // Fails since Typescript 1.4 because exported functions are now called internally in functions so stub does not works - //it("should call TriggerPromiseReaction Abstract Operation once with [[RejectReactions]] internal slot and given reason as arguments", () => { - // var promise = new Promise(commonHelpers.noop()), - // resolveFunction = abstract.createResolveFunction(promise), - - // expectedReactions = promise._rejectReactions, - // expectedResolution = { value: "Yoopie !" }; - - // resolveFunction.call(undefined, expectedResolution); - - // sinon.assert.calledOnce(triggerPromiseReactionStub); - // sinon.assert.calledWithExactly(triggerPromiseReactionStub, expectedReactions, expectedResolution); - //}); - }); - }); - - describe("createResolutionHandlerFunction", () => { - - it("should return a function which accepts a single resolution argument", () => { - var promise = Object.create(Promise), - resolutionHandler = abstract.createResolutionHandlerFunction(promise, commonHelpers.noop(), commonHelpers.noop()); - - resolutionHandler.should.be.type("function"); - resolutionHandler.length.should.be.equal(1); - }); - - describe("when called", () => { - var triggerPromiseReactionStub: sinon.SinonStub; - beforeEach(() => { triggerPromiseReactionStub = sinon.stub(abstract, "triggerPromiseReaction"); }); - afterEach(() => { triggerPromiseReactionStub.restore(); }); - - it("should call reject callback with TypeError if resolution argument is input promise", () => { - var promise = new Promise(commonHelpers.noop()), - rejectSpy = sinon.spy(), - resolutionHandler = abstract.createResolutionHandlerFunction(promise, commonHelpers.noop(), rejectSpy); - - resolutionHandler.call(undefined, promise); - - sinon.assert.calledOnce(rejectSpy); - sinon.assert.calledWith(rejectSpy, sinon.match.instanceOf(TypeError)); - }); - - it("should call fulfill callback with given resolution if not a thenable", () => { - var promise = new Promise(commonHelpers.noop()), - resolveSpy = sinon.spy(), - expectedResolution = { value: "Yoopie!" }, - resolutionHandler = abstract.createResolutionHandlerFunction(promise, resolveSpy, commonHelpers.noop()); - - resolutionHandler.call(undefined, expectedResolution); - - sinon.assert.calledOnce(resolveSpy); - sinon.assert.calledWithExactly(resolveSpy, expectedResolution); - }); - - it("should register callbacks to a new capability if given resolution is a thenable", () => { - var promise = new Promise(commonHelpers.noop()), - resolveSpy = sinon.spy(), - thenable = { then: sinon.spy() }, - resolutionHandler = abstract.createResolutionHandlerFunction(promise, resolveSpy, commonHelpers.noop()); - - resolutionHandler.call(undefined, thenable); - - sinon.assert.calledOnce(thenable.then); - }); - - }); - }); - - describe("newPromiseCapability", () => { - - it("should throw a TypeError if called using a non-Promise like constructor argument", () => { - abstract.newPromiseCapability.bind(undefined).should.throw(TypeError); - abstract.newPromiseCapability.bind(undefined, {}).should.throw(TypeError); - abstract.newPromiseCapability.bind(undefined, commonHelpers.noop()).should.throw(TypeError); - - abstract.newPromiseCapability.bind(undefined, Promise).should.not.throw(); - abstract.newPromiseCapability.bind(undefined, FakePromise).should.not.throw(); - }); - - it("should return a proper PromiseCapability object", () => { - var capability = abstract.newPromiseCapability(Promise); - - capability.promise.should.have.property("then"); - - capability.reject.should.be.type("function"); - capability.reject.length.should.equal(1); - - capability.resolve.should.be.type("function"); - capability.resolve.length.should.equal(1); - }); - }); - - describe("triggerPromiseReactions", () => { - - describe("should call task.enqueue for each given reactions", () => { - var enqueueTaskStub: sinon.SinonStub; - beforeEach(() => { enqueueTaskStub = sinon.stub(tasks, "enqueue"); }); - afterEach(() => { enqueueTaskStub.restore(); }); - - it("", () => { - var reactions = reactionsHelpers.createFakeReactions(3), - expectedResolution = { value: "Yoopie !" }; - - abstract.triggerPromiseReaction(reactions, expectedResolution); - - sinon.assert.calledThrice(enqueueTaskStub); - }); - - it("using PromiseReactionTask executor as first argument", () => { - var reactions = reactionsHelpers.createFakeReactions(3), - expectedResolution = { value: "Yoopie !" }; - - abstract.triggerPromiseReaction(reactions, expectedResolution); - - sinon.assert.alwaysCalledWith(enqueueTaskStub, abstract.PromiseReactionTask); - }); - - it("using each reactions and expectedResolution as second argument", () => { - var reactions = reactionsHelpers.createFakeReactions(3), - expectedResolution = { value: "Yoopie !" }; - - abstract.triggerPromiseReaction(reactions, expectedResolution); - - sinon.assert.calledWith(enqueueTaskStub, abstract.PromiseReactionTask, [reactions[0], expectedResolution]); - sinon.assert.calledWith(enqueueTaskStub, abstract.PromiseReactionTask, [reactions[1], expectedResolution]); - sinon.assert.calledWith(enqueueTaskStub, abstract.PromiseReactionTask, [reactions[2], expectedResolution]); - }); - }); - }); - - describe("updatePromiseFromPotentialThenable", () => { - - it("should return false if given object does not contain a then function", () => { - var capability = abstract.newPromiseCapability(Promise); - - abstract.updatePromiseFromPotentialThenable(null, capability).should.be.false; - abstract.updatePromiseFromPotentialThenable(undefined, capability).should.be.false; - abstract.updatePromiseFromPotentialThenable({}, capability).should.be.false; - abstract.updatePromiseFromPotentialThenable({ then: {} }, capability).should.be.false; - abstract.updatePromiseFromPotentialThenable({ then: "flux" }, capability).should.be.false; - }); - - it("should return true if contains a then function", () => { - var capability = abstract.newPromiseCapability(Promise), - thenable = { - then: commonHelpers.noop() - }; - - abstract.updatePromiseFromPotentialThenable(thenable, capability).should.be.true; - }); - - it("should return true if contains a then function even if it throws", () => { - var capability = abstract.newPromiseCapability(Promise), - thenable = { - then: () => { throw new Error("this is an error"); } - }; - - abstract.updatePromiseFromPotentialThenable(thenable, capability).should.be.true; - }); - - it("should reject capability if potential then function throws an error", () => { - var capability = abstract.newPromiseCapability(Promise), - capabilityRejectStub = sinon.stub(capability, "reject"), - expectedError = new Error("this is an error"), - thenable = { - then: () => { throw expectedError; } - }; - - abstract.updatePromiseFromPotentialThenable(thenable, capability).should.be.true; - sinon.assert.calledOnce(capabilityRejectStub); - sinon.assert.calledWithExactly(capabilityRejectStub, expectedError); - }); - - }); - - describe("PromiseReactionTask", () => { - - it("should throws a TypeError if called using a misformated PromiseReaction", () => { - abstract.PromiseReactionTask.bind(undefined).should.throw(TypeError); - abstract.PromiseReactionTask.bind(undefined, null).should.throw(TypeError); - abstract.PromiseReactionTask.bind(undefined, {}).should.throw(TypeError); - }); - - it("should reject capability's Promise if reaction handler returns capability's Promise", () => { - var reaction = reactionsHelpers.createFakeReaction(), - capabilityRejectStub = sinon.stub(reaction.capability, "reject"); - - reaction.handler = () => { return reaction.capability.promise; }; - abstract.PromiseReactionTask(reaction, null); - - sinon.assert.calledOnce(capabilityRejectStub); - sinon.assert.calledWith(capabilityRejectStub, sinon.match.instanceOf(TypeError)); - }); - - it("should resolve capability's Promise if reaction handler returns a non-thenable result", () => { - var reaction = reactionsHelpers.createFakeReaction(), - expectedResolution = { value: "Yoopie !" }, - capabilityResolveStub = sinon.stub(reaction.capability, "resolve"); - - reaction.handler = () => expectedResolution; - abstract.PromiseReactionTask(reaction, null); - - sinon.assert.calledOnce(capabilityResolveStub); - sinon.assert.calledWith(capabilityResolveStub, expectedResolution); - }); - - it("should not resolve capability's Promise if reaction handler returns a thenable result", () => { - var reaction = reactionsHelpers.createFakeReaction(), - thenable = { then: sinon.spy() }, - capabilityResolveStub = sinon.stub(reaction.capability, "resolve"); - - reaction.handler = () => thenable; - abstract.PromiseReactionTask(reaction, null); - - sinon.assert.notCalled(capabilityResolveStub); - }); - - it("should call thenable's then function if reaction handler returns a thenable result", () => { - var reaction = reactionsHelpers.createFakeReaction(), - thenable = { then: sinon.spy() }, - capabilityResolveStub = sinon.stub(reaction.capability, "resolve"); - - reaction.handler = () => thenable; - abstract.PromiseReactionTask(reaction, null); - - sinon.assert.notCalled(capabilityResolveStub); - sinon.assert.calledOnce(thenable.then); - sinon.assert.calledWithExactly(thenable.then, reaction.capability.resolve, reaction.capability.reject); - }); - }); -}); diff --git a/tests/testpoly/build.ts b/tests/testpoly/build.ts deleted file mode 100644 index 19bbeb5..0000000 --- a/tests/testpoly/build.ts +++ /dev/null @@ -1,21 +0,0 @@ -/// - -import ImportedPromise = require("promise"); -import PolyfillPromise = require("polyfill/class"); -import common = require("./helpers/common"); - -describe("build polyfill", () => { - - it("should be accessible from AMD", () => { - ImportedPromise.should.not.be.empty; - common.isPromise(ImportedPromise); - //ImportedPromise.should.be.exactly(PolyfillPromise); - }); - - it("should be accessible from Globals", () => { - Promise.should.not.be.empty; - common.isPromise(Promise); - //Promise.should.be.exactly(PolyfillPromise); - }); - -}); diff --git a/tests/testpoly/config.ts b/tests/testpoly/config.ts deleted file mode 100644 index e3d7a1d..0000000 --- a/tests/testpoly/config.ts +++ /dev/null @@ -1,37 +0,0 @@ -/// - -requirejs.config({ - //baseUrl: "../", - - paths: { - "polyfill": "../../polyfill", - "promise": "../../dist/polyfill", - "mocha": "../../bower_components/mocha/mocha", - "should": "../../bower_components/should/should", - "sinon": "../../bower_components/sinon/index" - }, - - shim: { - mocha: { - exports: "mocha" - } - } -}); - -(window).root = window; - -(window).console = window.console || function () { return; }; -(window).notrack = true; - -var tests = [ - "abstract", - "build", - "tasks", - "constructor", - "prototype", - "static" -]; - -require(tests, function () { - mocha.run(); -}); diff --git a/tests/testpoly/constructor.ts b/tests/testpoly/constructor.ts deleted file mode 100644 index 99a6c5d..0000000 --- a/tests/testpoly/constructor.ts +++ /dev/null @@ -1,77 +0,0 @@ -/// - -import * as sinon from "sinon"; -import * as abstract from "polyfill/abstract"; -import * as commonHelpers from "./helpers/common"; -import Promise = require("polyfill/class"); - -describe("Promise Constructor", () => { - var createResolveFunctionStub: sinon.SinonStub, createRejectFunctionStub: sinon.SinonStub; - - beforeEach(() => { - createResolveFunctionStub = sinon.stub(abstract, "createResolveFunction"); - createRejectFunctionStub = sinon.stub(abstract, "createRejectFunction"); - }); - - afterEach(() => { - createResolveFunctionStub.restore(); - createRejectFunctionStub.restore(); - }); - - it("should throws a TypeError if given argument is not a Function", () => { - var P: any = Promise; - (() => { new P({}); }).should.throw(TypeError); - (() => { new P(null); }).should.throw(TypeError); - (() => { new P("test"); }).should.throw(TypeError); - }); - - it("should throws a TypeError if not called using new keyword", () => { - var P: any = Promise; - (() => { P(null); }).should.throw(TypeError); - }); - - it("should set [[Status]] internal slot to 'unresolved'", () => { - var promise = new Promise(commonHelpers.noop()); - - promise._status.should.equal("unresolved"); - }); - - it("should set [[ResolveReactions]] and [[RejectReactions]] internal slots to empty lists", () => { - var promise = new Promise(commonHelpers.noop()); - - promise._resolveReactions.should.eql([]); - promise._rejectReactions.should.eql([]); - }); - - it("should call CreateResolveFunction and CreateRejectFunction Abstract Operations", () => { - var promise = new Promise(commonHelpers.noop()); - - sinon.assert.calledOnce(createResolveFunctionStub); - sinon.assert.calledOnce(createRejectFunctionStub); - }); - - it("should call given executor with result of CreateResolveFunction and CreateRejectFunction Abstract Operations", () => { - var executorSpy = sinon.spy(), - rejectSpy = sinon.spy(), - resolveSpy = sinon.spy(); - - createResolveFunctionStub.returns(resolveSpy); - createRejectFunctionStub.returns(rejectSpy); - - new Promise(executorSpy); - - sinon.assert.calledOnce(executorSpy); - sinon.assert.calledWithExactly(executorSpy, resolveSpy, rejectSpy); - }); - - it("should call reject function if executor throws", () => { - var rejectSpy = sinon.spy(); - - createRejectFunctionStub.returns(rejectSpy); - - new Promise(() => { throw new Error("an error"); }); - - sinon.assert.calledOnce(rejectSpy); - }); - -}); diff --git a/tests/testpoly/fakes/fake1.ts b/tests/testpoly/fakes/fake1.ts deleted file mode 100644 index 3a730ec..0000000 --- a/tests/testpoly/fakes/fake1.ts +++ /dev/null @@ -1 +0,0 @@ -export var type = "fake1"; diff --git a/tests/testpoly/fakes/fake2.ts b/tests/testpoly/fakes/fake2.ts deleted file mode 100644 index 98b65be..0000000 --- a/tests/testpoly/fakes/fake2.ts +++ /dev/null @@ -1 +0,0 @@ -export var type = "fake2"; diff --git a/tests/testpoly/helpers/capabilities.ts b/tests/testpoly/helpers/capabilities.ts deleted file mode 100644 index cf4e3bf..0000000 --- a/tests/testpoly/helpers/capabilities.ts +++ /dev/null @@ -1,31 +0,0 @@ -/// - -import * as abstract from "polyfill/abstract"; -import Promise = require("polyfill/class"); - -export function createFakeCapability(): PromiseCapability { - return abstract.newPromiseCapability(Promise); -} - -export function createFakeCapabilities(count: number): PromiseCapability[] { - var result = [], - i = 0; - - for (; i < count; i++) { - result[i] = createFakeCapability(); - } - - return result; -} - -export function extractPromisesFromCapabilities(capabilities: PromiseCapability[]): Promise[] { - var result = [], - len = capabilities.length, - i = 0; - - for (; i < len; i++) { - result[i] = capabilities[i].promise; - } - - return result; -} diff --git a/tests/testpoly/helpers/common.ts b/tests/testpoly/helpers/common.ts deleted file mode 100644 index 8f78bf9..0000000 --- a/tests/testpoly/helpers/common.ts +++ /dev/null @@ -1,46 +0,0 @@ -if (!Function.prototype.bind) { - Function.prototype.bind = function (oThis) { - if (typeof this !== "function") { - // closest thing possible to the ECMAScript 5 internal IsCallable function - throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); - } - - var args = Array.prototype.slice.call(arguments, 1), - toBind = this, - Noop = function () { return; }, - bound = function () { - return toBind.apply( - this instanceof Noop && oThis ? this : oThis, - args.concat(Array.prototype.slice.call(arguments)) - ); - }; - - Noop.prototype = this.prototype; - bound.prototype = new Noop(); - - return bound; - }; -} - -export function isUndefined(x: any): boolean { - return typeof x === "undefined"; -} - -export function noop(): () => void { - return function () { return; }; -} - -export function isWellImplemented(P) { - var resolve; - new P(function (r) { resolve = r; }); - return (typeof resolve === "function"); -} - -export function isPromise(P) { - return P && - // Some of these methods are missing from Firefox/Chrome experimental implementations - "resolve" in P && "reject" in P && - "all" in P && "race" in P && - // Older version of the spec had a resolver object as the arg rather than a function - isWellImplemented(P); -} diff --git a/tests/testpoly/helpers/fake-promise.ts b/tests/testpoly/helpers/fake-promise.ts deleted file mode 100644 index 4f68f7a..0000000 --- a/tests/testpoly/helpers/fake-promise.ts +++ /dev/null @@ -1,21 +0,0 @@ -class FakePromise { - private _status = undefined; - private _result = undefined; - - private _onFulfilled; - private _onRejected; - - constructor(executor: (resolve, reject) => any) { - executor( - (value) => { this._onFulfilled && this._onFulfilled.call(undefined, value); }, - (reason) => { this._onRejected && this._onRejected.call(undefined, reason); } - ); - } - - public then(onFulfilled: (any) => any, onRejected?: (any) => any): any { - this._onFulfilled = onFulfilled; - this._onRejected = onRejected; - } -} - -export = FakePromise; diff --git a/tests/testpoly/helpers/reactions.ts b/tests/testpoly/helpers/reactions.ts deleted file mode 100644 index 09e8184..0000000 --- a/tests/testpoly/helpers/reactions.ts +++ /dev/null @@ -1,31 +0,0 @@ -/// - -import * as sinon from "sinon"; -import * as abstract from "polyfill/abstract"; -import * as commonHelpers from "./common"; -import Promise = require("polyfill/class"); - -export function createFakeReaction(): PromiseReaction { - return { - capability: abstract.newPromiseCapability(Promise), - handler: sinon.spy() - }; -} - -export function createFakeReactionNoSpy(): PromiseReaction { - return { - capability: abstract.newPromiseCapability(Promise), - handler: commonHelpers.noop() - }; -} - -export function createFakeReactions(count: number): PromiseReaction[] { - var result = [], - i = 0; - - for (; i < count; i++) { - result[i] = createFakeReaction(); - } - - return result; -} diff --git a/tests/testpoly/index.html b/tests/testpoly/index.html deleted file mode 100644 index 4cdef20..0000000 --- a/tests/testpoly/index.html +++ /dev/null @@ -1,21 +0,0 @@ - - - - - SPA Tools - All tests - - - -
- - - - - - - diff --git a/tests/testpoly/prototype.ts b/tests/testpoly/prototype.ts deleted file mode 100644 index 850c572..0000000 --- a/tests/testpoly/prototype.ts +++ /dev/null @@ -1,108 +0,0 @@ -/// - -import * as sinon from "sinon"; -import * as abstract from "polyfill/abstract"; -import * as tasks from "polyfill/tasks"; -import * as reactionsHelpers from "./helpers/reactions"; -import * as commonHelpers from "./helpers/common"; -import Promise = require("polyfill/class"); - -describe("Promise Prototype", () => { - describe("then", () => { - - it("should call CreateResolutionHandlerFunction Abstract Operation with promise and both callbacks", () => { - var promise = new Promise(commonHelpers.noop()), - resolveHandler = commonHelpers.noop(), rejectHandler = commonHelpers.noop(), - createResolutionHandlerStub = sinon.stub(abstract, "createResolutionHandlerFunction"); - - promise.then(resolveHandler, rejectHandler); - - sinon.assert.calledOnce(createResolutionHandlerStub); - sinon.assert.calledWithExactly(createResolutionHandlerStub, promise, resolveHandler, rejectHandler); - - createResolutionHandlerStub.restore(); - }); - - describe("when [[Status]] internal slot is 'unresolved'", () => { - - it("should create a PromiseReaction for both callbacks with a common capability", () => { - var promise = new Promise(commonHelpers.noop()); - - promise.then(undefined, undefined); - - promise._resolveReactions.length.should.equal(1); - promise._rejectReactions.length.should.equal(1); - - promise._resolveReactions[0].capability.should.equal(promise._rejectReactions[0].capability); - }); - - it("should create a PromiseReaction with given reject callback as handler", () => { - var promise = new Promise(commonHelpers.noop()), - rejectHanler = commonHelpers.noop(); - - promise.then(undefined, rejectHanler); - - promise._resolveReactions.length.should.equal(1); - promise._rejectReactions.length.should.equal(1); - - promise._rejectReactions[0].handler.should.equal(rejectHanler); - }); - }); - - describe("when [[Status]] internal slot is 'has-resolution'", () => { - - it("should call enqueueTask with PromiseReactionTask", () => { - var expectedResolution = { value: "Yoopie!" }, - - promise = new Promise(resolve => { resolve(expectedResolution); }), - enqueueTaskStub = sinon.stub(tasks, "enqueue"); - - promise._status.should.equal("has-resolution"); - - promise.then(undefined, undefined); - - sinon.assert.calledOnce(enqueueTaskStub); - sinon.assert.calledWith(enqueueTaskStub, abstract.PromiseReactionTask); - - enqueueTaskStub.restore(); - }); - - }); - - describe("when [[Status]] internal slot is 'has-rejection'", () => { - - it("should call enqueueTask with PromiseReactionTask", () => { - var expectedReason = new Error("an error"), - - promise = new Promise((resolve, reject) => { reject(expectedReason); }), - enqueueTaskStub = sinon.stub(tasks, "enqueue"); - - promise._status.should.equal("has-rejection"); - - promise.then(undefined, undefined); - - sinon.assert.calledOnce(enqueueTaskStub); - sinon.assert.calledWith(enqueueTaskStub, abstract.PromiseReactionTask); - - enqueueTaskStub.restore(); - }); - - }); - - }); - - describe("catch", () => { - - it("should call then with [undefined, onRejected] as arguments", () => { - var promise = new Promise(commonHelpers.noop()), - rejectHandler = commonHelpers.noop(), - thenStub = sinon.stub(promise, "then"); - - promise.catch(rejectHandler); - - sinon.assert.calledOnce(thenStub); - sinon.assert.calledWithExactly(thenStub, undefined, rejectHandler); - }); - - }); -}); diff --git a/tests/testpoly/static.ts b/tests/testpoly/static.ts deleted file mode 100644 index 5c2a953..0000000 --- a/tests/testpoly/static.ts +++ /dev/null @@ -1,145 +0,0 @@ -/// - -import * as sinon from "sinon"; -import * as abstract from "polyfill/abstract"; -import * as tasks from "polyfill/tasks"; -import * as commonHelpers from "./helpers/common"; -import * as reactionsHelpers from "./helpers/reactions"; -import * as capabilitiesHelpers from "./helpers/capabilities"; -import FakePromise = require("./helpers/fake-promise"); -import Promise = require("polyfill/class"); - -describe("Promise Static", () => { - - describe("all", () => { - - it("should return a Promise object", () => { - var promise = Promise.all([]); - - abstract.isPromise(promise).should.be.ok; - promise.then.should.be.type("function"); - }); - - it("should return a resolved Promise object if array is empty", () => { - var promise = Promise.all([]); - - promise._status.should.equal("has-resolution"); - }); - - it("should return an unresolved Promise object if array is not empty", () => { - var promise = Promise.all([ - new Promise(commonHelpers.noop()) - ]); - - promise._status.should.equal("unresolved"); - }); - - it("should resolve Promise when every given Promises are fulfilled", (done) => { - var capabilities = capabilitiesHelpers.createFakeCapabilities(3), - promises = capabilitiesHelpers.extractPromisesFromCapabilities(capabilities), - - promise = Promise.all(promises); - - for (var i = 0; i < capabilities.length; i++) { - promise._status.should.equal("unresolved"); - - capabilities[i].resolve(undefined); - } - - setTimeout(() => { - promise._status.should.equal("has-resolution"); - done(); - }, 40); - }); - - it("should resolve Promise whenever any given Promises fails", (done) => { - var capabilities = capabilitiesHelpers.createFakeCapabilities(3), - promises = capabilitiesHelpers.extractPromisesFromCapabilities(capabilities), - - promise = Promise.all(promises); - - promise._status.should.equal("unresolved"); - - capabilities[0].reject(new Error("an error")); - - setTimeout(() => { - promise._status.should.equal("has-rejection"); - done(); - }, 10); - }); - - }); - - describe("race", () => { - - it("should return a Promise object", () => { - var promise = Promise.race([]); - - abstract.isPromise(promise).should.be.ok; - promise.then.should.be.type("function"); - }); - - it("should return an unresolved Promise object", () => { - var promise = Promise.race([ - new Promise(commonHelpers.noop()) - ]); - - promise._status.should.equal("unresolved"); - }); - - it("should resolve Promise whenever any given Promises are fulfilled", (done) => { - var capabilities = capabilitiesHelpers.createFakeCapabilities(3), - promises = capabilitiesHelpers.extractPromisesFromCapabilities(capabilities), - - promise = Promise.race(promises); - - capabilities[0].resolve(undefined); - - setTimeout(() => { - promise._status.should.equal("has-resolution"); - done(); - }, 10); - }); - - it("should resolve Promise whenever any given Promises fails", (done) => { - var capabilities = capabilitiesHelpers.createFakeCapabilities(3), - promises = capabilitiesHelpers.extractPromisesFromCapabilities(capabilities), - - promise = Promise.race(promises); - - promise._status.should.equal("unresolved"); - - capabilities[0].reject(new Error("an error")); - - setTimeout(() => { - promise._status.should.equal("has-rejection"); - done(); - }, 10); - }); - - }); - - describe("reject", () => { - - it("should return a new rejected Promise with given reason", () => { - var expectedReason = new Error("an error"), - promise = Promise.reject(expectedReason); - - promise._status.should.equal("has-rejection"); - promise._result.should.equal(expectedReason); - }); - - }); - - describe("resolve", () => { - - it("should return a new resolved Promise with given resolution", () => { - var expectedResolution = { value: "Yoopie!" }, - promise = Promise.resolve(expectedResolution); - - promise._status.should.equal("has-resolution"); - promise._result.should.equal(expectedResolution); - }); - - }); -}); diff --git a/tests/testpoly/tasks.ts b/tests/testpoly/tasks.ts deleted file mode 100644 index 6ee00b9..0000000 --- a/tests/testpoly/tasks.ts +++ /dev/null @@ -1,69 +0,0 @@ -/// - -import * as sinon from "sinon"; -import * as tasks from "polyfill/tasks"; - -describe("Promise Task Queue", () => { - - describe("enqueue", () => { - beforeEach(() => { tasks.clear(); }); - - it("should enqueue given function but does not call it while thread is busy", () => { - var callback = sinon.spy(); - - tasks.enqueue(callback, []); - - sinon.assert.notCalled(callback); - }); - - it("should call function when thread is released", (done) => { - var callback = sinon.spy(); - - tasks.enqueue(callback, []); - - sinon.assert.notCalled(callback); - - setTimeout(() => { - sinon.assert.calledOnce(callback); - done(); - }, 1); - }); - - it("should call function with given arguments", (done) => { - var callback = sinon.spy(), - arg1 = "argument1", - arg2 = { value: "argument2" }; - - tasks.enqueue(callback, [arg1, arg2]); - - sinon.assert.notCalled(callback); - - setTimeout(() => { - sinon.assert.calledOnce(callback); - sinon.assert.calledWithExactly(callback, arg1, arg2); - - done(); - }, 1); - }); - - it("should call all callbacks when thread is released", (done) => { - var callback = sinon.spy(), - callback2 = sinon.spy(); - - tasks.enqueue(callback, []); - tasks.enqueue(callback2, []); - - sinon.assert.notCalled(callback); - sinon.assert.notCalled(callback2); - - setTimeout(() => { - sinon.assert.calledOnce(callback); - sinon.assert.calledOnce(callback2); - - done(); - }, 1); - }); - - }); - -}); diff --git a/tests/testpoly/tests.d.ts b/tests/testpoly/tests.d.ts deleted file mode 100644 index b5f9b55..0000000 --- a/tests/testpoly/tests.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -/// -/// -/// -/// -///