test-mockup.js (4713B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 /** 6 * This file is for use by unit tests for isolated debugger components that do 7 * not need to interact with the redux store. When these tests need to construct 8 * debugger objects, these interfaces should be used instead of plain object 9 * literals. 10 */ 11 12 import * as asyncValue from "./async-value"; 13 14 import { getDisplayURL } from "./sources-tree/getURL"; 15 16 function makeMockSource(url = "url", id = "source", thread = "FakeThread") { 17 return { 18 id, 19 url, 20 shortName: getDisplayURL(url).filename, 21 longName: getDisplayURL(url).filename + getDisplayURL(url).search, 22 displayURL: getDisplayURL(url), 23 thread, 24 isPrettyPrinted: false, 25 isWasm: false, 26 extensionName: null, 27 isExtension: false, 28 isOriginal: id.includes("originalSource"), 29 }; 30 } 31 32 function makeMockDisplaySource( 33 url = "url", 34 id = "source", 35 thread = "FakeThread" 36 ) { 37 return makeMockSource(url, id, thread); 38 } 39 40 function makeMockSourceWithContent( 41 url, 42 id, 43 contentType = "text/javascript", 44 text = "" 45 ) { 46 const source = makeMockSource(url, id); 47 48 return { 49 ...source, 50 content: text 51 ? asyncValue.fulfilled({ 52 type: "text", 53 value: text, 54 contentType, 55 }) 56 : null, 57 }; 58 } 59 60 function makeMockSourceAndContent( 61 url, 62 id, 63 contentType = "text/javascript", 64 text = "" 65 ) { 66 const source = makeMockSource(url, id); 67 68 return { 69 ...source, 70 content: { 71 type: "text", 72 value: text, 73 contentType, 74 }, 75 }; 76 } 77 78 function makeFullfilledMockSourceContent( 79 text = "", 80 contentType = "text/javascript" 81 ) { 82 return asyncValue.fulfilled({ 83 type: "text", 84 value: text, 85 contentType, 86 }); 87 } 88 89 function makeMockWasmSource() { 90 return { 91 id: "wasm-source-id", 92 url: "url", 93 displayURL: getDisplayURL("url"), 94 thread: "FakeThread", 95 isPrettyPrinted: false, 96 isWasm: true, 97 extensionName: null, 98 isExtension: false, 99 isOriginal: false, 100 }; 101 } 102 103 function makeMockWasmSourceWithContent(text) { 104 const source = makeMockWasmSource(); 105 106 return { 107 ...source, 108 content: asyncValue.fulfilled({ 109 type: "wasm", 110 value: text, 111 }), 112 }; 113 } 114 115 function makeMockScope(actor = "scope-actor", type = "block", parent = null) { 116 return { 117 actor, 118 parent, 119 bindings: { 120 arguments: [], 121 variables: {}, 122 }, 123 object: null, 124 function: null, 125 type, 126 scopeKind: "", 127 }; 128 } 129 130 function mockScopeAddVariable(scope, name) { 131 if (!scope.bindings) { 132 throw new Error("no scope bindings"); 133 } 134 scope.bindings.variables[name] = { value: null }; 135 } 136 137 function makeMockBreakpoint(source = makeMockSource(), line = 1, column) { 138 const location = column ? { source, line, column } : { source, line }; 139 return { 140 id: "breakpoint", 141 location, 142 generatedLocation: location, 143 disabled: false, 144 text: "text", 145 originalText: "text", 146 options: {}, 147 }; 148 } 149 150 function makeWhyNormal(frameReturnValue = undefined) { 151 if (frameReturnValue) { 152 return { type: "why-normal", frameFinished: { return: frameReturnValue } }; 153 } 154 return { type: "why-normal" }; 155 } 156 157 function makeWhyThrow(frameThrowValue) { 158 return { type: "why-throw", frameFinished: { throw: frameThrowValue } }; 159 } 160 161 function makeMockExpression(value) { 162 return { 163 input: "input", 164 value, 165 from: "from", 166 updating: false, 167 }; 168 } 169 170 // Mock contexts for use in tests that do not create a redux store. 171 const mockcx = { navigateCounter: 0 }; 172 const mockthreadcx = { 173 navigateCounter: 0, 174 thread: "FakeThread", 175 pauseCounter: 0, 176 isPaused: false, 177 }; 178 179 function makeMockThread(fields) { 180 return { 181 actor: "test", 182 url: "example.com", 183 type: "worker", 184 name: "test", 185 ...fields, 186 }; 187 } 188 189 function formatTree(tree, depth = 0, str = "") { 190 const whitespace = new Array(depth * 2).join(" "); 191 192 if (tree.type === "directory") { 193 str += `${whitespace} - ${tree.name} path=${tree.path} \n`; 194 tree.contents.forEach(t => { 195 str = formatTree(t, depth + 1, str); 196 }); 197 } else { 198 str += `${whitespace} - ${tree.name} path=${tree.path} source_id=${tree.contents.id} \n`; 199 } 200 201 return str; 202 } 203 204 export { 205 makeMockDisplaySource, 206 makeMockSource, 207 makeMockSourceWithContent, 208 makeMockSourceAndContent, 209 makeMockWasmSource, 210 makeMockWasmSourceWithContent, 211 makeMockScope, 212 mockScopeAddVariable, 213 makeMockBreakpoint, 214 makeWhyNormal, 215 makeWhyThrow, 216 makeMockExpression, 217 mockcx, 218 mockthreadcx, 219 makeMockThread, 220 makeFullfilledMockSourceContent, 221 formatTree, 222 };