test_suggestion-picker.js (3572B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 /** 7 * Test the suggestion-picker helper methods. 8 */ 9 10 const { 11 findMostRelevantIndex, 12 findMostRelevantCssPropertyIndex, 13 } = require("resource://devtools/client/shared/suggestion-picker.js"); 14 15 /** 16 * Run all tests defined below. 17 */ 18 function run_test() { 19 ensureMostRelevantIndexProvidedByHelperFunction(); 20 ensureMostRelevantIndexProvidedByClassMethod(); 21 ensureErrorThrownWithInvalidArguments(); 22 } 23 24 /** 25 * Generic test data. 26 */ 27 const TEST_DATA = [ 28 { 29 // Match in sortedItems array. 30 items: ["chrome", "edge", "firefox"], 31 sortedItems: ["firefox", "chrome", "edge"], 32 expectedIndex: 2, 33 }, 34 { 35 // No match in sortedItems array. 36 items: ["apple", "oranges", "banana"], 37 sortedItems: ["kiwi", "pear", "peach"], 38 expectedIndex: 0, 39 }, 40 { 41 // Empty items array. 42 items: [], 43 sortedItems: ["empty", "arrays", "can't", "have", "relevant", "indexes"], 44 expectedIndex: -1, 45 }, 46 ]; 47 48 function ensureMostRelevantIndexProvidedByHelperFunction() { 49 info("Running ensureMostRelevantIndexProvidedByHelperFunction()"); 50 51 for (const testData of TEST_DATA) { 52 const { items, sortedItems, expectedIndex } = testData; 53 const mostRelevantIndex = findMostRelevantIndex(items, sortedItems); 54 strictEqual(mostRelevantIndex, expectedIndex); 55 } 56 } 57 58 /** 59 * CSS properties test data. 60 */ 61 const CSS_TEST_DATA = [ 62 { 63 items: [ 64 "backface-visibility", 65 "background", 66 "background-attachment", 67 "background-blend-mode", 68 "background-clip", 69 "background-color", 70 "background-image", 71 "background-origin", 72 "background-position", 73 "background-repeat", 74 ], 75 expectedIndex: 1, 76 }, 77 { 78 items: [ 79 "caption-side", 80 "clear", 81 "clip", 82 "clip-path", 83 "clip-rule", 84 "color", 85 "color-interpolation", 86 "color-interpolation-filters", 87 "content", 88 "counter-increment", 89 ], 90 expectedIndex: 5, 91 }, 92 { 93 items: ["direction", "display", "dominant-baseline"], 94 expectedIndex: 1, 95 }, 96 { 97 items: [ 98 "object-fit", 99 "object-position", 100 "offset-block-end", 101 "offset-block-start", 102 "offset-inline-end", 103 "offset-inline-start", 104 "opacity", 105 "order", 106 "orphans", 107 "outline", 108 ], 109 expectedIndex: 6, 110 }, 111 { 112 items: [ 113 "white-space", 114 "widows", 115 "width", 116 "will-change", 117 "word-break", 118 "word-spacing", 119 "word-wrap", 120 "writing-mode", 121 ], 122 expectedIndex: 2, 123 }, 124 ]; 125 126 function ensureMostRelevantIndexProvidedByClassMethod() { 127 info("Running ensureMostRelevantIndexProvidedByClassMethod()"); 128 129 for (const testData of CSS_TEST_DATA) { 130 const { items, expectedIndex } = testData; 131 const mostRelevantIndex = findMostRelevantCssPropertyIndex(items); 132 strictEqual(mostRelevantIndex, expectedIndex); 133 } 134 } 135 136 function ensureErrorThrownWithInvalidArguments() { 137 info("Running ensureErrorThrownWithInvalidTypeArgument()"); 138 139 const expectedError = /Please provide valid items and sortedItems arrays\./; 140 // No arguments passed. 141 Assert.throws(() => findMostRelevantIndex(), expectedError); 142 // Invalid arguments passed. 143 Assert.throws(() => findMostRelevantIndex([]), expectedError); 144 Assert.throws(() => findMostRelevantIndex(null, []), expectedError); 145 Assert.throws(() => findMostRelevantIndex([], "string"), expectedError); 146 Assert.throws(() => findMostRelevantIndex("string", []), expectedError); 147 }