|
1 | 1 | import functionsJsonData from "./test-data.json"; |
2 | 2 | import { pingTestServer, sendTestResults } from "office-addin-test-helpers"; |
3 | | -import { closeWorkbook, sleep } from "./test-helpers"; |
| 3 | +import { addErrorResult, closeWorkbook, formatError, sleep } from "./test-helpers"; |
4 | 4 |
|
5 | 5 | /* global Office, document, Excel, run, navigator */ |
6 | 6 | const customFunctionsData = (<any>functionsJsonData).functions; |
7 | 7 | const port: number = 4201; |
8 | | -let testValues = []; |
| 8 | +let testValues: any[] = []; |
9 | 9 |
|
10 | 10 | Office.onReady(async () => { |
11 | 11 | document.getElementById("sideload-msg").style.display = "none"; |
12 | 12 | document.getElementById("app-body").style.display = "flex"; |
13 | 13 | document.getElementById("run").onclick = run; |
14 | 14 | addTestResult("UserAgent", navigator.userAgent); |
15 | 15 |
|
16 | | - const testServerResponse: object = await pingTestServer(port); |
17 | | - if (testServerResponse["status"] === 200) { |
18 | | - await runCfTests(); |
19 | | - await sendTestResults(testValues, port); |
20 | | - await closeWorkbook(); |
| 16 | + try { |
| 17 | + const testServerResponse: object = await pingTestServer(port); |
| 18 | + if (testServerResponse["status"] === 200) { |
| 19 | + await runCfTests(); |
| 20 | + await sendTestResults(testValues, port); |
| 21 | + await closeWorkbook(); |
| 22 | + } else { |
| 23 | + addErrorResult(testValues, `Ping failed: ${JSON.stringify(testServerResponse)}`); |
| 24 | + await sendTestResults(testValues, port).catch(() => {}); |
| 25 | + } |
| 26 | + } catch (err) { |
| 27 | + testValues = []; |
| 28 | + addErrorResult(testValues, `Initialization failed: ${formatError(err)}`); |
| 29 | + await sendTestResults(testValues, port).catch(() => {}); |
21 | 30 | } |
22 | 31 | }); |
23 | 32 |
|
24 | 33 | async function runCfTests(): Promise<void> { |
25 | | - // Exercise custom functions |
26 | | - await Excel.run(async (context) => { |
27 | | - for (let key in customFunctionsData) { |
28 | | - const formula: string = customFunctionsData[key].formula; |
29 | | - const range = context.workbook.getSelectedRange(); |
30 | | - range.formulas = [[formula]]; |
31 | | - await context.sync(); |
| 34 | + try { |
| 35 | + await Excel.run(async (context) => { |
| 36 | + for (let key in customFunctionsData) { |
| 37 | + const formula: string = customFunctionsData[key].formula; |
| 38 | + const range = context.workbook.getSelectedRange(); |
| 39 | + range.formulas = [[formula]]; |
| 40 | + await context.sync(); |
32 | 41 |
|
33 | | - await sleep(5000); |
| 42 | + await sleep(5000); |
34 | 43 |
|
35 | | - // Check to if this is a streaming function |
36 | | - await readCFData(key, customFunctionsData[key].streaming != undefined ? 2 : 1); |
37 | | - } |
38 | | - }); |
| 44 | + await readCFData(key, customFunctionsData[key].streaming != undefined ? 2 : 1); |
| 45 | + } |
| 46 | + }); |
| 47 | + } catch (err) { |
| 48 | + testValues = []; |
| 49 | + addErrorResult(testValues, `runCfTests failed: ${formatError(err)}`); |
| 50 | + throw err; |
| 51 | + } |
39 | 52 | } |
40 | 53 |
|
41 | 54 | export async function readCFData(cfName: string, readCount: number): Promise<void> { |
42 | 55 | await Excel.run(async (context) => { |
43 | | - // if this is a streaming function, we want to capture two values so we can |
44 | | - // validate the function is indeed streaming |
45 | 56 | for (let i = 0; i < readCount; i++) { |
46 | | - try { |
47 | | - const range = context.workbook.getSelectedRange(); |
48 | | - range.load("values"); |
49 | | - await context.sync(); |
| 57 | + const range = context.workbook.getSelectedRange(); |
| 58 | + range.load("values"); |
| 59 | + await context.sync(); |
50 | 60 |
|
51 | | - await sleep(5000); |
| 61 | + await sleep(5000); |
52 | 62 |
|
53 | | - addTestResult(cfName, range.values[0][0]); |
54 | | - Promise.resolve(); |
55 | | - } catch { |
56 | | - Promise.reject(); |
57 | | - } |
| 63 | + addTestResult(cfName, range.values[0][0]); |
58 | 64 | } |
59 | 65 | }); |
60 | 66 | } |
|
0 commit comments