@@ -10,14 +10,19 @@ const { createTempDir, createBuilder } = require('broccoli-test-helper');
1010const fixturify = require ( 'fixturify' ) ;
1111const addDependencyTracker = require ( '../lib/addDependencyTracker' ) ;
1212const templateCompiler = require ( 'ember-source/dist/ember-template-compiler.js' ) ;
13- const CANNOT_UNREGISTER_PLUGINS = ! templateCompiler . unregisterPlugin ;
1413
1514describe ( 'AST plugins' , function ( ) {
1615 const they = it ;
1716 this . timeout ( 10000 ) ;
1817
1918 let input , output , builder , tree , htmlbarsOptions ;
2019
20+ async function clearTreeCache ( tree ) {
21+ if ( tree && tree . processor . processor . _cache ) {
22+ await tree . processor . processor . _cache . clear ( ) ;
23+ }
24+ }
25+
2126 beforeEach (
2227 co . wrap ( function * ( ) {
2328 rewriterCallCount = 0 ;
@@ -33,12 +38,7 @@ describe('AST plugins', function () {
3338
3439 afterEach (
3540 co . wrap ( function * ( ) {
36- if ( tree ) {
37- tree . unregisterPlugins ( ) ;
38- if ( tree . processor . processor . _cache ) {
39- yield tree . processor . processor . _cache . clear ( ) ;
40- }
41- }
41+ yield clearTreeCache ( tree ) ;
4242
4343 if ( builder ) {
4444 builder . cleanup ( ) ;
@@ -104,9 +104,6 @@ describe('AST plugins', function () {
104104 they (
105105 'are accepted and used.' ,
106106 co . wrap ( function * ( ) {
107- if ( CANNOT_UNREGISTER_PLUGINS ) {
108- this . skip ( ) ;
109- }
110107 htmlbarsOptions . plugins = {
111108 ast : [ DivRewriter ] ,
112109 } ;
@@ -126,9 +123,6 @@ describe('AST plugins', function () {
126123 they (
127124 'will bust the hot cache if the dependency changes.' ,
128125 co . wrap ( function * ( ) {
129- if ( CANNOT_UNREGISTER_PLUGINS ) {
130- this . skip ( ) ;
131- }
132126 Object . assign ( htmlbarsOptions , {
133127 plugins : {
134128 ast : [ DivRewriter ] ,
@@ -185,65 +179,67 @@ describe('AST plugins', function () {
185179 they (
186180 'will bust the persistent cache if the template cache key changes.' ,
187181 co . wrap ( function * ( ) {
188- if ( CANNOT_UNREGISTER_PLUGINS ) {
189- this . skip ( ) ;
190- }
191182 Object . assign ( htmlbarsOptions , {
192183 plugins : {
193184 ast : [ DivRewriter ] ,
194185 } ,
195186 dependencyInvalidation : true ,
196187 } ) ;
197188
198- let firstTree = new TemplateCompiler ( input . path ( ) , htmlbarsOptions ) ;
189+ let firstTree , secondTree , thirdTree ;
199190
200191 try {
201- output = createBuilder ( firstTree ) ;
202- yield output . build ( ) ;
203-
204- let templateOutput = output . readText ( 'template.js' ) ;
205- assert . ok ( ! templateOutput . match ( / d i v / ) ) ;
206- assert . ok ( templateOutput . match ( / m y - c u s t o m - e l e m e n t / ) ) ;
207- assert . strictEqual ( rewriterCallCount , 1 ) ;
208- } finally {
209- yield output . dispose ( ) ;
210- firstTree . unregisterPlugins ( ) ;
211- }
212-
213- // The state didn't change. the output should be cached
214- // and the rewriter shouldn't be invoked.
215- let secondTree = new TemplateCompiler ( input . path ( ) , htmlbarsOptions ) ;
216- try {
217- let output = createBuilder ( secondTree ) ;
218- yield output . build ( ) ;
219- assert . deepStrictEqual ( output . changes ( ) [ 'template.js' ] , 'create' ) ;
220- // the "new" file is read from cache.
221- let templateOutput = output . readText ( 'template.js' ) ;
222- assert . ok ( ! templateOutput . match ( / d i v / ) ) ;
223- assert . ok ( templateOutput . match ( / m y - c u s t o m - e l e m e n t / ) ) ;
224- assert . strictEqual ( rewriterCallCount , 1 ) ;
225- } finally {
226- yield output . dispose ( ) ;
227- secondTree . unregisterPlugins ( ) ;
228- }
192+ firstTree = new TemplateCompiler ( input . path ( ) , htmlbarsOptions ) ;
193+
194+ try {
195+ output = createBuilder ( firstTree ) ;
196+ yield output . build ( ) ;
197+
198+ let templateOutput = output . readText ( 'template.js' ) ;
199+ assert . ok ( ! templateOutput . match ( / d i v / ) ) ;
200+ assert . ok ( templateOutput . match ( / m y - c u s t o m - e l e m e n t / ) ) ;
201+ assert . strictEqual ( rewriterCallCount , 1 ) ;
202+ } finally {
203+ yield output . dispose ( ) ;
204+ }
229205
230- // The state changes. the cache key updates and the template
231- // should be recompiled.
232- input . write ( {
233- 'template.tagname' : 'MyChangedElement' ,
234- } ) ;
206+ // The state didn't change. the output should be cached
207+ // and the rewriter shouldn't be invoked.
208+ secondTree = new TemplateCompiler ( input . path ( ) , htmlbarsOptions ) ;
209+ try {
210+ let output = createBuilder ( secondTree ) ;
211+ yield output . build ( ) ;
212+ assert . deepStrictEqual ( output . changes ( ) [ 'template.js' ] , 'create' ) ;
213+ // the "new" file is read from cache.
214+ let templateOutput = output . readText ( 'template.js' ) ;
215+ assert . ok ( ! templateOutput . match ( / d i v / ) ) ;
216+ assert . ok ( templateOutput . match ( / m y - c u s t o m - e l e m e n t / ) ) ;
217+ assert . strictEqual ( rewriterCallCount , 1 ) ;
218+ } finally {
219+ yield output . dispose ( ) ;
220+ }
235221
236- let thirdTree = new TemplateCompiler ( input . path ( ) , htmlbarsOptions ) ;
237- try {
238- let output = createBuilder ( thirdTree ) ;
239- yield output . build ( ) ;
240- let templateOutput = output . readText ( 'template.js' ) ;
241- assert . strictEqual ( rewriterCallCount , 2 ) ;
242- assert . ok ( templateOutput . match ( / m y - c h a n g e d - e l e m e n t / ) ) ;
243- assert . strictEqual ( rewriterCallCount , 2 ) ;
222+ // The state changes. the cache key updates and the template
223+ // should be recompiled.
224+ input . write ( {
225+ 'template.tagname' : 'MyChangedElement' ,
226+ } ) ;
227+
228+ thirdTree = new TemplateCompiler ( input . path ( ) , htmlbarsOptions ) ;
229+ try {
230+ let output = createBuilder ( thirdTree ) ;
231+ yield output . build ( ) ;
232+ let templateOutput = output . readText ( 'template.js' ) ;
233+ assert . strictEqual ( rewriterCallCount , 2 ) ;
234+ assert . ok ( templateOutput . match ( / m y - c h a n g e d - e l e m e n t / ) ) ;
235+ assert . strictEqual ( rewriterCallCount , 2 ) ;
236+ } finally {
237+ yield output . dispose ( ) ;
238+ }
244239 } finally {
245- yield output . dispose ( ) ;
246- thirdTree . unregisterPlugins ( ) ;
240+ clearTreeCache ( firstTree ) ;
241+ clearTreeCache ( secondTree ) ;
242+ clearTreeCache ( thirdTree ) ;
247243 }
248244 } )
249245 ) ;
0 commit comments