github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/webapp/components/test/loading-state.html (about) 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="../../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script> 6 <script src="../../node_modules/wct-browser-legacy/browser.js"></script> 7 </head> 8 9 <body> 10 <dom-module id="loading-state-concrete"> 11 <script type="module"> 12 import { PolymerElement } from '../../node_modules/@polymer/polymer/polymer-element.js'; 13 import { LoadingState } from '../loading-state.js'; 14 15 class ConcreteType extends LoadingState(PolymerElement) {} 16 17 window.customElements.define('loading-state-concrete', ConcreteType); 18 </script> 19 </dom-module> 20 21 <test-fixture id="loading-state-fixture"> 22 <template> 23 <loading-state-concrete></loading-state-concrete> 24 </template> 25 </test-fixture> 26 27 <script type="module"> 28 import '../../node_modules/@polymer/polymer/polymer-element.js'; 29 import '../loading-state.js'; 30 suite('LoadingState', () => { 31 let state; 32 33 setup(() => { 34 state = fixture('loading-state-fixture'); 35 }); 36 37 suite('LoadingState.prototype.*', () => { 38 suite('onLoadingComplete', () => { 39 let completeCallback; 40 41 setup(() => { 42 completeCallback = sinon.spy(); 43 state.onLoadingComplete = completeCallback; 44 }); 45 46 test('executed when count reaches zero', async() => { 47 state.loadingCount++; 48 state.loadingCount--; 49 assert.isTrue(completeCallback.calledOnce); 50 }); 51 52 test('executed when load returns', async() => { 53 await state.load(Promise.resolve()); 54 assert.isTrue(completeCallback.calledOnce); 55 }); 56 57 test('but not when already zero', () => { 58 state.loadingCount = 0; 59 assert.isTrue(completeCallback.notCalled); 60 }); 61 }); 62 63 suite('retry', () => { 64 test('should not retry', async() => { 65 let count = 0; 66 await state.retry(async() => { 67 count++; 68 // Expected: Invoke this function once. 69 // Unexpected: Invoke this function more than once. 70 throw count === 1 ? 'Expected' : 'Unexpected'; 71 }, () => false, 2, 0).then(() => { 72 // Unexpected: Promise resolved. 73 throw new Error('Expected promise to reject; it resolved.'); 74 }).catch(err => { 75 assert.equal('Expected', err); 76 }); 77 }); 78 79 test('retry n times', async() => { 80 let count = 0; 81 await state.retry(async() => { 82 count++; 83 throw count; 84 }, () => true, 2, 0).then(() => { 85 // Unexpected: Promise resolved. 86 throw new Error('Expected promise to reject; it resolved.'); 87 }).catch(err => { 88 assert.equal(2, err); 89 }); 90 }); 91 92 test('stop after success', async() => { 93 let count = 0; 94 await state.retry(async() => { 95 count++; 96 if (count === 2) { 97 return count; 98 } 99 100 throw count; 101 }, () => true, 5, 0).then(v => { 102 assert.equal(2, v); 103 }); 104 }); 105 106 test('wait prescribed timeout', async() => { 107 let count = 0; 108 window.setTimeout(() => assert.isTrue(count > 1 && count < 5), 30); 109 await state.retry(async() => { 110 count++; 111 throw count; 112 }, () => true, 5, 10).catch(() => true); 113 }); 114 }); 115 }); 116 }); 117 </script> 118 </body> 119 120 </html>