Skip to content

Instantly share code, notes, and snippets.

@ryanto
Forked from ro0gr/controllers.application.js
Created February 4, 2017 03:14
Show Gist options
  • Save ryanto/a2636afd619bbe2d81160df0049ece6d to your computer and use it in GitHub Desktop.
Save ryanto/a2636afd619bbe2d81160df0049ece6d to your computer and use it in GitHub Desktop.
infinite-wait-for
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
import { waitFor } from '../helpers/wait-for';
moduleForAcceptance('TODO: put something here');
QUnit.config.testTimeout = 1000;
test('visiting /my-acceptance', function(assert) {
visit('/');
return waitFor(".aaa", { interval: 500 });
});
import Ember from 'ember';
import { cleanup } from './wait-for';
export default function destroyApp(application) {
cleanup();
Ember.run(application, 'destroy');
}
import { module } from 'qunit';
import Ember from 'ember';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
const { RSVP: { Promise } } = Ember;
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
return options.beforeEach.apply(this, arguments);
}
},
afterEach() {
let afterEach = options.afterEach && options.afterEach.apply(this, arguments);
return Promise.resolve(afterEach).then(() => destroyApp(this.application));
}
});
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import Ember from 'ember';
const {
$,
Test: {
registerAsyncHelper,
},
RSVP: { Promise }
} = Ember;
function _waitFor(app, selectorOrFn, contextOrOptions, selectorOptions) {
let waitForFn;
let options;
// find the options argument
if (typeof contextOrOptions === "string") {
options = selectorOptions;
} else {
options = contextOrOptions;
}
// option defaults
options = options || {};
options.interval = options.interval || 10;
// Support old API where you can pass in a selector as
// a string and we'll wait for that to exist. Can also
// pass along context and count option.
if (typeof selectorOrFn === "string") {
let selector;
let count = options.count || 1;
// if context is a string we'll use it to scope the
// selector
if (typeof contextOrOptions === "string") {
let context = contextOrOptions;
selector = `${context} ${selectorOrFn}`;
} else {
selector = selectorOrFn;
}
waitForFn = selectorToExist(selector, count);
} else {
// new style, selectorOrFn is a function
waitForFn = selectorOrFn;
}
// TWIDDLE DEMO COUNTER
let counter = 0;
let $qunitHeader = $('#qunit-header');
let origHeaderHtml = $('#qunit-header').html();
// END
return new Promise((resolve) => {
let label = waitForFn;
let isComplete = waitForFn;
let stopTrying = function() {
return !_isActive(label);
};
let loop = function() {
let timer = setTimeout(peek, options.interval);
// TWIDDLE DEMO COUNTER
$qunitHeader.html(`${origHeaderHtml} ${++counter}`)
// END
_track(label, timer);
};
let peek = function() {
if (isComplete() || stopTrying()) {
resolve(_done(label));
} else {
loop();
}
};
loop();
});
}
// Normally we we use Map here, but that doesn't work
// in phantom without including the babel polyfill.
// Note that Ember.Map is private, so we may have
// to refactor this out at some point.
const _runningWaiters = new Ember.Map();
function _track(label, timer) {
_runningWaiters.set(label, timer);
}
function _cancel(label) {
_runningWaiters.delete(label);
}
function _isActive(label) {
return _runningWaiters.has(label);
}
function _done(label) {
_cancel(label);
}
export function activeCount() {
return _runningWaiters.size;
}
export function cleanup() {
return _runningWaiters.clear();
}
export function selectorToExist(selector, count) {
return function() {
let existsCount = $(selector).length;
if (count) {
return existsCount === count;
} else {
return existsCount > 0;
}
};
}
export function waitFor(selector, context, options) {
return _waitFor(null, selector, context, options);
}
export default registerAsyncHelper('waitFor', _waitFor);
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
import Ember from 'ember';
const { registerAsyncHelper } = Ember.Test;
{
"version": "0.10.7",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.10.0",
"ember-data": "2.10.0",
"ember-template-compiler": "2.10.0",
"ember-testing": "2.10.0"
},
"addons": {
"ember-wait-for-test-helper": "0.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment