1
+ <!doctype html>
2
+ <!--
3
+ @license
4
+ Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
5
+ This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6
+ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7
+ The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8
+ Code distributed by Google as part of the polymer project is also
9
+ subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10
+ -->
11
+ < html >
12
+
13
+ < head >
14
+ <!--
15
+ This test should be run only for spec-compliant browsers that require no polyfills.
16
+ When Loader is used async, we expect `WebComponentsReady` event to fire after all
17
+ waitFor() callbacks are resolved.
18
+ -->
19
+ < title > Test loader async/no-polyfills cases</ title >
20
+ < meta charset ="UTF-8 ">
21
+ < script src ="../webcomponents-loader.js " defer > </ script >
22
+ < script src ="./wct-config.js "> </ script >
23
+ < script >
24
+ WCT . waitForFrameworks = false ;
25
+ window . _wctCallback = function ( ) { } ;
26
+ </ script >
27
+ < script src ="../node_modules/wct-browser-legacy/browser.js "> </ script >
28
+ < script >
29
+ // Define mock load function that takes `duration` to resolve.
30
+ window . mockLoadCount = 0 ;
31
+ window . mockLoad = function ( duration ) {
32
+ return new Promise ( function ( resolve ) {
33
+ window . mockLoadCount ++ ;
34
+ setTimeout ( resolve , duration ) ;
35
+ } ) ;
36
+ } ;
37
+ </ script >
38
+ < script type ="module ">
39
+ WebComponents . waitFor ( ( ) => {
40
+ return mockLoad ( 1000 ) . then ( ( ) => {
41
+ window . mockModule1Loaded = true ;
42
+ } ) ;
43
+ } ) ;
44
+ </ script >
45
+ < script type ="module ">
46
+ WebComponents . waitFor ( ( ) => {
47
+ return mockLoad ( 500 ) . then ( ( ) => {
48
+ window . mockModule2Loaded = true ;
49
+ } )
50
+ } ) ;
51
+ </ script >
52
+ </ head >
53
+
54
+ < body >
55
+ < script >
56
+ suite ( 'Loader async/no-polyfills tests' , function ( ) {
57
+ test ( 'Modules are loaded after WCR fires' , function ( done ) {
58
+ var tested = false ;
59
+ var assertModulesLoaded = function ( ) {
60
+ tested = true ;
61
+ assert . isOk ( window . mockModule1Loaded ) ;
62
+ assert . isOk ( window . mockModule2Loaded ) ;
63
+ done ( ) ;
64
+ } ;
65
+ window . addEventListener ( 'WebComponentsReady' , assertModulesLoaded ) ;
66
+ // An issue with WCT on Safari 10 causes `document.readyState` to be wrongly resolved as `complete`.
67
+ // This causes WCR to fire immediately before the event listener is defined, causing a timeout.
68
+ // As a workaround, we run a timer to check if WCR has been missed, then run the assertions.
69
+ setTimeout ( function ( ) {
70
+ if ( ! tested && WebComponents . ready ) {
71
+ assertModulesLoaded ( ) ;
72
+ }
73
+ } , 4000 ) ;
74
+ } ) ;
75
+ test ( 'waitFor() callbacks are run exactly once' , function ( ) {
76
+ // We expect mockLoadCount to be 2, since we defined two `waitFor` cbs.
77
+ assert . equal ( window . mockLoadCount , 2 ) ;
78
+ } ) ;
79
+ } ) ;
80
+ </ script >
81
+ </ body >
82
+
83
+ </ html >
0 commit comments