summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_jsterm_completion_bracket_cached_results.js
blob: ecca77f07147822a4535aac0f764c2ed86eb3c63 (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
126
127
128
129
130
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that code completion works properly with `[` and cached results

"use strict";

const TEST_URI = `data:text/html;charset=utf8,<!DOCTYPE html><p>test [ completion cached results.
  <script>
    window.testObject = Object.create(null, Object.getOwnPropertyDescriptors({
      bar: 0,
      dataTest: 1,
      "data-test": 2,
      'da"ta"test': 3,
      "da\`ta\`test": 4,
      "da'ta'test": 5,
      "DATA-TEST": 6,
    }));
  </script>`;

add_task(async function () {
  await pushPref("devtools.editor.autoclosebrackets", false);
  const hud = await openNewTabAndConsole(TEST_URI);
  const { jsterm } = hud;

  info("Test that the autocomplete cache works with brackets");
  const { autocompletePopup } = jsterm;

  const tests = [
    {
      description: "Test that it works if the user did not type a quote",
      initialInput: `window.testObject[dat`,
      expectedItems: [`"data-test"`, `"dataTest"`, `"DATA-TEST"`],
      expectedCompletionText: `a-test"]`,
      sequence: [
        {
          char: "a",
          expectedItems: [`"data-test"`, `"dataTest"`, `"DATA-TEST"`],
          expectedCompletionText: `-test"]`,
        },
        {
          char: "-",
          expectedItems: [`"data-test"`, `"DATA-TEST"`],
          expectedCompletionText: `test"]`,
        },
        {
          char: "t",
          expectedItems: [`"data-test"`, `"DATA-TEST"`],
          expectedCompletionText: `est"]`,
        },
        {
          char: "e",
          expectedItems: [`"data-test"`, `"DATA-TEST"`],
          expectedCompletionText: `st"]`,
        },
      ],
    },
    {
      description: "Test that it works if the user did type a quote",
      initialInput: `window.testObject['dat`,
      expectedItems: [`'data-test'`, `'dataTest'`, `'DATA-TEST'`],
      expectedCompletionText: `a-test']`,
      sequence: [
        {
          char: "a",
          expectedItems: [`'data-test'`, `'dataTest'`, `'DATA-TEST'`],
          expectedCompletionText: `-test']`,
        },
        {
          char: "-",
          expectedItems: [`'data-test'`, `'DATA-TEST'`],
          expectedCompletionText: `test']`,
        },
        {
          char: "t",
          expectedItems: [`'data-test'`, `'DATA-TEST'`],
          expectedCompletionText: `est']`,
        },
        {
          char: "e",
          expectedItems: [`'data-test'`, `'DATA-TEST'`],
          expectedCompletionText: `st']`,
        },
      ],
    },
  ];

  for (const test of tests) {
    info(test.description);

    let onPopupUpdate = jsterm.once("autocomplete-updated");
    EventUtils.sendString(test.initialInput);
    await onPopupUpdate;

    ok(
      hasExactPopupLabels(autocompletePopup, test.expectedItems),
      `popup has expected items, in expected order`
    );

    checkInputCompletionValue(
      hud,
      test.expectedCompletionText,
      `completeNode has expected value`
    );
    for (const {
      char,
      expectedItems,
      expectedCompletionText,
    } of test.sequence) {
      onPopupUpdate = jsterm.once("autocomplete-updated");
      EventUtils.sendString(char);
      await onPopupUpdate;

      ok(
        hasExactPopupLabels(autocompletePopup, expectedItems),
        `popup has expected items, in expected order`
      );
      checkInputCompletionValue(
        hud,
        expectedCompletionText,
        `completeNode has expected value`
      );
    }

    const onPopupClose = autocompletePopup.once("popup-closed");
    EventUtils.synthesizeKey("KEY_Escape");
    await onPopupClose;
    setInputValue(hud, "");
  }
});