adopted-ember-addons / ember-moment

MIT License
400 stars 122 forks source link

Freeze moment for testing #301

Open AndreJoaquim opened 5 years ago

AndreJoaquim commented 5 years ago

Hey! I've used this add-on for a long time and I has served my needs perfectly.

However, one thing that I'd like to see included in this add-on is a helper to freeze moment (i.e. override moment.now) in tests, namely acceptance tests. Below I show what the API could look like:

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { setupMoment } from 'ember-moment/test-support';

module('Integration | Component | x-event/date', function(hooks) {
  setupRenderingTest(hooks);
  setupMoment(hooks, '2042-01-01');

  test('it renders', async function(assert) {
    // This component only displays today's date in the format "DD/MM/YYYY"
    await render(hbs`{{x-event/date}}`);

    assert.dom(this.element).hasText('01/01/2042');
  });
});

This would allow (more) deterministic tests, instead of relying of always getting today's date to see if the behaviour(s) is(are) correct.

What do you think about this feature? Thanks! 😄

jasonmit commented 5 years ago

@AndreJoaquim I love it! Feel free to send a PR (or anyone for that matter) if you have the cycles!