@@ -19,9 +19,14 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
1919 it ( `should throw an error if templating fails` , function ( ) {
2020 const manifestEntries = [ 'ignored' ] ;
2121
22+ // Mock the Eta class and its renderString method to throw an error
2223 const { populateSWTemplate} = proxyquire ( MODULE_PATH , {
23- 'lodash/template' : ( ) => {
24- throw new Error ( ) ;
24+ 'eta' : {
25+ Eta : class {
26+ renderString ( ) {
27+ throw new Error ( ) ;
28+ }
29+ } ,
2530 } ,
2631 } ) ;
2732
@@ -35,7 +40,11 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
3540
3641 it ( `should throw an error if both manifestEntries and runtimeCaching are empty` , function ( ) {
3742 const { populateSWTemplate} = proxyquire ( MODULE_PATH , {
38- 'lodash/template' : ( ) => { } ,
43+ 'eta' : {
44+ Eta : class {
45+ renderString ( ) { }
46+ } ,
47+ } ,
3948 } ) ;
4049
4150 try {
@@ -54,10 +63,16 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
5463 const precacheOptionsString = '{}' ;
5564 const manifestEntries = [ 'ignored' ] ;
5665
57- const innerStub = sinon . stub ( ) . returns ( '' ) ;
58- const outerStub = sinon . stub ( ) . returns ( innerStub ) ;
66+ // Create a single stub to simulate renderString
67+ const renderStringStub = sinon . stub ( ) . returns ( '' ) ;
5968 const { populateSWTemplate} = proxyquire ( MODULE_PATH , {
60- 'lodash/template' : outerStub ,
69+ 'eta' : {
70+ Eta : class {
71+ constructor ( ) {
72+ this . renderString = renderStringStub ;
73+ }
74+ } ,
75+ } ,
6176 './runtime-caching-converter' : {
6277 runtimeCachingConverter : ( ) => runtimeCachingPlaceholder ,
6378 } ,
@@ -66,30 +81,30 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
6681
6782 populateSWTemplate ( { manifestEntries} ) ;
6883
69- expect ( outerStub . alwaysCalledWith ( swTemplate ) ) . to . be . true ;
70-
71- // Doing a strict comparison with functions isn't easy.
72- expect ( innerStub . args [ 0 ] [ 0 ] . use ) . to . be . a ( 'function' ) ;
73- delete innerStub . args [ 0 ] [ 0 ] . use ;
74-
75- expect ( innerStub . args [ 0 ] ) . to . eql ( [
76- {
77- manifestEntries ,
78- cacheId : undefined ,
79- cleanupOutdatedCaches : undefined ,
80- clientsClaim : undefined ,
81- disableDevLogs : undefined ,
82- importScripts : undefined ,
83- navigateFallback : undefined ,
84- navigateFallbackDenylist : undefined ,
85- navigateFallbackAllowlist : undefined ,
86- navigationPreload : undefined ,
87- offlineAnalyticsConfigString : undefined ,
88- precacheOptionsString ,
89- runtimeCaching : runtimeCachingPlaceholder ,
90- skipWaiting : undefined ,
91- } ,
92- ] ) ;
84+ // Eta receives the template as the first argument: args[0][0]
85+ expect ( renderStringStub . args [ 0 ] [ 0 ] ) . to . equal ( swTemplate ) ;
86+
87+ // The data is passed as the second argument: args[0][1]
88+ expect ( renderStringStub . args [ 0 ] [ 1 ] . use ) . to . be . a ( 'function' ) ;
89+ delete renderStringStub . args [ 0 ] [ 1 ] . use ;
90+
91+ // Compare the data object directly
92+ expect ( renderStringStub . args [ 0 ] [ 1 ] ) . to . eql ( {
93+ manifestEntries ,
94+ cacheId : undefined ,
95+ cleanupOutdatedCaches : undefined ,
96+ clientsClaim : undefined ,
97+ disableDevLogs : undefined ,
98+ importScripts : undefined ,
99+ navigateFallback : undefined ,
100+ navigateFallbackDenylist : undefined ,
101+ navigateFallbackAllowlist : undefined ,
102+ navigationPreload : undefined ,
103+ offlineAnalyticsConfigString : undefined ,
104+ precacheOptionsString ,
105+ runtimeCaching : runtimeCachingPlaceholder ,
106+ skipWaiting : undefined ,
107+ } ) ;
93108 } ) ;
94109
95110 it ( `should pass the expected options to the template` , function ( ) {
@@ -115,14 +130,15 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
115130 const precacheOptionsString =
116131 '{\n "directoryIndex": "index.html",\n "ignoreURLParametersMatching": [/a/, /b/]\n}' ;
117132
118- // There are two stages in templating: creating the active template function
119- // from an initial string, and passing variables to that template function
120- // to get back a final, populated template string.
121- // We need to stub out both of those steps to test the full flow.
122- const templatePopulationStub = sinon . stub ( ) . returns ( '' ) ;
123- const templateCreationStub = sinon . stub ( ) . returns ( templatePopulationStub ) ;
133+ const renderStringStub = sinon . stub ( ) . returns ( '' ) ;
124134 const { populateSWTemplate} = proxyquire ( MODULE_PATH , {
125- 'lodash/template' : templateCreationStub ,
135+ 'eta' : {
136+ Eta : class {
137+ constructor ( ) {
138+ this . renderString = renderStringStub ;
139+ }
140+ } ,
141+ } ,
126142 './runtime-caching-converter' : {
127143 runtimeCachingConverter : ( ) => runtimeCachingPlaceholder ,
128144 } ,
@@ -148,30 +164,30 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
148164 skipWaiting,
149165 } ) ;
150166
151- expect ( templateCreationStub . alwaysCalledWith ( swTemplate ) ) . to . be . true ;
152-
153- // Doing a strict comparison with functions isn't easy.
154- expect ( templatePopulationStub . args [ 0 ] [ 0 ] . use ) . to . be . a ( 'function' ) ;
155- delete templatePopulationStub . args [ 0 ] [ 0 ] . use ;
156-
157- expect ( templatePopulationStub . args [ 0 ] ) . to . eql ( [
158- {
159- cacheId ,
160- cleanupOutdatedCaches ,
161- clientsClaim ,
162- disableDevLogs ,
163- importScripts ,
164- manifestEntries ,
165- navigateFallback ,
166- navigateFallbackDenylist ,
167- navigateFallbackAllowlist ,
168- navigationPreload ,
169- offlineAnalyticsConfigString ,
170- runtimeCaching : runtimeCachingPlaceholder ,
171- precacheOptionsString ,
172- skipWaiting ,
173- } ,
174- ] ) ;
167+ // Eta receives the template as the first argument: args[0][0]
168+ expect ( renderStringStub . args [ 0 ] [ 0 ] ) . to . equal ( swTemplate ) ;
169+
170+ // The data is passed as the second argument: args[0][1]
171+ expect ( renderStringStub . args [ 0 ] [ 1 ] . use ) . to . be . a ( 'function' ) ;
172+ delete renderStringStub . args [ 0 ] [ 1 ] . use ;
173+
174+ // Compare the data object directly
175+ expect ( renderStringStub . args [ 0 ] [ 1 ] ) . to . eql ( {
176+ cacheId ,
177+ cleanupOutdatedCaches ,
178+ clientsClaim ,
179+ disableDevLogs ,
180+ importScripts ,
181+ manifestEntries ,
182+ navigateFallback ,
183+ navigateFallbackDenylist ,
184+ navigateFallbackAllowlist ,
185+ navigationPreload ,
186+ offlineAnalyticsConfigString ,
187+ runtimeCaching : runtimeCachingPlaceholder ,
188+ precacheOptionsString ,
189+ skipWaiting ,
190+ } ) ;
175191 } ) ;
176192
177193 it ( `should handle a complex offlineGoogleAnalytics value when populating the template` , function ( ) {
@@ -190,10 +206,15 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
190206 const offlineAnalyticsConfigString = `{\n\tparameterOverrides: {\n\t\tcd1: 'offline'\n\t},\n\thitFilter: (params) => {\n \n params.set('cm1', params.get('qt'));\n }\n}` ;
191207 const manifestEntries = [ 'ignored' ] ;
192208
193- const innerStub = sinon . stub ( ) . returns ( '' ) ;
194- const outerStub = sinon . stub ( ) . returns ( innerStub ) ;
209+ const renderStringStub = sinon . stub ( ) . returns ( '' ) ;
195210 const { populateSWTemplate} = proxyquire ( MODULE_PATH , {
196- 'lodash/template' : outerStub ,
211+ 'eta' : {
212+ Eta : class {
213+ constructor ( ) {
214+ this . renderString = renderStringStub ;
215+ }
216+ } ,
217+ } ,
197218 './runtime-caching-converter' : {
198219 runtimeCachingConverter : ( ) => runtimeCachingPlaceholder ,
199220 } ,
@@ -202,29 +223,26 @@ describe(`[workbox-build] lib/populate-sw-template.js`, function () {
202223
203224 populateSWTemplate ( { manifestEntries, offlineGoogleAnalytics} ) ;
204225
205- expect ( outerStub . alwaysCalledWith ( swTemplate ) ) . to . be . true ;
206-
207- // Doing a strict comparison with functions isn't easy.
208- expect ( innerStub . args [ 0 ] [ 0 ] . use ) . to . be . a ( 'function' ) ;
209- delete innerStub . args [ 0 ] [ 0 ] . use ;
210-
211- expect ( innerStub . args [ 0 ] ) . to . eql ( [
212- {
213- manifestEntries,
214- cacheId : undefined ,
215- cleanupOutdatedCaches : undefined ,
216- clientsClaim : undefined ,
217- disableDevLogs : undefined ,
218- importScripts : undefined ,
219- navigateFallback : undefined ,
220- navigateFallbackDenylist : undefined ,
221- navigateFallbackAllowlist : undefined ,
222- navigationPreload : undefined ,
223- offlineAnalyticsConfigString,
224- precacheOptionsString,
225- runtimeCaching : runtimeCachingPlaceholder ,
226- skipWaiting : undefined ,
227- } ,
228- ] ) ;
226+ expect ( renderStringStub . args [ 0 ] [ 0 ] ) . to . equal ( swTemplate ) ;
227+
228+ expect ( renderStringStub . args [ 0 ] [ 1 ] . use ) . to . be . a ( 'function' ) ;
229+ delete renderStringStub . args [ 0 ] [ 1 ] . use ;
230+
231+ expect ( renderStringStub . args [ 0 ] [ 1 ] ) . to . eql ( {
232+ manifestEntries,
233+ cacheId : undefined ,
234+ cleanupOutdatedCaches : undefined ,
235+ clientsClaim : undefined ,
236+ disableDevLogs : undefined ,
237+ importScripts : undefined ,
238+ navigateFallback : undefined ,
239+ navigateFallbackDenylist : undefined ,
240+ navigateFallbackAllowlist : undefined ,
241+ navigationPreload : undefined ,
242+ offlineAnalyticsConfigString,
243+ precacheOptionsString,
244+ runtimeCaching : runtimeCachingPlaceholder ,
245+ skipWaiting : undefined ,
246+ } ) ;
229247 } ) ;
230248} ) ;
0 commit comments