summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/navigation-api/ordering-and-transition/resources/helpers.mjs
blob: 341befc10565d0759fd517258b2a6883fe6d564a (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const variants = new Set((new URLSearchParams(location.search)).keys());

export function hasVariant(name) {
  return variants.has(name);
}

export class Recorder {
  #events = [];
  #errors = [];
  #navigationAPI;
  #domExceptionConstructor;
  #location;
  #skipCurrentChange;
  #finalExpectedEvent;
  #finalExpectedEventCount;
  #currentFinalEventCount = 0;

  #readyToAssertResolve;
  #readyToAssertPromise = new Promise(resolve => { this.#readyToAssertResolve = resolve; });

  constructor({ window = self, skipCurrentChange = false, finalExpectedEvent, finalExpectedEventCount = 1 }) {
    assert_equals(typeof finalExpectedEvent, "string", "Must pass a string for finalExpectedEvent");

    this.#navigationAPI = window.navigation;
    this.#domExceptionConstructor = window.DOMException;
    this.#location = window.location;

    this.#skipCurrentChange = skipCurrentChange;
    this.#finalExpectedEvent = finalExpectedEvent;
    this.#finalExpectedEventCount = finalExpectedEventCount;
  }

  setUpNavigationAPIListeners() {
    this.#navigationAPI.addEventListener("navigate", e => {
      this.record("navigate");

      e.signal.addEventListener("abort", () => {
        this.recordWithError("AbortSignal abort", e.signal.reason);
      });
    });

    this.#navigationAPI.addEventListener("navigateerror", e => {
      this.recordWithError("navigateerror", e.error);

      this.#navigationAPI.transition?.finished.then(
        () => this.record("transition.finished fulfilled"),
        err => this.recordWithError("transition.finished rejected", err)
      );
    });

    this.#navigationAPI.addEventListener("navigatesuccess", () => {
      this.record("navigatesuccess");

      this.#navigationAPI.transition?.finished.then(
        () => this.record("transition.finished fulfilled"),
        err => this.recordWithError("transition.finished rejected", err)
      );
    });

    if (!this.#skipCurrentChange) {
      this.#navigationAPI.addEventListener("currententrychange", () => this.record("currententrychange"));
    }
  }

  setUpResultListeners(result, suffix = "") {
    result.committed.then(
      () => this.record(`committed fulfilled${suffix}`),
      err => this.recordWithError(`committed rejected${suffix}`, err)
    );

    result.finished.then(
      () => this.record(`finished fulfilled${suffix}`),
      err => this.recordWithError(`finished rejected${suffix}`, err)
    );
  }

  record(name) {
    const transitionProps = this.#navigationAPI.transition === null ? null : {
      from: this.#navigationAPI.transition.from,
      navigationType: this.#navigationAPI.transition.navigationType
    };

    this.#events.push({ name, location: this.#location.hash, transitionProps });

    if (name === this.#finalExpectedEvent && ++this.#currentFinalEventCount === this.#finalExpectedEventCount) {
      this.#readyToAssertResolve();
    }
  }

  recordWithError(name, errorObject) {
    this.record(name);
    this.#errors.push({ name, errorObject });
  }

  get readyToAssert() {
    return this.#readyToAssertPromise;
  }

  // Usage:
  //   recorder.assert([
  //     /* event name, location.hash value, navigation.transition properties */
  //     ["currententrychange", "", null],
  //     ["committed fulfilled", "#1", { from, navigationType }],
  //     ...
  //   ]);
  //
  // The array format is to avoid repitition at the call site, but I recommend
  // you document it like above.
  //
  // This will automatically also assert that any error objects recorded are
  // equal to each other. Use the other assert functions to check the actual
  // contents of the error objects.
  assert(expectedAsArray) {
    if (this.#skipCurrentChange) {
      expectedAsArray = expectedAsArray.filter(expected => expected[0] !== "currententrychange");
    }

    // Doing this up front gives nicer error messages because
    // assert_array_equals is nice.
    const recordedNames = this.#events.map(e => e.name);
    const expectedNames = expectedAsArray.map(e => e[0]);
    assert_array_equals(recordedNames, expectedNames);

    for (let i = 0; i < expectedAsArray.length; ++i) {
      const recorded = this.#events[i];
      const expected = expectedAsArray[i];

      assert_equals(
        recorded.location,
        expected[1],
        `event ${i} (${recorded.name}): location.hash value`
      );

      if (expected[2] === null) {
        assert_equals(
          recorded.transitionProps,
          null,
          `event ${i} (${recorded.name}): navigation.transition expected to be null`
        );
      } else {
        assert_not_equals(
          recorded.transitionProps,
          null,
          `event ${i} (${recorded.name}): navigation.transition expected not to be null`
        );
        assert_equals(
          recorded.transitionProps.from,
          expected[2].from,
          `event ${i} (${recorded.name}): navigation.transition.from`
        );
        assert_equals(
          recorded.transitionProps.navigationType,
          expected[2].navigationType,
          `event ${i} (${recorded.name}): navigation.transition.navigationType`
        );
      }
    }

    if (this.#errors.length > 1) {
      for (let i = 1; i < this.#errors.length; ++i) {
        assert_equals(
          this.#errors[i].errorObject,
          this.#errors[0].errorObject,
          `error objects must match: error object for ${this.#errors[i].name} did not match the one for ${this.#errors[0].name}`
        );
      }
    }
  }

  assertErrorsAreAbortErrors() {
    assert_greater_than(
      this.#errors.length,
      0,
      "No errors were recorded but assertErrorsAreAbortErrors() was called"
    );

    // Assume assert() has been called so all error objects are the same.
    const { errorObject } = this.#errors[0];
    assert_throws_dom("AbortError", this.#domExceptionConstructor, () => { throw errorObject; });
  }

  assertErrorsAre(expectedErrorObject) {
    assert_greater_than(
      this.#errors.length,
      0,
      "No errors were recorded but assertErrorsAre() was called"
    );

    // Assume assert() has been called so all error objects are the same.
    const { errorObject } = this.#errors[0];
    assert_equals(errorObject, expectedErrorObject);
  }
}