DEV: Upgrade sinon and fix time based bookmark tests (#9647)

DEV: Upgrade sinon and fix time based bookmark tests (#9647)

Update sinon.js to 9.0.2 to access async fake timers https://sinonjs.org/releases/v9.0.2/fake-timers/ which can then be used with acceptance tests (previously useFakeTimers didn’t work with await, e.g. for visit).

Fix the bookmark acceptance test that was time based to use these new fake timers.

Add a fakeTime function that uses moment and the provided date string + timezone to freeze time using useFakeTimers and return a clock.

Add a timeStep function that accepts a clock from fakeTime and a function to run. Once the function is run we call clock.tickAsync(1000) to progress the fake clock forward 1s to progress promises/callbacks.

diff --git a/package.json b/package.json
index 95dc2e4..59ed84c 100644
--- a/package.json
+++ b/package.json
@@ -52,7 +52,7 @@
     "puppeteer": "1.20",
     "qunit": "2.8.0",
     "route-recognizer": "^0.3.3",
-    "sinon": "^7.2.5"
+    "sinon": "^9.0.2"
   },
   "scripts": {
     "preinstall": "node -e \"if(process.env.npm_execpath.indexOf('yarn') === -1) throw new Error('NPM is not supported, please use Yarn instead. ')\""
diff --git a/test/javascripts/acceptance/bookmarks-test.js b/test/javascripts/acceptance/bookmarks-test.js
index d30ff49..df8fb56 100644
--- a/test/javascripts/acceptance/bookmarks-test.js
+++ b/test/javascripts/acceptance/bookmarks-test.js
@@ -1,8 +1,18 @@
-import { acceptance, loggedInUser } from "helpers/qunit-helpers";
+import {
+  acceptance,
+  loggedInUser,
+  fakeTime,
+  timeStep
+} from "helpers/qunit-helpers";
 import pretender from "helpers/create-pretender";
 import { parsePostData } from "helpers/create-pretender";
 
-acceptance("Bookmarking", { loggedIn: true });
+acceptance("Bookmarking", {
+  loggedIn: true,
+  afterEach() {
+    sandbox.restore();
+  }
+});
 
 function handleRequest(assert, request) {
   const data = parsePostData(request.requestBody);
@@ -196,25 +206,26 @@ test("Editing a bookmark", async assert => {
   assert.verifySteps(["tomorrow"]);
 });
 
-QUnit.skip(
-  "Editing a bookmark that has a Later Today reminder, and it is before 6pm today",
-  async assert => {
-    // This test needs to mock the current time. sandbox.useFakeTimers() seems to break the `visit` step
-
-    mockSuccessfulBookmarkPost(assert);
-    await visit("/t/internationalization-localization/280");
-    await openBookmarkModal();
-    await fillIn("input#bookmark-name", "Test name");
-    await click("#tap_tile_later_today");
-    await openEditBookmarkModal();
-    assert.not(
-      exists("#bookmark-custon-date > input"),
-      "it does not show the custom date input"
-    );
-    assert.ok(
-      exists("#tap_tile_later_today.active"),
-      "it preselects Later Today"
-    );
-    assert.verifySteps(["later_today"]);
-  }
-);
+test("Editing a bookmark that has a Later Today reminder, and it is before 6pm today", async assert => {
+  mockSuccessfulBookmarkPost(assert);
+  let clock = fakeTime(
+    "2020-05-04T13:00:00",
+    loggedInUser().resolvedTimezone()
+  );
+  await timeStep(clock, () =>
+    visit("/t/internationalization-localization/280")
+  );
+  await timeStep(clock, () => openBookmarkModal());
+  await timeStep(clock, () => fillIn("input#bookmark-name", "Test name"));
+  await timeStep(clock, () => click("#tap_tile_later_today"));
+  await timeStep(clock, () => openEditBookmarkModal());
+  assert.not(
+    exists("#bookmark-custon-date > input"),
+    "it does not show the custom date input"
+  );
+  assert.ok(
+    exists("#tap_tile_later_today.active"),
+    "it preselects Later Today"
+  );
+  assert.verifySteps(["later_today"]);
+});
diff --git a/test/javascripts/controllers/bookmark-test.js b/test/javascripts/controllers/bookmark-test.js
index 67d9187..246d10d 100644
--- a/test/javascripts/controllers/bookmark-test.js
+++ b/test/javascripts/controllers/bookmark-test.js
@@ -2,6 +2,7 @@ import { logIn } from "helpers/qunit-helpers";
 import User from "discourse/models/user";
 import KeyboardShortcutInitializer from "discourse/initializers/keyboard-shortcuts";
 import { REMINDER_TYPES } from "discourse/lib/bookmark";
+import { fakeTime } from "helpers/qunit-helpers";
 let BookmarkController;
 
 moduleFor("controller:bookmark", {
@@ -18,8 +19,7 @@ moduleFor("controller:bookmark", {
 });
 
 function mockMomentTz(dateString) {
-  let now = moment.tz(dateString, BookmarkController.userTimezone);
-  sandbox.useFakeTimers(now.valueOf());
+  fakeTime(dateString, BookmarkController.userTimezone);
 }
 
 QUnit.test("showLaterToday when later today is tomorrow do not show", function(
diff --git a/test/javascripts/helpers/qunit-helpers.js b/test/javascripts/helpers/qunit-helpers.js
index 0bc9eae..2dd5c79 100644
--- a/test/javascripts/helpers/qunit-helpers.js
+++ b/test/javascripts/helpers/qunit-helpers.js
@@ -41,6 +41,16 @@ export function loggedInUser() {
   return User.current();
 }
 
+export function fakeTime(timeString, timezone) {
+  let now = moment.tz(timeString, timezone);
+  return sandbox.useFakeTimers(now.valueOf());
+}
+
+export async function timeStep(clock, fn) {
+  fn();
+  return await clock.tickAsync(1000);
+}
+
 const Plugin = $.fn.modal;
 const Modal = Plugin.Constructor;
 
diff --git a/test/javascripts/lib/bookmark-test.js b/test/javascripts/lib/bookmark-test.js
index 1e2530a..c863c88 100644
--- a/test/javascripts/lib/bookmark-test.js
+++ b/test/javascripts/lib/bookmark-test.js
@@ -1,10 +1,13 @@
 import { formattedReminderTime } from "discourse/lib/bookmark";
+import { fakeTime } from "helpers/qunit-helpers";
 
 QUnit.module("lib:bookmark", {
   beforeEach() {
-    // set the current now time for all tests
-    let now = moment.tz("2020-04-11 08:00:00", "Australia/Brisbane");
-    sandbox.useFakeTimers(now.valueOf());
+    fakeTime("2020-04-11 08:00:00", "Australia/Brisbane");
+  },
+
+  afterEach() {
+    sandbox.restore();
   }
 });
 
diff --git a/yarn.lock b/yarn.lock
index 742a3bd..5173f28 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -159,29 +159,43 @@
   resolved "https://registry.yarnpkg.com/@simple-dom/interface/-/interface-1.4.0.tgz#e8feea579232017f89b0138e2726facda6fbb71f"
   integrity sha512-l5qumKFWU0S+4ZzMaLXFU8tQZsicHEMEyAxI5kDFGhJsRqDwe0a7/iPA/GdxlGyDKseQQAgIz5kzU7eXTrlSpA==
 
-"@sinonjs/commons@^1", "@sinonjs/commons@^1.0.2", "@sinonjs/commons@^1.3.0":
+"@sinonjs/commons@^1":
   version "1.3.0"
   resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.3.0.tgz#50a2754016b6f30a994ceda6d9a0a8c36adda849"
   integrity sha512-j4ZwhaHmwsCb4DlDOIWnI5YyKDNMoNThsmwEpfHx6a1EpsGZ9qYLxP++LMlmBRjtGptGHFsGItJ768snllFWpA==
   dependencies:
     type-detect "4.0.8"
 
-"@sinonjs/formatio@^3.1.0":
-  version "3.1.1"
-  resolved "https://registry.yarnpkg.com/@sinonjs/formatio/-/formatio-3.1.1.tgz#3b40de6412b6475cfb046f303b9d8ee13bc7e7ae"
-  integrity sha512-kMqzWDvtplLhIfqlsDSM2i7T37iHPyEa3Y2Mon/DNE84fnOHheRW0jpuJCxiGUcS5DLs+yGtJPyJpN9rdqMjlA==
+"@sinonjs/commons@^1.6.0", "@sinonjs/commons@^1.7.0", "@sinonjs/commons@^1.7.2":
+  version "1.7.2"
+  resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.7.2.tgz#505f55c74e0272b43f6c52d81946bed7058fc0e2"
+  integrity sha512-+DUO6pnp3udV/v2VfUWgaY5BIE1IfT7lLfeDzPVeMT1XKkaAp9LgSI9x5RtrFQoZ9Oi0PgXQQHPaoKu7dCjVxw==
+  dependencies:
+    type-detect "4.0.8"
+
+"@sinonjs/fake-timers@^6.0.0", "@sinonjs/fake-timers@^6.0.1":
+  version "6.0.1"
+  resolved "https://registry.yarnpkg.com/@sinonjs/fake-timers/-/fake-timers-6.0.1.tgz#293674fccb3262ac782c7aadfdeca86b10c75c40"
+  integrity sha512-MZPUxrmFubI36XS1DI3qmI0YdN1gks62JtFZvxR67ljjSNCeK6U08Zx4msEWOXuofgqUt6zPHSi1H9fbjR/NRA==
+  dependencies:
+    "@sinonjs/commons" "^1.7.0"
+
+"@sinonjs/formatio@^5.0.1":
+  version "5.0.1"
+  resolved "https://registry.yarnpkg.com/@sinonjs/formatio/-/formatio-5.0.1.tgz#f13e713cb3313b1ab965901b01b0828ea6b77089"
+  integrity sha512-KaiQ5pBf1MpS09MuA0kp6KBQt2JUOQycqVG1NZXvzeaXe5LGFqAKueIS0bw4w0P9r7KuBSVdUk5QjXsUdu2CxQ==
   dependencies:
     "@sinonjs/commons" "^1"
-    "@sinonjs/samsam" "^3.1.0"
+    "@sinonjs/samsam" "^5.0.2"
 
-"@sinonjs/samsam@^3.1.0", "@sinonjs/samsam@^3.2.0":
-  version "3.2.0"
-  resolved "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-3.2.0.tgz#58c62b5f1f42e46d039d073d0ae2753da676bf0c"
-  integrity sha512-j5F1rScewLtx6pbTK0UAjA3jJj4RYiSKOix53YWv+Jzy/AZ69qHxUpU8fwVLjyKbEEud9QrLpv6Ggs7WqTimYw==
+"@sinonjs/samsam@^5.0.2", "@sinonjs/samsam@^5.0.3":
+  version "5.0.3"
+  resolved "https://registry.yarnpkg.com/@sinonjs/samsam/-/samsam-5.0.3.tgz#86f21bdb3d52480faf0892a480c9906aa5a52938"
+  integrity sha512-QucHkc2uMJ0pFGjJUDP3F9dq5dx8QIaqISl9QgwLOh6P9yv877uONPGXh/OH/0zmM3tW1JjuJltAZV2l7zU+uQ==
   dependencies:

[... diff too long, it was truncated ...]

GitHub sha: 7e303f93

This commit appears in #9647 which was approved by eviltrout. It was merged by martin.