summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg_variables-view-06.js
blob: 6d923eb02bfc4442a26cd802ad27d948847ca395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test that Promises get their internal state added as psuedo properties.
 */

const TAB_URL = EXAMPLE_URL + "doc_promise.html";

var test = Task.async(function* () {
  let options = {
    source: TAB_URL,
    line: 1
  };
  const [tab,, panel] = yield initDebugger(TAB_URL, options);

  const scopes = waitForCaretAndScopes(panel, 21);
  callInTab(tab, "doPause");
  yield scopes;

  const variables = panel.panelWin.DebuggerView.Variables;
  ok(variables, "Should get the variables view.");

  const scope = [...variables][0];
  ok(scope, "Should get the current function's scope.");

  const promiseVariables = [...scope].filter(([name]) =>
    ["p", "f", "r"].indexOf(name) !== -1);

  is(promiseVariables.length, 3,
     "Should have our 3 promise variables: p, f, r");

  for (let [name, item] of promiseVariables) {
    info("Expanding variable '" + name + "'");
    let expanded = once(variables, "fetched");
    item.expand();
    yield expanded;

    let foundState = false;
    switch (name) {
      case "p":
        for (let [property, { value }] of item) {
          if (property !== "<state>") {
            isnot(property, "<value>",
                  "A pending promise shouldn't have a value");
            isnot(property, "<reason>",
                  "A pending promise shouldn't have a reason");
            continue;
          }

          foundState = true;
          is(value, "pending", "The state should be pending.");
        }
        ok(foundState, "We should have found the <state> property.");
        break;

      case "f":
        let foundValue = false;
        for (let [property, value] of item) {
          if (property === "<state>") {
            foundState = true;
            is(value.value, "fulfilled", "The state should be fulfilled.");
          } else if (property === "<value>") {
            foundValue = true;

            let expanded = once(variables, "fetched");
            value.expand();
            yield expanded;

            let expectedProps = new Map([["a", 1], ["b", 2], ["c", 3]]);
            for (let [prop, val] of value) {
              if (prop === "__proto__") {
                continue;
              }
              ok(expectedProps.has(prop), "The property should be expected.");
              is(val.value, expectedProps.get(prop), "The property value should be correct.");
              expectedProps.delete(prop);
            }
            is(Object.keys(expectedProps).length, 0,
               "Should have found all of the expected properties.");
          } else {
            isnot(property, "<reason>",
                  "A fulfilled promise shouldn't have a reason");
          }
        }
        ok(foundState, "We should have found the <state> property.");
        ok(foundValue, "We should have found the <value> property.");
        break;

      case "r":
        let foundReason = false;
        for (let [property, value] of item) {
          if (property === "<state>") {
            foundState = true;
            is(value.value, "rejected", "The state should be rejected.");
          } else if (property === "<reason>") {
            foundReason = true;

            let expanded = once(variables, "fetched");
            value.expand();
            yield expanded;

            let foundMessage = false;
            for (let [prop, val] of value) {
              if (prop !== "message") {
                continue;
              }
              foundMessage = true;
              is(val.value, "uh oh", "Should have the correct error message.");
            }
            ok(foundMessage, "Should have found the error's message");
          } else {
            isnot(property, "<value>",
                  "A rejected promise shouldn't have a value");
          }
        }
        ok(foundState, "We should have found the <state> property.");
        break;
    }
  }

  resumeDebuggerThenCloseAndFinish(panel);
});