Skip to content

Commit

Permalink
Updated tests to use modern ember idioms
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiupover committed Aug 8, 2021
1 parent 5a8af63 commit 045172a
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 68 deletions.
4 changes: 0 additions & 4 deletions .bowerrc

This file was deleted.

9 changes: 3 additions & 6 deletions addon/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import MockDate from 'mockdate';
import { run } from '@ember/runloop';
import { trySet } from "@ember/object";
import { trySet } from '@ember/object';

const originalDate = Date;
const originalPlatformNow = run.backburner._platform.now;

const { set, reset } = MockDate || {
set() {},
reset() {}
reset() {},
};

/*
Expand All @@ -28,7 +28,4 @@ const unfreezeDate = (...args) => {
reset(args);
};

export {
freezeDateAt,
unfreezeDate
};
export { freezeDateAt, unfreezeDate };
4 changes: 0 additions & 4 deletions bower.json

This file was deleted.

3 changes: 1 addition & 2 deletions config/release.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* jshint node:true */

module.exports = {
};
module.exports = {};
25 changes: 13 additions & 12 deletions tests/acceptance/index-test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { freezeDateAt, unfreezeDate } from 'ember-mockdate-shim';

moduleForAcceptance('Acceptance | index');
module('Acceptance | index', function (hooks) {
setupApplicationTest(hooks);

test('can visit index route with async model and freeze date in acceptance test', function(assert) {
assert.expect(3);

freezeDateAt('1/1/2000');
test('It mocks the date', async function (assert) {
freezeDateAt('1970-08-29');

visit('/');
await visit('/');

andThen(function() {
assert.equal(currentURL(), '/');

const frozenDateYear = document.getElementById('index-page').textContent.trim();

assert.equal(frozenDateYear, 2000, 'Year is the same as frozen date');
assert.dom('[data-test-dateyear]').hasText('1970');

unfreezeDate();

assert.ok(true, 'make it to the end of the acceptance test after freezing/unfreezing dates');
assert.ok(
true,
'make it to the end of the acceptance test after freezing/unfreezing dates'
);
});
});
1 change: 1 addition & 0 deletions tests/dummy/app/components/index-page.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span data-test-dateyear>{{this.dateYear}}</span>
17 changes: 6 additions & 11 deletions tests/dummy/app/components/index-page.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import Component from "@ember/component";
import layout from '../templates/components/index-page';
import Component from '@glimmer/component';

export default Component.extend({
layout,
elementId: 'index-page',

init() {
this._super(...arguments);
this.set('dateYear', new Date().getFullYear());
},
});
export default class IndexPageComponent extends Component {
get dateYear() {
return new Date().getFullYear();
}
}
14 changes: 7 additions & 7 deletions tests/dummy/app/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Route from "@ember/routing/route";
import { Promise } from "rsvp";
import { next } from "@ember/runloop";
import { Promise } from 'rsvp';
import { next } from '@ember/runloop';
import Route from '@ember/routing/route';

export default Route.extend({
export default class IndexRoute extends Route {
// Return async model to test that embers run loop doesn't fall
// into an infinite loop when freezing time in acceptance tests.
model() {
return new Promise((res => next(() => res())));
},
});
return new Promise((res) => next(() => res()));
}
}
1 change: 0 additions & 1 deletion tests/dummy/app/templates/components/index-page.hbs

This file was deleted.

2 changes: 1 addition & 1 deletion tests/dummy/app/templates/index.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{outlet}}

{{index-page}}
<IndexPage />
29 changes: 13 additions & 16 deletions tests/integration/components/index-page-test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { module, test } from 'qunit';
import { hbs } from 'ember-cli-htmlbars';
import { render } from '@ember/test-helpers';
import { setupRenderingTest } from 'ember-qunit';
import { freezeDateAt, unfreezeDate } from 'ember-mockdate-shim';

moduleForComponent('index-page', 'Integration | Component | index page', {
integration: true
});

test('freeze `new Date()` in component integration test', function(assert) {
assert.expect(2);

freezeDateAt('1/1/2000');
module('Integration | Component | index-page', function (hooks) {
setupRenderingTest(hooks);

this.render(hbs`{{index-page}}`);
test('freezes new Date', async function (assert) {
freezeDateAt('1999-12-26');

const frozenDateYear = this.$().text().trim();
await render(hbs`<IndexPage />`);

assert.equal(frozenDateYear, 2000, 'Year is the same as frozen date');
assert.dom('[data-test-dateyear]').hasText('1999');

unfreezeDate();

assert.ok(true, 'Made it to the end of the integration test')
unfreezeDate();
});
});

12 changes: 8 additions & 4 deletions tests/unit/utils/mockdate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { freezeDateAt, unfreezeDate } from 'ember-mockdate-shim';
module('Unit | Utility | mockdate', {
afterEach() {
unfreezeDate();
}
},
});

test('freezes `new Date()`', function(assert) {
test('freezes `new Date()`', function (assert) {
freezeDateAt(new Date('1/1/2000'));

const result = new Date();

assert.equal(result.getFullYear(), 2000, 'Year is the same as frozen date');
});

test('unfreezes date', function(assert) {
test('unfreezes date', function (assert) {
freezeDateAt(new Date('2/2/1992'));

const frozenResult = new Date();
Expand All @@ -24,5 +24,9 @@ test('unfreezes date', function(assert) {

const unfrozenResult = new Date();

assert.notEqual(frozenResult.getFullYear(), unfrozenResult.getFullYear(), "Date is different after unfreezing");
assert.notEqual(
frozenResult.getFullYear(),
unfrozenResult.getFullYear(),
'Date is different after unfreezing'
);
});

0 comments on commit 045172a

Please sign in to comment.