constructor.html (2394B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>ScrollTimeline constructor</title> 4 <link rel="help" href="https://wicg.github.io/scroll-animations/#scrolltimeline-interface"> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 8 <style> 9 .scroller { 10 height: 100px; 11 width: 100px; 12 overflow: scroll; 13 } 14 15 .content { 16 height: 500px; 17 width: 500px; 18 } 19 </style> 20 21 <div class="scroller"> 22 <div class="content"></div> 23 </div> 24 25 <script> 26 'use strict'; 27 28 function formatOffset(v) { 29 if (typeof(v) == 'object') 30 return `${v.constructor.name}(${v.toString()})`; 31 return `'${v.toString()}'`; 32 } 33 34 function assert_offsets_equal(a, b) { 35 assert_equals(formatOffset(a), formatOffset(b)); 36 } 37 38 // source 39 40 test(t => { 41 const scroller = document.querySelector('.scroller'); 42 assert_equals( 43 new ScrollTimeline({source: scroller}).source, scroller); 44 }, 'A ScrollTimeline can be created with a source'); 45 46 test(t => { 47 const div = document.createElement('div'); 48 assert_equals(new ScrollTimeline({source: div}).source, div); 49 }, 'A ScrollTimeline can be created with a non-scrolling source'); 50 51 test(t => { 52 assert_equals(new ScrollTimeline({source: null}).source, null); 53 }, 'A ScrollTimeline created with a null source should have no source'); 54 55 test(t => { 56 assert_equals(new ScrollTimeline().source, document.scrollingElement); 57 }, 'A ScrollTimeline created without a source should use the ' + 58 'document.scrollingElement'); 59 60 // axis 61 62 test(t => { 63 assert_equals(new ScrollTimeline().axis, 'block'); 64 }, 'A ScrollTimeline created with the default axis should default to ' + 65 `'block'`); 66 67 const gValidAxisValues = [ 68 'block', 69 'inline', 70 'x', 71 'y', 72 ]; 73 74 for (let axis of gValidAxisValues) { 75 test(function() { 76 const scrollTimeline = 77 new ScrollTimeline({axis: axis}); 78 assert_equals(scrollTimeline.axis, axis); 79 }, `'${axis}' is a valid axis value`); 80 } 81 82 test(t => { 83 let constructorFunc = function() { 84 new ScrollTimeline({axis: 'nonsense'}) 85 }; 86 assert_throws_js(TypeError, constructorFunc); 87 88 // 'auto' for axis was previously in the spec, but was removed. Make 89 // sure that implementations do not support it. 90 constructorFunc = function() { 91 new ScrollTimeline({axis: 'auto'}) 92 }; 93 assert_throws_js(TypeError, constructorFunc); 94 }, 'Creating a ScrollTimeline with an invalid axis value should throw'); 95 </script>