1- const fs = require ( 'fs' ) ;
2- const vm = require ( 'vm' ) ;
3- const SimpleDOM = require ( 'simple-dom' ) ;
4- const { emberPath, loadEmber, clearEmber } = require ( './helpers/load-ember' ) ;
1+ import SimpleDOM from 'simple-dom' ;
2+ import Application from 'ember-source/@ember/application/index.js' ;
3+ import EmberRouter from 'ember-source/@ember/routing/router.js' ;
4+ import { run } from 'ember-source/@ember/runloop/index.js' ;
5+ import { precompile } from 'ember-source/ember-template-compiler/index.js' ;
6+ import { createTemplateFactory } from 'ember-source/@ember/template-factory/index.js' ;
7+
8+ function compile ( templateString , options ) {
9+ let templateSpec = precompile ( templateString , options ) ;
10+ return createTemplateFactory ( JSON . parse ( templateSpec ) ) ;
11+ }
512
613// This is based on what fastboot-server does
714let HTMLSerializer = new SimpleDOM . HTMLSerializer ( SimpleDOM . voidMap ) ;
815
9- async function fastbootVisit ( context , url ) {
16+ async function fastbootVisit ( app , url ) {
1017 let doc = new SimpleDOM . Document ( ) ;
1118 let rootElement = doc . body ;
1219 let options = { isBrowser : false , document : doc , rootElement : rootElement } ;
1320
14- let { app } = context ;
15-
1621 await app . boot ( ) ;
1722
1823 let instance = await app . buildInstance ( ) ;
@@ -31,84 +36,40 @@ async function fastbootVisit(context, url) {
3136 }
3237}
3338
34- // essentially doing the same as what is done in FastBoot 3.1.0
35- // https://github.com/ember-fastboot/fastboot/blob/v3.1.0/src/sandbox.js
36- function buildSandboxContext ( precompile ) {
37- let URL = require ( 'url' ) ;
38-
39- let sandbox = {
40- console,
41- setTimeout,
42- clearTimeout,
43- URL ,
44-
45- // Convince jQuery not to assume it's in a browser
46- module : { exports : { } , require ( ) { } } ,
47- } ;
48-
49- // Set the global as `window`
50- sandbox . window = sandbox ;
51- sandbox . window . self = sandbox ;
52-
53- let context = vm . createContext ( sandbox ) ;
54-
55- let environmentSetupScript = new vm . Script (
56- `
57- var EmberENV = {
58- _DEFAULT_ASYNC_OBSERVERS: true,
59- _JQUERY_INTEGRATION: false,
60- };` ,
61- { filename : 'prepend.js' }
62- ) ;
63- environmentSetupScript . runInContext ( context ) ;
64-
65- let emberSource = fs . readFileSync ( emberPath , { encoding : 'utf-8' } ) ;
66- let emberScript = new vm . Script ( emberSource , { filename : emberPath } ) ;
67- emberScript . runInContext ( context ) ;
68-
69- let applicationSource = `
70- let Ember = module.exports;
71-
72- class Router extends Ember.Router {}
73- Router.map(function() {
74- this.route('a');
75- this.route('b');
76- });
77-
78- const registry = {
79- 'router:main': Router,
80- 'template:application': ${ precompile ( '<h1>Hello world!</h1>\n{{outlet}}' ) }
81- };
82-
83- class Resolver extends Ember.Object {
84- resolve(specifier) {
85- return registry[specifier];
86- }
87- }
88-
89- var app = class extends Ember.Application {}.create({
90- autoboot: false,
91- Resolver,
92- });
93- ` ;
94- let appScript = new vm . Script ( applicationSource , { filename : 'app.js' } ) ;
95- appScript . runInContext ( context ) ;
96-
97- return context ;
98- }
99-
10039QUnit . module ( 'Ember.Application - visit() Integration Tests' , function ( hooks ) {
101- hooks . beforeEach ( function ( ) {
102- let { precompile } = loadEmber ( ) ;
103- this . context = buildSandboxContext ( precompile ) ;
104- } ) ;
40+ let app ;
10541
10642 hooks . afterEach ( function ( ) {
107- clearEmber ( ) ;
43+ if ( app ) {
44+ run ( app , 'destroy' ) ;
45+ app = null ;
46+ }
10847 } ) ;
10948
11049 QUnit . test ( 'FastBoot: basic' , async function ( assert ) {
111- let result = await fastbootVisit ( this . context , '/' ) ;
50+ let Router = class extends EmberRouter { } ;
51+ Router . map ( function ( ) {
52+ this . route ( 'a' ) ;
53+ this . route ( 'b' ) ;
54+ } ) ;
55+
56+ let registry = {
57+ 'router:main' : Router ,
58+ 'template:application' : compile ( '<h1>Hello world!</h1>\n{{outlet}}' ) ,
59+ } ;
60+
61+ app = class extends Application { } . create ( {
62+ autoboot : false ,
63+ Resolver : {
64+ create : ( ) => ( {
65+ resolve ( specifier ) {
66+ return registry [ specifier ] ;
67+ } ,
68+ } ) ,
69+ } ,
70+ } ) ;
71+
72+ let result = await fastbootVisit ( app , '/' ) ;
11273
11374 assert . equal ( result . url , '/' , 'landed on correct url' ) ;
11475 assert . equal (
0 commit comments