|
| 1 | +import { test } from 'qunit'; |
| 2 | +import moduleForAcceptance from 'ember-twiddle/tests/helpers/module-for-acceptance'; |
| 3 | +import { timeout } from 'ember-concurrency'; |
| 4 | + |
| 5 | +moduleForAcceptance('Acceptance | acceptance-application-test', { |
| 6 | + beforeEach: function() { |
| 7 | + this.cachePrompt = window.prompt; |
| 8 | + window.prompt = (text, defaultResponse) => defaultResponse; |
| 9 | + }, |
| 10 | + |
| 11 | + afterEach: function() { |
| 12 | + window.prompt = this.cachePrompt; |
| 13 | + } |
| 14 | +}); |
| 15 | + |
| 16 | +test('An acceptance test for an application works', function(assert) { |
| 17 | + |
| 18 | + const files = [ |
| 19 | + { |
| 20 | + filename: "application.template.hbs", |
| 21 | + content: `Welcome to {{appName}}` |
| 22 | + }, |
| 23 | + { |
| 24 | + filename: "application.controller.js", |
| 25 | + content: `import Ember from "ember"; |
| 26 | + export default Ember.Controller.extend({ |
| 27 | + appName: 'Ember Twiddle' |
| 28 | + });` |
| 29 | + }, |
| 30 | + { |
| 31 | + filename: "twiddle.json", |
| 32 | + content: `{ |
| 33 | + "version": "0.13.0", |
| 34 | + "EmberENV": { |
| 35 | + "FEATURES": {} |
| 36 | + }, |
| 37 | + "options": { |
| 38 | + "use_pods": false, |
| 39 | + "enable-testing": true |
| 40 | + }, |
| 41 | + "dependencies": { |
| 42 | + "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js" |
| 43 | + } |
| 44 | + }` |
| 45 | + }, |
| 46 | + { |
| 47 | + filename: "components/my-component.js", |
| 48 | + content: `import Ember from 'ember'; |
| 49 | +
|
| 50 | + export default Ember.Component.extend({ |
| 51 | + });` |
| 52 | + }, |
| 53 | + { |
| 54 | + filename: "templates/components/my-component.hbs", |
| 55 | + content: `{{yield}}` |
| 56 | + }, |
| 57 | + { |
| 58 | + filename: "tests/test-helper.js", |
| 59 | + content: `import resolver from './helpers/resolver'; |
| 60 | + import { |
| 61 | + setResolver |
| 62 | + } from 'ember-qunit'; |
| 63 | + import jQuery from 'jquery'; |
| 64 | +
|
| 65 | + setResolver(resolver); |
| 66 | +
|
| 67 | + window.testModule = 'twiddle/tests/acceptance/application-test'; |
| 68 | + ` |
| 69 | + }, |
| 70 | + { |
| 71 | + filename: "tests/helpers/resolver.js", |
| 72 | + content: `import Resolver from '../../resolver'; |
| 73 | + import config from '../../config/environment'; |
| 74 | +
|
| 75 | + const resolver = Resolver.create(); |
| 76 | +
|
| 77 | + resolver.namespace = { |
| 78 | + modulePrefix: config.modulePrefix, |
| 79 | + podModulePrefix: config.podModulePrefix |
| 80 | + }; |
| 81 | +
|
| 82 | + export default resolver;` |
| 83 | + }, |
| 84 | + { |
| 85 | + filename: "tests/helpers/module-for-acceptance.js", |
| 86 | + content: `import { module } from 'qunit'; |
| 87 | + import Ember from 'ember'; |
| 88 | + import startApp from '../helpers/start-app'; |
| 89 | + import destroyApp from '../helpers/destroy-app'; |
| 90 | +
|
| 91 | + const { RSVP: { Promise } } = Ember; |
| 92 | +
|
| 93 | + export default function(name, options = {}) { |
| 94 | + module(name, { |
| 95 | + beforeEach() { |
| 96 | + this.application = startApp(); |
| 97 | +
|
| 98 | + if (options.beforeEach) { |
| 99 | + return options.beforeEach.apply(this, arguments); |
| 100 | + } |
| 101 | + }, |
| 102 | +
|
| 103 | + afterEach() { |
| 104 | + let afterEach = options.afterEach && options.afterEach.apply(this, arguments); |
| 105 | + return Promise.resolve(afterEach).then(() => destroyApp(this.application)); |
| 106 | + } |
| 107 | + }); |
| 108 | + }` |
| 109 | + }, |
| 110 | + { |
| 111 | + filename: "tests/helpers/start-app.js", |
| 112 | + content: `import Ember from 'ember'; |
| 113 | + import Application from '../../app'; |
| 114 | + import config from '../../config/environment'; |
| 115 | +
|
| 116 | + const { run } = Ember; |
| 117 | + const assign = Ember.assign || Ember.merge; |
| 118 | +
|
| 119 | + export default function startApp(attrs) { |
| 120 | + let application; |
| 121 | +
|
| 122 | + let attributes = assign({rootElement: "#test-root"}, config.APP); |
| 123 | + attributes = assign(attributes, attrs); // use defaults, but you can override; |
| 124 | +
|
| 125 | + run(() => { |
| 126 | + application = Application.create(attributes); |
| 127 | + application.setupForTesting(); |
| 128 | + application.injectTestHelpers(); |
| 129 | + }); |
| 130 | +
|
| 131 | + return application; |
| 132 | + } |
| 133 | + ` |
| 134 | + }, |
| 135 | + { |
| 136 | + filename: "tests/helpers/destroy-app.js", |
| 137 | + content: `import Ember from 'ember'; |
| 138 | +
|
| 139 | + export default function destroyApp(application) { |
| 140 | + Ember.run(application, 'destroy'); |
| 141 | + }` |
| 142 | + }, |
| 143 | + { |
| 144 | + filename: "tests/acceptance/application-test.js", |
| 145 | + content: `import { test } from 'qunit'; |
| 146 | + import moduleForAcceptance from '../../tests/helpers/module-for-acceptance'; |
| 147 | +
|
| 148 | + moduleForAcceptance('TODO: put something here'); |
| 149 | +
|
| 150 | + test('visiting /application', function(assert) { |
| 151 | + visit('/'); |
| 152 | +
|
| 153 | + andThen(function() { |
| 154 | + assert.equal(currentURL(), '/', 'route loaded correctly'); |
| 155 | + }); |
| 156 | + });` |
| 157 | + } |
| 158 | + ]; |
| 159 | + |
| 160 | + runGist(files); |
| 161 | + |
| 162 | + andThen(function() { |
| 163 | + return timeout(500); // TODO: fix and remove this timing hack |
| 164 | + }); |
| 165 | + |
| 166 | + andThen(function() { |
| 167 | + const outputSpan = 'div#qunit-testresult-display > span.passed'; |
| 168 | + |
| 169 | + assert.equal(outputPane().$(outputSpan).text(), '1', 'acceptance test passed'); |
| 170 | + }); |
| 171 | +}); |
0 commit comments