'use strict';

const Promise = require('promise');
const chai = require('chai');
const expect = chai.expect;
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

describe('ES2015 Promises', () => {
  it('should provide a greeting', () => {
    // When
    const promise = generateGreeting('Sean');

    // Then
    return expect(promise).to.eventually.equal('Hello Sean!');
  });

  it('should provide multiple greetings', () => {
    // Given
    const promise1 = generateGreeting('Sean');
    const promise2 = generateGreeting('Beth');

    // When
    const promises = Promise.all([promise1, promise2]);

    // Then®
    const expectedGreetings = ['Hello Sean!', 'Hello Beth!'];
    return expect(promises).to.eventually.deep.equal(expectedGreetings);
  });

  it('should provide results of first promise', () => {
    // Given
    const promise1 = generateGreeting('Sean');
    const promise2 = generateGreeting('Beth');

    // When
    const promises = Promise.race([promise1, promise2]);

    // Then
    return expect(promises).to.eventually.equal('Hello Sean!');
  });

  it('should resolve a promise with a synchronous value', () => {
    // Given
    const message = 'Say Hello!';

    // When
    const promise = Promise.resolve(message);

    // Then
    return expect(promise).to.eventually.equal(message);
  });

  it('should reject a promise with a message', () => {
    // Given
    const errorMessage = 'Help! Error!';

    // When
    const promise = Promise.reject(new Error(errorMessage));

    // Then
    return expect(promise).to.eventually.rejectedWith(errorMessage);
  });

  const generateGreeting = (name) => {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(`Hello ${name}!`);
      }, 1500);
    });
  };
});

@@ -392,32 +392,286 @@
 Sean!');%0A  %7D);%0A%0A
+  it('should handle invalid delay', () =%3E %7B%0A    // Given%0A    const invalidDelay = 1501;%0A%0A    // When%0A    const promise = generateGreeting('Sean', invalidDelay);%0A%0A    // Then%0A    return expect(promise).to.eventually.rejectedWith('Invalid delay!');%0A  %7D);%0A%0A
   it('should pro
@@ -696,32 +696,32 @@
 tings', () =%3E %7B%0A
-
     // Given%0A   
@@ -1097,32 +1097,55 @@
  %7B%0A    // Given%0A
+    const delay = 100;%0A
     const promis
@@ -1164,32 +1164,39 @@
 eGreeting('Sean'
+, delay
 );%0A    const pro
@@ -1218,32 +1218,46 @@
 eGreeting('Beth'
+, delay + 1000
 );%0A%0A    // When%0A
@@ -1920,21 +1920,24 @@
  %7D);%0A%0A  
-const
+function
  generat
@@ -1949,20 +1949,28 @@
 ting
- = 
 (name
-) =%3E
+, delay = 1000)
  %7B%0A 
@@ -1999,23 +1999,111 @@
 (resolve
+, reject
 ) =%3E %7B%0A
+      if (delay %3E 1500) %7B%0A        reject(new Error('Invalid delay!'));%0A      %7D%0A%0A
       se
@@ -2116,26 +2116,16 @@
 ut(() =%3E
- %7B%0A       
  resolve
@@ -2146,23 +2146,15 @@
 %7D!%60)
-;%0A      %7D, 1500
+, delay
 );%0A 
@@ -2164,12 +2164,11 @@
 %7D);%0A
+
   %7D
-;
 %0A%7D);

Update promise to have a default timeout that can be overridden