summaryrefslogtreecommitdiff
path: root/spec/frontend/__helpers__/set_window_location_helper.js
blob: 573a089f11132d0d75661761c06e4f3986efecfa (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
/**
 * setWindowLocation allows for setting `window.location` within Jest.
 *
 * The jsdom environment at the time of writing does not support changing the
 * current location (see
 * https://github.com/jsdom/jsdom/blob/16.4.0/lib/jsdom/living/window/navigation.js#L76),
 * hence this helper.
 *
 * This helper mutates the current `window.location` very similarly to how
 * a direct assignment to `window.location.href` would in a browser (but
 * without the navigation/reload behaviour). For instance:
 *
 * - Set the full href by passing an absolute URL, e.g.:
 *
 *     setWindowLocation('https://gdk.test');
 *     // window.location.href is now 'https://gdk.test'
 *
 * - Set the path, search and/or hash components by passing a relative URL:
 *
 *     setWindowLocation('/foo/bar');
 *     // window.location.href is now 'http://test.host/foo/bar'
 *
 *     setWindowLocation('?foo=bar');
 *     // window.location.href is now 'http://test.host/?foo=bar'
 *
 *     setWindowLocation('#foo');
 *     // window.location.href is now 'http://test.host/#foo'
 *
 *     setWindowLocation('/a/b/foo.html?bar=1#qux');
 *     // window.location.href is now 'http://test.host/a/b/foo.html?bar=1#qux
 *
 * Both approaches also automatically update the rest of the properties on
 * `window.locaton`. For instance:
 *
 *     setWindowLocation('http://test.host/a/b/foo.html?bar=1#qux');
 *     // window.location.origin is now 'http://test.host'
 *     // window.location.pathname is now '/a/b/foo.html'
 *     // window.location.search is now '?bar=1'
 *     // window.location.searchParams is now { bar: 1 }
 *     // window.location.hash is now '#qux'
 *
 * @param {string} url A string representing an absolute or relative URL.
 * @returns {undefined}
 */
export default function setWindowLocation(url) {
  if (typeof url !== 'string') {
    throw new TypeError(`Expected string; got ${url} (${typeof url})`);
  }

  const newUrl = new URL(url, window.location.href);

  global.jsdom.reconfigure({ url: newUrl.href });
}