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 | |
| 23 | test('click handler', async () => { |
| 24 | const spy = sinon.spy(); |
| 25 | page.registerRoute(/\/settings/, spy); |
| 26 | const link = await fixture<HTMLAnchorElement>( |
| 27 | html`<a href="/settings"></a>` |
| 28 | ); |
| 29 | link.click(); |
| 30 | assert.isTrue(spy.calledOnce); |
| 31 | }); |
| 32 | |
| 33 | test('register route and exit', () => { |
| 34 | const handleA = sinon.spy(); |
| 35 | const handleAExit = sinon.stub(); |
| 36 | page.registerRoute(/\/A/, handleA); |
| 37 | page.registerExitRoute(/\/A/, handleAExit); |
| 38 | |
| 39 | page.show('/A'); |
| 40 | assert.equal(handleA.callCount, 1); |
| 41 | assert.equal(handleAExit.callCount, 0); |
| 42 | |
| 43 | page.show('/B'); |
| 44 | assert.equal(handleA.callCount, 1); |
| 45 | assert.equal(handleAExit.callCount, 1); |
| 46 | }); |
| 47 | |
| 48 | test('register, show, replace', () => { |
| 49 | const handleA = sinon.spy(); |
Ben Rohlfs | dc22ff4 | 2023-02-06 10:21:28 +0100 | [diff] [blame] | 50 | const handleB = sinon.spy(); |
Ben Rohlfs | 1da030b | 2023-01-31 13:09:53 +0100 | [diff] [blame] | 51 | page.registerRoute(/\/A/, handleA); |
| 52 | page.registerRoute(/\/B/, handleB); |
| 53 | |
| 54 | page.show('/A'); |
| 55 | assert.equal(handleA.callCount, 1); |
| 56 | assert.equal(handleB.callCount, 0); |
| 57 | |
| 58 | page.show('/B'); |
| 59 | assert.equal(handleA.callCount, 1); |
| 60 | assert.equal(handleB.callCount, 1); |
| 61 | |
| 62 | page.replace('/A'); |
| 63 | assert.equal(handleA.callCount, 2); |
| 64 | assert.equal(handleB.callCount, 1); |
| 65 | |
| 66 | page.replace('/B'); |
| 67 | assert.equal(handleA.callCount, 2); |
| 68 | assert.equal(handleB.callCount, 2); |
| 69 | }); |
| 70 | |
Ben Rohlfs | dc22ff4 | 2023-02-06 10:21:28 +0100 | [diff] [blame] | 71 | test('popstate browser back', async () => { |
| 72 | const handleA = sinon.spy(); |
| 73 | const handleB = sinon.spy(); |
| 74 | page.registerRoute(/\/A/, handleA); |
| 75 | page.registerRoute(/\/B/, handleB); |
| 76 | |
| 77 | page.show('/A'); |
| 78 | assert.equal(handleA.callCount, 1); |
| 79 | assert.equal(handleB.callCount, 0); |
| 80 | |
| 81 | page.show('/B'); |
| 82 | assert.equal(handleA.callCount, 1); |
| 83 | assert.equal(handleB.callCount, 1); |
| 84 | |
| 85 | window.history.back(); |
| 86 | await waitUntil(() => window.location.href.includes('/A')); |
| 87 | assert.equal(handleA.callCount, 2); |
| 88 | assert.equal(handleB.callCount, 1); |
| 89 | }); |
| 90 | |
Ben Rohlfs | 1da030b | 2023-01-31 13:09:53 +0100 | [diff] [blame] | 91 | test('register pattern, check context', async () => { |
| 92 | let context: PageContext; |
| 93 | const handler = (ctx: PageContext) => (context = ctx); |
| 94 | page.registerRoute(/\/asdf\/(.*)\/qwer\/(.*)\//, handler); |
| 95 | page.stop(); |
Ben Rohlfs | dc22ff4 | 2023-02-06 10:21:28 +0100 | [diff] [blame] | 96 | page.start({dispatch: false, base: '/base'}); |
Ben Rohlfs | 1da030b | 2023-01-31 13:09:53 +0100 | [diff] [blame] | 97 | |
| 98 | page.show('/base/asdf/1234/qwer/abcd/'); |
| 99 | |
| 100 | await waitUntil(() => !!context); |
| 101 | assert.equal(context!.canonicalPath, '/base/asdf/1234/qwer/abcd/'); |
| 102 | assert.equal(context!.path, '/asdf/1234/qwer/abcd/'); |
| 103 | assert.equal(context!.querystring, ''); |
| 104 | assert.equal(context!.hash, ''); |
| 105 | assert.equal(context!.params[0], '1234'); |
| 106 | assert.equal(context!.params[1], 'abcd'); |
| 107 | |
| 108 | page.show('/asdf//qwer////?a=b#go'); |
| 109 | |
| 110 | await waitUntil(() => !!context); |
| 111 | assert.equal(context!.canonicalPath, '/base/asdf//qwer////?a=b#go'); |
| 112 | assert.equal(context!.path, '/asdf//qwer////?a=b'); |
| 113 | assert.equal(context!.querystring, 'a=b'); |
| 114 | assert.equal(context!.hash, 'go'); |
| 115 | assert.equal(context!.params[0], ''); |
| 116 | assert.equal(context!.params[1], '//'); |
| 117 | }); |
| 118 | }); |