Skip to content

Commit

Permalink
Mocking of Date
Browse files Browse the repository at this point in the history
  • Loading branch information
zfir committed Jul 14, 2024
1 parent da348b5 commit b7a8ed2
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 2 deletions.
140 changes: 140 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@types/htmlbars-inline-precompile": "^3.0.0",
"@types/qunit": "^2.19.10",
"@types/rsvp": "^4.0.9",
"@types/sinon": "^17.0.3",
"broccoli-asset-rev": "^3.0.0",
"concurrently": "^8.2.2",
"ember-auto-import": "^2.7.0",
Expand Down Expand Up @@ -96,6 +97,7 @@
"prettier": "^3.1.1",
"qunit": "^2.20.0",
"qunit-dom": "^2.0.0",
"sinon": "^18.0.0",
"stylelint": "^15.11.0",
"stylelint-config-standard": "^34.0.0",
"stylelint-prettier": "^4.1.0",
Expand Down
19 changes: 17 additions & 2 deletions tests/integration/components/user-info-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { find, render, settled, waitFor, waitUntil } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import sinon from 'sinon';

module('Integration | Component | user-info', function (hooks) {
setupRenderingTest(hooks);

let clock: sinon.SinonFakeTimers;
let date: Date;

hooks.beforeEach(function () {
date = new Date('1999-01-01T00:00:00Z');
clock = sinon.useFakeTimers(date.getTime());
});

hooks.afterEach(function () {
clock.restore();
});

// Fails
// test('it fails without await settled to show user info', async function (assert) {
// await render(hbs`<UserInfo />`);
Expand Down Expand Up @@ -38,6 +51,7 @@ module('Integration | Component | user-info', function (hooks) {
// Passes
test('it passes with waitUntil to show updated text', async function (assert) {
await render(hbs`<UserInfo />`);
clock.tick(2000);

const found = await waitUntil(() => {
const div = find('.user-info') as HTMLDivElement;
Expand All @@ -52,16 +66,17 @@ module('Integration | Component | user-info', function (hooks) {
// await render(hbs`<UserInfo />`);

// const div = find('.date') as HTMLDivElement;
// assert.true(div.textContent?.includes(new Date() as unknown as string));
// assert.true(div.textContent?.includes("Fri Jan 01 1999 04:00:02 GMT+0400 (Mauritius Standard Time)"));
// });

// Passes
test('it passes with waitFor to show date', async function (assert) {
await render(hbs`<UserInfo />`);
clock.tick(2000);

await waitFor('.date', { timeout: 3000 });
const div = find('.date') as HTMLDivElement;

assert.true(div.textContent?.includes(new Date() as unknown as string));
assert.true(div.textContent?.includes("Fri Jan 01 1999 04:00:02 GMT+0400 (Mauritius Standard Time)"));
});
});

0 comments on commit b7a8ed2

Please sign in to comment.