Ben Rohlfs | 1da030b | 2023-01-31 13:09:53 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2023 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | import '../../../test/common-test-setup'; |
| 7 | import {html, assert, fixture, waitUntil} from '@open-wc/testing'; |
| 8 | import './gr-router'; |
| 9 | import {Page, PageContext} from './gr-page'; |
| 10 | |
| 11 | suite('gr-page tests', () => { |
| 12 | let page: Page; |
| 13 | |
| 14 | setup(() => { |
| 15 | page = new Page(); |
Ben Rohlfs | dc22ff4 | 2023-02-06 10:21:28 +0100 | [diff] [blame] | 16 | page.start({dispatch: false, base: ''}); |
Ben Rohlfs | 1da030b | 2023-01-31 13:09:53 +0100 | [diff] [blame] | 17 | }); |
| 18 | |
| 19 | teardown(() => { |
| 20 | page.stop(); |
| 21 | }); |
| 22 | |
Ben Rohlfs | 90b2b7d | 2024-03-13 08:42:32 +0100 | [diff] [blame^] | 23 | suite('click handler', () => { |
| 24 | const clickListener = (e: Event) => e.preventDefault(); |
| 25 | let spy: sinon.SinonSpy; |
| 26 | let link: HTMLAnchorElement; |
| 27 | |
| 28 | setup(async () => { |
| 29 | spy = sinon.spy(); |
| 30 | link = await fixture<HTMLAnchorElement>(html`<a href="/settings"></a>`); |
| 31 | |
| 32 | document.addEventListener('click', clickListener); |
| 33 | }); |
| 34 | |
| 35 | teardown(() => { |
| 36 | document.removeEventListener('click', clickListener); |
| 37 | }); |
| 38 | |
| 39 | test('click handled by specific route', async () => { |
| 40 | page.registerRoute(/\/settings/, spy); |
| 41 | link.href = '/settings'; |
| 42 | link.click(); |
| 43 | assert.isTrue(spy.calledOnce); |
| 44 | }); |
| 45 | |
| 46 | test('click handled by default route', async () => { |
| 47 | page.registerRoute(/.*/, spy); |
| 48 | link.href = '/something'; |
| 49 | link.click(); |
| 50 | assert.isTrue(spy.called); |
| 51 | }); |
| 52 | |
| 53 | test('click not handled for /plugins/... links', async () => { |
| 54 | page.registerRoute(/.*/, spy); |
| 55 | link.href = '/plugins/gitiles'; |
| 56 | link.click(); |
| 57 | assert.isFalse(spy.called); |
| 58 | }); |
| 59 | |
| 60 | test('click not handled for /login/... links', async () => { |
| 61 | page.registerRoute(/.*/, spy); |
| 62 | link.href = '/login'; |
| 63 | link.click(); |
| 64 | assert.isFalse(spy.called); |
| 65 | }); |
Ben Rohlfs | 1da030b | 2023-01-31 13:09:53 +0100 | [diff] [blame] | 66 | }); |
| 67 | |
| 68 | test('register route and exit', () => { |
| 69 | const handleA = sinon.spy(); |
| 70 | const handleAExit = sinon.stub(); |
| 71 | page.registerRoute(/\/A/, handleA); |
| 72 | page.registerExitRoute(/\/A/, handleAExit); |
| 73 | |
| 74 | page.show('/A'); |
| 75 | assert.equal(handleA.callCount, 1); |
| 76 | assert.equal(handleAExit.callCount, 0); |
| 77 | |
| 78 | page.show('/B'); |
| 79 | assert.equal(handleA.callCount, 1); |
| 80 | assert.equal(handleAExit.callCount, 1); |
| 81 | }); |
| 82 | |
| 83 | test('register, show, replace', () => { |
| 84 | const handleA = sinon.spy(); |
Ben Rohlfs | dc22ff4 | 2023-02-06 10:21:28 +0100 | [diff] [blame] | 85 | const handleB = sinon.spy(); |
Ben Rohlfs | 1da030b | 2023-01-31 13:09:53 +0100 | [diff] [blame] | 86 | page.registerRoute(/\/A/, handleA); |
| 87 | page.registerRoute(/\/B/, handleB); |
| 88 | |
| 89 | page.show('/A'); |
| 90 | assert.equal(handleA.callCount, 1); |
| 91 | assert.equal(handleB.callCount, 0); |
| 92 | |
| 93 | page.show('/B'); |
| 94 | assert.equal(handleA.callCount, 1); |
| 95 | assert.equal(handleB.callCount, 1); |
| 96 | |
| 97 | page.replace('/A'); |
| 98 | assert.equal(handleA.callCount, 2); |
| 99 | assert.equal(handleB.callCount, 1); |
| 100 | |
| 101 | page.replace('/B'); |
| 102 | assert.equal(handleA.callCount, 2); |
| 103 | assert.equal(handleB.callCount, 2); |
| 104 | }); |
| 105 | |
Ben Rohlfs | dc22ff4 | 2023-02-06 10:21:28 +0100 | [diff] [blame] | 106 | test('popstate browser back', async () => { |
| 107 | const handleA = sinon.spy(); |
| 108 | const handleB = sinon.spy(); |
| 109 | page.registerRoute(/\/A/, handleA); |
| 110 | page.registerRoute(/\/B/, handleB); |
| 111 | |
| 112 | page.show('/A'); |
| 113 | assert.equal(handleA.callCount, 1); |
| 114 | assert.equal(handleB.callCount, 0); |
| 115 | |
| 116 | page.show('/B'); |
| 117 | assert.equal(handleA.callCount, 1); |
| 118 | assert.equal(handleB.callCount, 1); |
| 119 | |
| 120 | window.history.back(); |
| 121 | await waitUntil(() => window.location.href.includes('/A')); |
| 122 | assert.equal(handleA.callCount, 2); |
| 123 | assert.equal(handleB.callCount, 1); |
| 124 | }); |
| 125 | |
Ben Rohlfs | 1da030b | 2023-01-31 13:09:53 +0100 | [diff] [blame] | 126 | test('register pattern, check context', async () => { |
| 127 | let context: PageContext; |
| 128 | const handler = (ctx: PageContext) => (context = ctx); |
| 129 | page.registerRoute(/\/asdf\/(.*)\/qwer\/(.*)\//, handler); |
| 130 | page.stop(); |
Ben Rohlfs | dc22ff4 | 2023-02-06 10:21:28 +0100 | [diff] [blame] | 131 | page.start({dispatch: false, base: '/base'}); |
Ben Rohlfs | 1da030b | 2023-01-31 13:09:53 +0100 | [diff] [blame] | 132 | |
| 133 | page.show('/base/asdf/1234/qwer/abcd/'); |
| 134 | |
| 135 | await waitUntil(() => !!context); |
| 136 | assert.equal(context!.canonicalPath, '/base/asdf/1234/qwer/abcd/'); |
| 137 | assert.equal(context!.path, '/asdf/1234/qwer/abcd/'); |
| 138 | assert.equal(context!.querystring, ''); |
| 139 | assert.equal(context!.hash, ''); |
| 140 | assert.equal(context!.params[0], '1234'); |
| 141 | assert.equal(context!.params[1], 'abcd'); |
| 142 | |
| 143 | page.show('/asdf//qwer////?a=b#go'); |
| 144 | |
| 145 | await waitUntil(() => !!context); |
| 146 | assert.equal(context!.canonicalPath, '/base/asdf//qwer////?a=b#go'); |
| 147 | assert.equal(context!.path, '/asdf//qwer////?a=b'); |
| 148 | assert.equal(context!.querystring, 'a=b'); |
| 149 | assert.equal(context!.hash, 'go'); |
| 150 | assert.equal(context!.params[0], ''); |
| 151 | assert.equal(context!.params[1], '//'); |
| 152 | }); |
| 153 | }); |