summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/test/FormHistoryTestUtils.sys.mjs
blob: f17b952b903070cef74224d61854e6eddb94b51a (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  FormHistory: "resource://gre/modules/FormHistory.sys.mjs",
});

/**
 * Provides a js-friendly promise-based API around FormHistory, and utils.
 *
 * Note: This is not a 100% complete implementation, it is intended for quick
 * additions and check, thus further changes may be necessary for different
 * use-cases.
 */
export var FormHistoryTestUtils = {
  /**
   * Adds values to form history.
   *
   * @param {string} fieldname The field name.
   * @param {Array} additions Array of entries describing the values to add.
   *   Each entry can either be a string, or an object with the shape
   *   { value, source}.
   * @returns {Promise} Resolved once the operation is complete.
   */
  async add(fieldname, additions = []) {
    // Additions are made one by one, so multiple identical entries are properly
    // applied.
    additions = additions.map(v => (typeof v == "string" ? { value: v } : v));
    for (let { value, source } of additions) {
      await lazy.FormHistory.update(
        Object.assign({ fieldname }, { op: "bump", value, source })
      );
    }
  },

  /**
   * Counts values from form history.
   *
   * @param {string} fieldname The field name.
   * @param {Array} filters Objects describing the search properties.
   * @returns {number} The number of entries found.
   */
  async count(fieldname, filters = {}) {
    return lazy.FormHistory.count(Object.assign({ fieldname }, filters));
  },

  /**
   * Removes values from form history.
   * If you want to remove all history, use clear() instead.
   *
   * @param {string} fieldname The field name.
   * @param {Array} removals Array of entries describing the values to add.
   *   Each entry can either be a string, or an object with the shape
   *   { value, source}. If source is specified, only the source relation will
   *   be removed, while the global form history value persists.
   * @returns {Promise} Resolved once the operation is complete.
   */
  remove(fieldname, removals) {
    let changes = removals.map(v => {
      let criteria = typeof v == "string" ? { value: v } : v;
      return Object.assign({ fieldname, op: "remove" }, criteria);
    });
    return lazy.FormHistory.update(changes);
  },

  /**
   * Removes all values from form history.
   * If you want to remove individual values, use remove() instead.
   *
   * @param {string} fieldname The field name whose history should be cleared.
   *   Can be omitted to clear all form history.
   * @returns {Promise} Resolved once the operation is complete.
   */
  clear(fieldname) {
    let baseChange = fieldname ? { fieldname } : {};
    return lazy.FormHistory.update(Object.assign(baseChange, { op: "remove" }));
  },

  /**
   * Searches form history.
   *
   * @param {string} fieldname The field name.
   * @param {Array} filters Objects describing the search properties.
   * @returns {Promise<Array>} Resolves an array of found form history entries.
   */
  search(fieldname, filters = {}) {
    return lazy.FormHistory.search(null, Object.assign({ fieldname }, filters));
  },

  /**
   * Gets autocomplete results from form history.
   *
   * @param {string} searchString The search string.
   * @param {string} fieldname The field name.
   * @param {Array} filters Objects describing the search properties.
   * @returns {Promise<Array>} Resolves an array of found form history entries.
   */
  autocomplete(searchString, fieldname, filters = {}) {
    return lazy.FormHistory.getAutoCompleteResults(
      searchString,
      Object.assign({ fieldname }, filters)
    );
  },
};