@@ -23,7 +23,7 @@ require('fetch-test');
2323 addon = Object . create ( AddonFactory ) ;
2424 Object . assign ( addon , {
2525 addons : [ ] ,
26- buildConfig : {
26+ _fetchBuildConfig : {
2727 preferNative
2828 } ,
2929 ui : {
@@ -95,4 +95,182 @@ require('fetch-test');
9595 } ) )
9696 }
9797 } ) ;
98+
99+ describe ( `Build browser assets with preferNative = ${ preferNative } in nested dependencies` , function ( ) {
100+ let output , subject , addon ;
101+
102+ beforeEach ( function ( ) {
103+ addon = Object . create ( AddonFactory ) ;
104+
105+ let app = {
106+ _fetchBuildConfig : {
107+ preferNative
108+ }
109+ } ;
110+
111+ Object . assign ( addon , {
112+ addons : [ ] ,
113+ _findHost ( ) {
114+ return app ;
115+ } ,
116+ ui : {
117+ writeWarnLine ( ) { } ,
118+ } ,
119+ } ) ;
120+ subject = addon . treeForVendor ( ) ;
121+ output = helpers . createBuilder ( subject ) ;
122+ } ) ;
123+
124+ afterEach ( co . wrap ( function * ( ) {
125+ yield output . dispose ( ) ;
126+ } ) ) ;
127+
128+ it ( 'preferNative is built into vendor file' , co . wrap ( function * ( ) {
129+ yield output . build ( ) ;
130+ let files = output . read ( ) ;
131+ expect ( files ) . to . have . all . keys ( 'ember-fetch.js' ) ;
132+ expect ( files [ 'ember-fetch.js' ] ) . to . include ( `var preferNative = ${ preferNative } ` ) ;
133+ } ) ) ;
134+
135+ it ( `${
136+ preferNative ? 'Prefers' : "Doesn't prefer"
137+ } native fetch as specified`, co . wrap ( function * ( ) {
138+ yield output . build ( ) ;
139+ let emberFetchCode = output . read ( ) [ 'ember-fetch.js' ] ;
140+ const amdLoaderCode = fs . readFileSync ( require . resolve ( 'loader.js' ) ) ;
141+ const sandbox = {
142+ __result : false ,
143+ window : {
144+ fetch : function ( ) {
145+ sandbox . __result = true ;
146+ } ,
147+ Ember : { RSVP }
148+ }
149+ } ;
150+ vm . createContext ( sandbox ) ;
151+ const code = amdLoaderCode + emberFetchCode + testCode ;
152+ vm . runInContext ( code , sandbox ) ;
153+ expect ( sandbox . __result ) . to . equal ( preferNative ) ;
154+ } ) ) ;
155+
156+ if ( preferNative === true ) {
157+ it ( 'Has fetch poly fill even if fetch is not there' , co . wrap ( function * ( ) {
158+ yield output . build ( ) ;
159+ let emberFetchCode = output . read ( ) [ 'ember-fetch.js' ] ;
160+ const amdLoaderCode = fs . readFileSync ( require . resolve ( 'loader.js' ) ) ;
161+ const sandbox = {
162+ console,
163+ window : {
164+ __result : false ,
165+ // no fetch here
166+ // fetch: function() {},
167+ Ember : { RSVP }
168+ }
169+ } ;
170+ vm . createContext ( sandbox ) ;
171+ const testCodeForNonFetch = `
172+ define('fetch-test', ['fetch'], function(_fetch) {
173+ if (_fetch.default.polyfill) {
174+ window.__result = true
175+ }
176+ });
177+ require('fetch-test');
178+ ` ;
179+ const code = amdLoaderCode + emberFetchCode + testCodeForNonFetch ;
180+ vm . runInContext ( code , sandbox ) ;
181+ expect ( sandbox . window . __result ) . to . equal ( true ) ;
182+ } ) )
183+ }
184+ } ) ;
185+
186+ describe ( `Build browser assets with preferNative = ${ preferNative } in nested dependencies without _findHost` , function ( ) {
187+ let output , subject , addon ;
188+
189+ beforeEach ( function ( ) {
190+ addon = Object . create ( AddonFactory ) ;
191+
192+ let app = {
193+ _fetchBuildConfig : {
194+ preferNative
195+ }
196+ } ;
197+
198+ Object . assign ( addon , {
199+ addons : [ ] ,
200+ app : this ,
201+ parent : {
202+ parent : {
203+ app,
204+ parent : { }
205+ }
206+ } ,
207+ ui : {
208+ writeWarnLine ( ) { } ,
209+ } ,
210+ } ) ;
211+ subject = addon . treeForVendor ( ) ;
212+ output = helpers . createBuilder ( subject ) ;
213+ } ) ;
214+
215+ afterEach ( co . wrap ( function * ( ) {
216+ yield output . dispose ( ) ;
217+ } ) ) ;
218+
219+ it ( 'preferNative is built into vendor file' , co . wrap ( function * ( ) {
220+ yield output . build ( ) ;
221+ let files = output . read ( ) ;
222+ expect ( files ) . to . have . all . keys ( 'ember-fetch.js' ) ;
223+ expect ( files [ 'ember-fetch.js' ] ) . to . include ( `var preferNative = ${ preferNative } ` ) ;
224+ } ) ) ;
225+
226+ it ( `${
227+ preferNative ? 'Prefers' : "Doesn't prefer"
228+ } native fetch as specified`, co . wrap ( function * ( ) {
229+ yield output . build ( ) ;
230+ let emberFetchCode = output . read ( ) [ 'ember-fetch.js' ] ;
231+ const amdLoaderCode = fs . readFileSync ( require . resolve ( 'loader.js' ) ) ;
232+ const sandbox = {
233+ __result : false ,
234+ window : {
235+ fetch : function ( ) {
236+ sandbox . __result = true ;
237+ } ,
238+ Ember : { RSVP }
239+ }
240+ } ;
241+ vm . createContext ( sandbox ) ;
242+ const code = amdLoaderCode + emberFetchCode + testCode ;
243+ vm . runInContext ( code , sandbox ) ;
244+ expect ( sandbox . __result ) . to . equal ( preferNative ) ;
245+ } ) ) ;
246+
247+ if ( preferNative === true ) {
248+ it ( 'Has fetch poly fill even if fetch is not there' , co . wrap ( function * ( ) {
249+ yield output . build ( ) ;
250+ let emberFetchCode = output . read ( ) [ 'ember-fetch.js' ] ;
251+ const amdLoaderCode = fs . readFileSync ( require . resolve ( 'loader.js' ) ) ;
252+ const sandbox = {
253+ console,
254+ window : {
255+ __result : false ,
256+ // no fetch here
257+ // fetch: function() {},
258+ Ember : { RSVP }
259+ }
260+ } ;
261+ vm . createContext ( sandbox ) ;
262+ const testCodeForNonFetch = `
263+ define('fetch-test', ['fetch'], function(_fetch) {
264+ if (_fetch.default.polyfill) {
265+ window.__result = true
266+ }
267+ });
268+ require('fetch-test');
269+ ` ;
270+ const code = amdLoaderCode + emberFetchCode + testCodeForNonFetch ;
271+ vm . runInContext ( code , sandbox ) ;
272+ expect ( sandbox . window . __result ) . to . equal ( true ) ;
273+ } ) )
274+ }
275+ } ) ;
98276} ) ;
0 commit comments