test_cache_add.js (2243B)
1 /* global context testDone:true */ 2 3 var singleUrl = "./test_cache_add.js"; 4 var urlList = ["./empty.html", "./frame.html", "./test_cache.js"]; 5 var cache; 6 var name = "adder" + context; 7 caches 8 .open(name) 9 .then(function (openCache) { 10 cache = openCache; 11 return cache.add("ftp://example.com/invalid" + context); 12 }) 13 .catch(function (err) { 14 is( 15 err.name, 16 "TypeError", 17 "add() should throw TypeError for invalid scheme" 18 ); 19 return cache.addAll([ 20 "https://example.com/valid" + context, 21 "ftp://example.com/invalid" + context, 22 ]); 23 }) 24 .catch(function (err) { 25 is( 26 err.name, 27 "TypeError", 28 "addAll() should throw TypeError for invalid scheme" 29 ); 30 var promiseList = urlList.map(function (url) { 31 return cache.match(url); 32 }); 33 promiseList.push(cache.match(singleUrl)); 34 return Promise.all(promiseList); 35 }) 36 .then(function (resultList) { 37 is(urlList.length + 1, resultList.length, "Expected number of results"); 38 resultList.every(function (result) { 39 is(undefined, result, "URLs should not already be in the cache"); 40 }); 41 return cache.add(singleUrl); 42 }) 43 .then(function (result) { 44 is(undefined, result, "Successful add() should resolve undefined"); 45 return cache.addAll(urlList); 46 }) 47 .then(function (result) { 48 is(undefined, result, "Successful addAll() should resolve undefined"); 49 var promiseList = urlList.map(function (url) { 50 return cache.match(url); 51 }); 52 promiseList.push(cache.match(singleUrl)); 53 return Promise.all(promiseList); 54 }) 55 .then(function (resultList) { 56 is(urlList.length + 1, resultList.length, "Expected number of results"); 57 resultList.every(function (result) { 58 ok(!!result, "Responses should now be in cache for each URL."); 59 }); 60 return cache.matchAll(); 61 }) 62 .then(function (resultList) { 63 is(urlList.length + 1, resultList.length, "Expected number of results"); 64 resultList.every(function (result) { 65 ok(!!result, "Responses should now be in cache for each URL."); 66 }); 67 return caches.delete(name); 68 }) 69 .then(function () { 70 testDone(); 71 }) 72 .catch(function (err) { 73 ok(false, "Caught error: " + err); 74 testDone(); 75 });