Make unit tests return a promise instead of manually calling done
[WebKit-https.git] / Websites / perf.webkit.org / unit-tests / privileged-api-tests.js
index 83985b7..5095e59 100644 (file)
@@ -2,24 +2,24 @@
 
 const assert = require('assert');
 
-let MockRemoteAPI = require('./resources/mock-remote-api.js').MockRemoteAPI;
+const MockRemoteAPI = require('./resources/mock-remote-api.js').MockRemoteAPI;
 require('../tools/js/v3-models.js');
 
-describe('PrivilegedAPI', function () {
+describe('PrivilegedAPI', () => {
     let requests = MockRemoteAPI.inject();
 
-    beforeEach(function () {
+    beforeEach(() => {
         PrivilegedAPI._token = null;
-    })
+    });
 
-    describe('requestCSRFToken', function () {
-        it('should generate a new token', function () {
+    describe('requestCSRFToken', () => {
+        it('should generate a new token', () => {
             PrivilegedAPI.requestCSRFToken();
             assert.equal(requests.length, 1);
             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
         });
 
-        it('should not generate a new token if the existing token had not expired', function (done) {
+        it('should not generate a new token if the existing token had not expired', () => {
             const tokenRequest = PrivilegedAPI.requestCSRFToken();
             assert.equal(requests.length, 1);
             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
@@ -27,15 +27,14 @@ describe('PrivilegedAPI', function () {
                 token: 'abc',
                 expiration: Date.now() + 3600 * 1000,
             });
-            tokenRequest.then(function (token) {
+            return tokenRequest.then((token) => {
                 assert.equal(token, 'abc');
                 PrivilegedAPI.requestCSRFToken();
                 assert.equal(requests.length, 1);
-                done();
-            }).catch(done);
+            });
         });
 
-        it('should generate a new token if the existing token had already expired', function (done) {
+        it('should generate a new token if the existing token had already expired', () => {
             const tokenRequest = PrivilegedAPI.requestCSRFToken();
             assert.equal(requests.length, 1);
             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
@@ -43,36 +42,32 @@ describe('PrivilegedAPI', function () {
                 token: 'abc',
                 expiration: Date.now() - 1,
             });
-            tokenRequest.then(function (token) {
+            return tokenRequest.then((token) => {
                 assert.equal(token, 'abc');
                 PrivilegedAPI.requestCSRFToken();
                 assert.equal(requests.length, 2);
                 assert.equal(requests[1].url, '/privileged-api/generate-csrf-token');
-                done();
-            }).catch(done);
+            });
         });
     });
     
-    describe('sendRequest', function () {
+    describe('sendRequest', () => {
 
-        it('should generate a new token if no token had been fetched', function (done) {
-            PrivilegedAPI.sendRequest('test', {});
+        it('should generate a new token if no token had been fetched', () => {
+            const promise = PrivilegedAPI.sendRequest('test', {});
             assert.equal(requests.length, 1);
             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
             requests[0].resolve({
                 token: 'abc',
                 expiration: Date.now() + 100 * 1000,
             });
-            Promise.resolve().then(function () {
-                return Promise.resolve();
-            }).then(function () {
+            return MockRemoteAPI.waitForRequest().then(() => {
                 assert.equal(requests.length, 2);
                 assert.equal(requests[1].url, '/privileged-api/test');
-                done();
-            }).catch(done);
+            });
         });
 
-        it('should not generate a new token if the existing token had not been expired', function (done) {
+        it('should not generate a new token if the existing token had not been expired', () => {
             PrivilegedAPI.sendRequest('test', {});
             assert.equal(requests.length, 1);
             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
@@ -80,37 +75,31 @@ describe('PrivilegedAPI', function () {
                 token: 'abc',
                 expiration: Date.now() + 3600 * 1000,
             });
-            Promise.resolve().then(function () {
-                return Promise.resolve();
-            }).then(function () {
+            return MockRemoteAPI.waitForRequest().then(() => {
                 assert.equal(requests.length, 2);
                 assert.equal(requests[1].url, '/privileged-api/test');
                 PrivilegedAPI.sendRequest('test2', {});
-                return Promise.resolve();
-            }).then(function () {
+                return MockRemoteAPI.waitForRequest();
+            }).then(() => {
                 assert.equal(requests.length, 3);
                 assert.equal(requests[2].url, '/privileged-api/test2');
-                done();
-            }).catch(done);
+            });
         });
 
-        it('should reject immediately when a token generation had failed', function (done) {
+        it('should reject immediately when a token generation had failed', () => {
             const request = PrivilegedAPI.sendRequest('test', {});
             let caught = false;
-            request.catch(function () { caught = true; });
+            request.catch(() => { caught = true; });
             assert.equal(requests.length, 1);
             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
             requests[0].reject({status: 'FailedToGenerateToken'});
-            Promise.resolve().then(function () {
-                return Promise.resolve();
-            }).then(function () {
+            return new Promise((resolve) => setTimeout(resolve, 0)).then(() => {
                 assert.equal(requests.length, 1);
                 assert(caught);
-                done();
-            }).catch(done);
+            });
         });
 
-        it('should re-generate token when it had become invalid', function (done) {
+        it('should re-generate token when it had become invalid', () => {
             PrivilegedAPI.sendRequest('test', {});
             assert.equal(requests.length, 1);
             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
@@ -118,63 +107,61 @@ describe('PrivilegedAPI', function () {
                 token: 'abc',
                 expiration: Date.now() + 3600 * 1000,
             });
-            Promise.resolve().then(function () {
-                return Promise.resolve();
-            }).then(function () {
+            return MockRemoteAPI.waitForRequest().then(() => {
                 assert.equal(requests.length, 2);
                 assert.equal(requests[1].data.token, 'abc');
                 assert.equal(requests[1].url, '/privileged-api/test');
                 requests[1].reject('InvalidToken');
-                return Promise.resolve();
-            }).then(function () {
+                return MockRemoteAPI.waitForRequest();
+            }).then(() => {
                 assert.equal(requests.length, 3);
                 assert.equal(requests[2].url, '/privileged-api/generate-csrf-token');
                 requests[2].resolve({
                     token: 'def',
                     expiration: Date.now() + 3600 * 1000,
                 });
-                return Promise.resolve();
-            }).then(function () {
+                return MockRemoteAPI.waitForRequest();
+            }).then(() => {
                 assert.equal(requests.length, 4);
                 assert.equal(requests[3].data.token, 'def');
                 assert.equal(requests[3].url, '/privileged-api/test');
-                done();
-            }).catch(done);
+            });
         });
 
-        it('should not re-generate token when the re-fetched token was invalid', function (done) {
-            PrivilegedAPI.sendRequest('test', {});
+        it('should not re-generate token when the re-fetched token was invalid', () => {
+            const request = PrivilegedAPI.sendRequest('test', {});
+            let caught = false;
+            request.catch(() => caught = true);
             assert.equal(requests.length, 1);
             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
             requests[0].resolve({
                 token: 'abc',
                 expiration: Date.now() + 3600 * 1000,
             });
-            Promise.resolve().then(function () {
-                return Promise.resolve();
-            }).then(function () {
+            return MockRemoteAPI.waitForRequest().then(() => {
                 assert.equal(requests.length, 2);
                 assert.equal(requests[1].data.token, 'abc');
                 assert.equal(requests[1].url, '/privileged-api/test');
                 requests[1].reject('InvalidToken');
-                return Promise.resolve();
-            }).then(function () {
+                return MockRemoteAPI.waitForRequest();
+            }).then(() => {
                 assert.equal(requests.length, 3);
                 assert.equal(requests[2].url, '/privileged-api/generate-csrf-token');
                 requests[2].resolve({
                     token: 'def',
                     expiration: Date.now() + 3600 * 1000,
                 });
-                return Promise.resolve();
-            }).then(function () {
+                return MockRemoteAPI.waitForRequest();
+            }).then(() => {
                 assert.equal(requests.length, 4);
                 assert.equal(requests[3].data.token, 'def');
                 assert.equal(requests[3].url, '/privileged-api/test');
                 requests[3].reject('InvalidToken');
-            }).then(function () {
+                return new Promise((resolve) => setTimeout(resolve, 0));
+            }).then(() => {
+                assert(caught);
                 assert.equal(requests.length, 4);
-                done();
-            }).catch(done);
+            });
         });
 
     });