|
| 1 | +"use strict"; |
| 2 | +require("./support/setup.js"); |
| 3 | +const shouldPass = require("./support/common.js").shouldPass; |
| 4 | +const shouldFail = require("./support/common.js").shouldFail; |
| 5 | +const assert = require("chai").assert; |
| 6 | +const expect = require("chai").expect; |
| 7 | + |
| 8 | +describe("Assert interface with eventually extender:", () => { |
| 9 | + let promise = null; |
| 10 | + |
| 11 | + describe("Direct tests of fulfilled promises", () => { |
| 12 | + it(".eventually.isNull(promise)", done => { |
| 13 | + assert.eventually.isNull(Promise.resolve(null)).notify(done); |
| 14 | + }); |
| 15 | + it(".eventually.isFunction(promise)", done => { |
| 16 | + assert.eventually.isFunction(Promise.resolve(() => { /* Forever pending */ })).notify(done); |
| 17 | + }); |
| 18 | + it(".eventually.typeOf(promise, 'string')", done => { |
| 19 | + assert.eventually.typeOf(Promise.resolve("hello"), "string").notify(done); |
| 20 | + }); |
| 21 | + it(".eventually.include(promiseForString, 'substring')", done => { |
| 22 | + assert.eventually.include(Promise.resolve("hello"), "hell").notify(done); |
| 23 | + }); |
| 24 | + it(".eventually.include(promiseForArray, arrayMember)", done => { |
| 25 | + assert.eventually.include(Promise.resolve([1, 2, 3]), 1).notify(done); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe("On a promise fulfilled with the number 42", () => { |
| 30 | + beforeEach(() => { |
| 31 | + promise = Promise.resolve(42); |
| 32 | + }); |
| 33 | + |
| 34 | + describe(".eventually.isNull(promise)", () => { |
| 35 | + shouldFail({ |
| 36 | + op: () => assert.eventually.isNull(promise), |
| 37 | + message: "to equal null" |
| 38 | + }); |
| 39 | + }); |
| 40 | + describe(".eventually.isDefined(promise)", () => { |
| 41 | + shouldPass(() => assert.eventually.isDefined(promise)); |
| 42 | + }); |
| 43 | + describe(".eventually.ok(promise)", () => { |
| 44 | + shouldPass(() => assert.eventually.ok(promise)); |
| 45 | + }); |
| 46 | + describe(".eventually.equal(promise, 42)", () => { |
| 47 | + shouldPass(() => assert.eventually.equal(promise, 42)); |
| 48 | + }); |
| 49 | + describe(".eventually.equal(promise, 52)", () => { |
| 50 | + shouldFail({ |
| 51 | + op: () => assert.eventually.equal(promise, 52), |
| 52 | + message: "to equal 52" |
| 53 | + }); |
| 54 | + |
| 55 | + function shouldFailWithCorrectActual(promiseProducer) { |
| 56 | + it("should return a promise rejected with an assertion error that has actual/expected properties " + |
| 57 | + "correct", done => { |
| 58 | + expect(promiseProducer().then( |
| 59 | + () => { |
| 60 | + throw new Error("promise fulfilled"); |
| 61 | + }, |
| 62 | + e => { |
| 63 | + e.actual.should.equal(42); |
| 64 | + e.expected.should.equal(52); |
| 65 | + } |
| 66 | + )).to.be.fulfilled.notify(done); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + describe("assert", () => { |
| 71 | + shouldFailWithCorrectActual(() => assert.eventually.equal(promise, 52)); |
| 72 | + }); |
| 73 | + describe("expect", () => { |
| 74 | + shouldFailWithCorrectActual(() => expect(promise).to.eventually.equal(52)); |
| 75 | + }); |
| 76 | + describe("should", () => { |
| 77 | + shouldFailWithCorrectActual(() => promise.should.eventually.equal(52)); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe(".eventually.notEqual(promise, 42)", () => { |
| 82 | + shouldFail({ |
| 83 | + op: () => assert.eventually.notEqual(promise, 42), |
| 84 | + message: "to not equal 42" |
| 85 | + }); |
| 86 | + }); |
| 87 | + describe(".eventually.notEqual(promise, 52)", () => { |
| 88 | + shouldPass(() => assert.eventually.notEqual(promise, 52)); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe("On a promise fulfilled with { foo: 'bar' }", () => { |
| 93 | + beforeEach(() => { |
| 94 | + promise = Promise.resolve({ foo: "bar" }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe(".eventually.equal(promise, { foo: 'bar' })", () => { |
| 98 | + shouldFail({ |
| 99 | + op: () => assert.eventually.equal(promise, { foo: "bar" }), |
| 100 | + message: "to equal { foo: 'bar' }" |
| 101 | + }); |
| 102 | + }); |
| 103 | + describe(".eventually.deepEqual(promise, { foo: 'bar' })", () => { |
| 104 | + shouldPass(() => assert.eventually.deepEqual(promise, { foo: "bar" })); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("Assertion messages", () => { |
| 109 | + const message = "He told me enough! He told me you killed him!"; |
| 110 | + |
| 111 | + describe("should pass through for .eventually.isNull(promise, message) for fulfilled", () => { |
| 112 | + shouldFail({ |
| 113 | + op: () => assert.eventually.isNull(Promise.resolve(42), message), |
| 114 | + message |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe("should pass through for .eventually.isNull(promise, message) for rejected", () => { |
| 119 | + shouldFail({ |
| 120 | + op: () => assert.eventually.isNull(Promise.reject(), message), |
| 121 | + message |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe("should pass through for .eventually.equal(promise, 52, message) for fulfilled", () => { |
| 126 | + shouldFail({ |
| 127 | + op: () => assert.eventually.equal(Promise.resolve(42), 52, message), |
| 128 | + message |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe("should pass through for .eventually.equal(promise, 52, message) for rejected", () => { |
| 133 | + shouldFail({ |
| 134 | + op: () => assert.eventually.equal(Promise.reject(), 52, message), |
| 135 | + message |
| 136 | + }); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments