DEV: Try once more to make acceptance test fake timers work

In my original PR (#9647) I attempted to solve the problem of
using fake timers in acceptance tests by using the new sinon
clock.tickAsync methods. This way of doing things seems to be flawed,
however, as we are getting random spec timeouts starting with the
bookmark acceptance test where this was introduced.

I think I was going about things the wrong way. This commit introduces
a new function with callback (acceptanceUseFakeClock) that sets up the
fake timers using sinon.useFakeTimers with the shouldAdvanceTime option
set to true. This advances time at a normal rate of 20ms per tick, which
means that we are not freezing any time and existing setTimeout funcs.
should proceed as normal. Along with this the callback passed will
run clock.reset() at the end to make sure all the timers are cleaned
up correctly.

There is an optional third parameter after the callback, which is the
timezone. If the user is logged in for the acceptance test then their
timezone is used, otherwise we default to America/Denver.

Usage is (inside an acceptance test):

```
test("Name of the test", async assert => {
  // first parameter is time to start fake clock at
  await acceptanceUseFakeClock("2020-05-04T13:00:00", async () => {
    // test code goes here e.g. await visit("/url");
  });
});
```
This commit is contained in:
Martin Brennan 2020-05-13 15:16:07 +10:00
parent a64cf265fd
commit 0e09c5837f
2 changed files with 32 additions and 24 deletions

View File

@ -1,8 +1,7 @@
import {
acceptance,
loggedInUser,
fakeTime,
timeStep
acceptanceUseFakeClock
} from "helpers/qunit-helpers";
import pretender from "helpers/create-pretender";
import { parsePostData } from "helpers/create-pretender";
@ -230,23 +229,16 @@ 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 => {
test("Editing a bookmark that has a Later Today reminder, and it is before 6pm today", async assert => {
await acceptanceUseFakeClock("2020-05-04T13:00:00", async () => {
mockSuccessfulBookmarkPost(assert);
let clock = fakeTime(
"2020-05-04T13:00:00",
loggedInUser().resolvedTimezone(loggedInUser())
);
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());
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"),
exists("#bookmark-custom-date > input"),
"it does not show the custom date input"
);
assert.ok(
@ -254,5 +246,5 @@ QUnit.skip(
"it preselects Later Today"
);
assert.verifySteps(["later_today"]);
}
);
});
});

View File

@ -41,14 +41,30 @@ export function loggedInUser() {
return User.current();
}
export function fakeTime(timeString, timezone) {
export function fakeTime(timeString, timezone = null, advanceTime = false) {
let now = moment.tz(timeString, timezone);
return sandbox.useFakeTimers(now.valueOf());
return sandbox.useFakeTimers({
now: now.valueOf(),
shouldAdvanceTime: advanceTime
});
}
export async function timeStep(clock, fn) {
fn();
return await clock.tickAsync(1000);
export async function acceptanceUseFakeClock(
timeString,
callback,
timezone = null
) {
if (!timezone) {
let user = loggedInUser();
if (user) {
timezone = user.resolvedTimezone(user);
} else {
timezone = "America/Denver";
}
}
let clock = fakeTime(timeString, timezone, true);
await callback();
clock.reset();
}
const Plugin = $.fn.modal;