From 151d33db180d44d41c89309002b150895e1e0206 Mon Sep 17 00:00:00 2001
From: Caleb James DeLisle <cjd@cjdns.fr>
Date: Thu, 11 Jan 2018 15:45:12 +0100
Subject: [PATCH] Added testing of /poll/ :D

---
 TestSelenium.js    |  3 ++
 www/common/test.js | 19 ++++++++----
 www/poll/inner.js  | 76 +++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 91 insertions(+), 7 deletions(-)

diff --git a/TestSelenium.js b/TestSelenium.js
index 59bdd427a..cd34b1986 100644
--- a/TestSelenium.js
+++ b/TestSelenium.js
@@ -49,6 +49,9 @@ var nt = nThen(function (waitFor) {
     ['/slide/#/1/edit/uwKqgj8Ezh2dRaFUWSlrRQ/JkJtAb-hNzfESZEHreAeULU1/', {}],
     ['/slide/#/1/view/uwKqgj8Ezh2dRaFUWSlrRQ/Xa8jXl+jWMpwep41mlrhkqbRuVKGxlueH80Pbgeu5Go/', {}],
 
+    ['/poll/#/1/edit/lHhnKHSs0HBsl2UGfSJoLw/ZXSsAq4BORIixuFaLVBFcxoq/', {}],
+    ['/poll/#/1/view/lHhnKHSs0HBsl2UGfSJoLw/TGul8PhswwLh1klHpBto6yEntWtKES2+tetYrrYec4M/', {}]
+
 ].forEach(function (x) {
     if (failed) { return; }
     var url = 'http://localhost:3000' + x[0];
diff --git a/www/common/test.js b/www/common/test.js
index ed48750a5..5265c2311 100644
--- a/www/common/test.js
+++ b/www/common/test.js
@@ -34,6 +34,11 @@ define([], function () {
                 if (locks.length === 1) {
                     runLock(locks.shift());
                 }
+            },
+            assert: function (expr) {
+                if (expr || failed) { return; }
+                failed = true;
+                out.failed("Failed assertion");
             }
         });
     };
@@ -131,10 +136,12 @@ define([], function () {
     };
     var enableManual = function () {
         out.testing = 'manual';
+        console.log('manual testing enabled');
         out.passed = function () {
             window.alert("Test passed");
         };
         out.failed = function (reason) {
+            try { throw new Error(reason); } catch (err) { console.log(err.stack); }
             window.alert("Test failed [" + reason + "]");
         };
         out.registerInner = function () { };
@@ -146,18 +153,18 @@ define([], function () {
     out.registerInner = function () { };
     out.registerOuter = function () { };
 
-    if (window.location.hash.indexOf("test=auto") > -1) {
-        enableAuto();
-    } else if (window.location.hash.indexOf("test=manual") > -1) {
-        enableManual();
-    } else if (document.cookie.indexOf('test=') === 0) {
+    if (document.cookie.indexOf('test=') === 0) {
         try {
             var x = JSON.parse(decodeURIComponent(document.cookie.replace('test=', '')));
             if (x.test === 'auto') {
                 out.options = x.opts;
                 enableAuto('auto');
+                console.log("Enable auto testing " + window.origin);
+            } else if (x.test === 'manual') {
+                out.options = x.opts;
+                enableManual();
+                console.log("Enable manual testing " + window.origin);
             }
-            console.log("Enable auto testing " + window.origin);
         } catch (e) { }
     }
 
diff --git a/www/poll/inner.js b/www/poll/inner.js
index c693a643d..b08b66097 100644
--- a/www/poll/inner.js
+++ b/www/poll/inner.js
@@ -17,6 +17,7 @@ define([
     '/common/common-interface.js',
     '/customize/messages.js',
     'cm/lib/codemirror',
+    '/common/test.js',
 
     'cm/addon/display/placeholder',
     'cm/mode/markdown/markdown',
@@ -45,7 +46,8 @@ define([
     ChainPad,
     UI,
     Messages,
-    CMeditor)
+    CMeditor,
+    Test)
 {
     var saveAs = window.saveAs;
 
@@ -965,6 +967,7 @@ define([
         });
         APP.$createRow = $('#cp-app-poll-create-option').click(function () {
             var uncommittedCopy = { content: getUncommitted('row') };
+            console.log(uncommittedCopy);
             mergeUncommitted(proxy, uncommittedCopy, true);
             change(null, null, null, null, function() {
                 var newId = APP.uncommitted.content.rowsOrder[0];
@@ -1045,6 +1048,77 @@ define([
             publish(true);
         }
 
+        var passIfOk = function (t) {
+            t.assert($('#cp-app-poll-description-published').text().indexOf(
+                "Content for the description") === 0);
+            t.assert($('.cp-app-poll-comments-list-data-name').text().indexOf(
+                "Mr.Me") === 0);
+            t.assert($('.cp-app-poll-comments-list-msg-text').text().indexOf(
+                "Example comment yay") === 0);
+            t.assert($('input[value="Candy"]').length === 1);
+            t.assert($('input[value="IceCream"]').length === 1);
+            t.assert($('input[value="Soda"]').length === 1);
+            t.assert($('input[value="Meeee"]').length === 1);
+            t.pass();
+        };
+
+        if (!APP.readOnly) {
+            console.log("Here is the test");
+            Test(function (t) {
+                if ($('input[value="Candy"]').length) {
+                    t.fail("Test has already been performed");
+                    return;
+                }
+                nThen(function (waitFor) {
+                    console.log("Here is the test1");
+                    APP.editor.setValue("Content for the description");
+                    $('.cp-app-poll-table-editing .cp-app-poll-table-text-cell input').val(
+                        'Candy').keyup();
+                    $('#cp-app-poll-create-option').click();
+                    // TODO(cjd): Need to click outside to lock the first option we create.. bug?
+                    $(window).trigger({ type: "click", which: 1 });
+                    setTimeout(waitFor());
+                }).nThen(function (waitFor) {
+                    $('.cp-app-poll-table-editing .cp-app-poll-table-text-cell input').val(
+                        'IceCream').keyup();
+                    $('#cp-app-poll-create-option').click();
+                    setTimeout(waitFor());
+                }).nThen(function (waitFor) {
+                    $('.cp-app-poll-table-editing .cp-app-poll-table-text-cell input').val(
+                        'Soda').keyup();
+                    $('#cp-app-poll-create-option').click();
+                    setTimeout(waitFor());
+                }).nThen(function (waitFor) {
+                    // Switch to non-admin mode
+                    $('.cp-toolbar-rightside-button.fa-check').click();
+                    setTimeout(waitFor());
+                }).nThen(function (waitFor) {
+                    $('.cp-app-poll-comments-add-name').val("Mr.Me").keyup();
+                    $('.cp-app-poll-comments-add-msg').val("Example comment yay").keyup();
+                    setTimeout(waitFor());
+                }).nThen(function (waitFor) {
+                    $('.cp-app-poll-comments-add-submit').click();
+                    setTimeout(waitFor());
+                }).nThen(function (waitFor) {
+                    $('#cp-app-poll-create-user').parent().find('input').val('Meeee').keyup();
+                    [1,3,2].forEach(function (num, i) {
+                        var x = $($('.cp-app-poll-table-checkbox-contain label')[i]);
+                        for (var ii = 0; ii < num; ii++) {
+                            x.trigger({ type: 'click', which: 1 });
+                        }
+                    });
+                    setTimeout(waitFor());
+                }).nThen(function (waitFor) {
+                    $('#cp-app-poll-create-user').click();
+                    setTimeout(waitFor());
+                }).nThen(function (waitFor) {
+                    passIfOk(t);
+                });
+            });
+        } else {
+            Test(passIfOk);
+        }
+
         UI.removeLoadingScreen();
         if (isNew) {
             common.openTemplatePicker();