|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {expect, fixture, html} from '@open-wc/testing'; |
| 18 | +import '../webstatus-not-found-error-page.js'; |
| 19 | +import {WebstatusNotFoundErrorPage} from '../webstatus-notfound-error-page.js'; |
| 20 | + |
| 21 | +const GITHUB_REPO_ISSUE_LINK = 'https://github.com/example/repo/issues'; |
| 22 | + |
| 23 | +describe('webstatus-not-found-error-page', () => { |
| 24 | + it('renders the correct error message when featureId is missing', async () => { |
| 25 | + const component = await fixture<WebstatusNotFoundErrorPage>( |
| 26 | + html`<webstatus-not-found-error-page></webstatus-not-found-error-page>`, |
| 27 | + ); |
| 28 | + |
| 29 | + expect( |
| 30 | + component.shadowRoot |
| 31 | + ?.querySelector('#error-status-code') |
| 32 | + ?.textContent?.trim(), |
| 33 | + ).to.equal('404'); |
| 34 | + |
| 35 | + expect( |
| 36 | + component.shadowRoot |
| 37 | + ?.querySelector('#error-headline') |
| 38 | + ?.textContent?.trim(), |
| 39 | + ).to.equal('Page not found'); |
| 40 | + |
| 41 | + expect( |
| 42 | + component.shadowRoot |
| 43 | + ?.querySelector('#error-detailed-message .error-message') |
| 44 | + ?.textContent?.trim(), |
| 45 | + ).to.equal("We couldn't find the page you're looking for."); |
| 46 | + }); |
| 47 | + |
| 48 | + it('renders the correct error message when featureId is provided', async () => { |
| 49 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 50 | + <webstatus-not-found-error-page |
| 51 | + .location=${{search: '?q=test-feature'}} |
| 52 | + ></webstatus-not-found-error-page> |
| 53 | + `); |
| 54 | + |
| 55 | + expect( |
| 56 | + component.shadowRoot?.querySelector('#error-detailed-message') |
| 57 | + ?.textContent, |
| 58 | + ).to.include('We could not find Feature ID: test-feature'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('displays "Loading similar features..." when the API request is pending', async () => { |
| 62 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 63 | + <webstatus-not-found-error-page |
| 64 | + .location=${{search: '?q=test-feature'}} |
| 65 | + ></webstatus-not-found-error-page> |
| 66 | + `); |
| 67 | + |
| 68 | + component._loadingSimilarResults = {status: 'pending'} as any; |
| 69 | + await component.updateComplete; |
| 70 | + |
| 71 | + const loadingMessage = |
| 72 | + component.shadowRoot?.querySelector('.loading-message'); |
| 73 | + expect(loadingMessage).to.exist; |
| 74 | + expect(loadingMessage?.textContent?.trim()).to.equal( |
| 75 | + 'Loading similar features...', |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('renders similar features when API returns results', async () => { |
| 80 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 81 | + <webstatus-not-found-error-page |
| 82 | + .location=${{search: '?q=test-feature'}} |
| 83 | + ></webstatus-not-found-error-page> |
| 84 | + `); |
| 85 | + |
| 86 | + component.similarFeatures = [ |
| 87 | + {name: 'Feature One', url: '/features/one'}, |
| 88 | + {name: 'Feature Two', url: '/features/two'}, |
| 89 | + ]; |
| 90 | + await component.updateComplete; |
| 91 | + |
| 92 | + const featureList = |
| 93 | + component.shadowRoot?.querySelectorAll('.feature-list li'); |
| 94 | + expect(featureList?.length).to.equal(2); |
| 95 | + expect(featureList?.[0]?.textContent?.trim()).to.equal('Feature One'); |
| 96 | + expect(featureList?.[1]?.textContent?.trim()).to.equal('Feature Two'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('renders "No similar features found." when API returns no results', async () => { |
| 100 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 101 | + <webstatus-not-found-error-page |
| 102 | + .location=${{search: '?q=test-feature'}} |
| 103 | + ></webstatus-not-found-error-page> |
| 104 | + `); |
| 105 | + |
| 106 | + component.similarFeatures = []; |
| 107 | + await component.updateComplete; |
| 108 | + |
| 109 | + const noResultsMessage = component.shadowRoot?.querySelector( |
| 110 | + '.similar-features-container p', |
| 111 | + ); |
| 112 | + expect(noResultsMessage).to.exist; |
| 113 | + expect(noResultsMessage?.textContent?.trim()).to.equal( |
| 114 | + 'No similar features found.', |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('renders all three buttons when featureId exists', async () => { |
| 119 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 120 | + <webstatus-not-found-error-page |
| 121 | + .location=${{search: '?q=test-feature'}} |
| 122 | + ></webstatus-not-found-error-page> |
| 123 | + `); |
| 124 | + |
| 125 | + expect(component.shadowRoot?.querySelector('#error-action-search-btn')).to |
| 126 | + .exist; |
| 127 | + expect(component.shadowRoot?.querySelector('#error-action-home-btn')).to |
| 128 | + .exist; |
| 129 | + expect(component.shadowRoot?.querySelector('#error-action-report')).to |
| 130 | + .exist; |
| 131 | + }); |
| 132 | + |
| 133 | + it('renders only two buttons when featureId does not exist', async () => { |
| 134 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 135 | + <webstatus-not-found-error-page |
| 136 | + .location=${{search: ''}} |
| 137 | + ></webstatus-not-found-error-page> |
| 138 | + `); |
| 139 | + |
| 140 | + expect(component.shadowRoot?.querySelector('#error-action-search-btn')).to |
| 141 | + .not.exist; |
| 142 | + expect(component.shadowRoot?.querySelector('#error-action-home-btn')).to |
| 143 | + .exist; |
| 144 | + expect(component.shadowRoot?.querySelector('#error-action-report')).to |
| 145 | + .exist; |
| 146 | + }); |
| 147 | + |
| 148 | + it('search button contains the correct query parameter', async () => { |
| 149 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 150 | + <webstatus-not-found-error-page |
| 151 | + .location=${{search: '?q=correct-query'}} |
| 152 | + ></webstatus-not-found-error-page> |
| 153 | + `); |
| 154 | + |
| 155 | + const searchButton = component.shadowRoot?.querySelector( |
| 156 | + '#error-action-search-btn', |
| 157 | + ); |
| 158 | + expect(searchButton?.getAttribute('href')).to.equal('/?q=correct-query'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('report issue button links to GitHub', async () => { |
| 162 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 163 | + <webstatus-not-found-error-page></webstatus-not-found-error-page> |
| 164 | + `); |
| 165 | + |
| 166 | + const reportButton = component.shadowRoot?.querySelector( |
| 167 | + '#error-action-report', |
| 168 | + ); |
| 169 | + expect(reportButton?.getAttribute('href')).to.equal(GITHUB_REPO_ISSUE_LINK); |
| 170 | + }); |
| 171 | + |
| 172 | + it('applies correct gap spacing in error-actions when featureId is present', async () => { |
| 173 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 174 | + <webstatus-not-found-error-page |
| 175 | + .location=${{search: '?q=test-feature'}} |
| 176 | + ></webstatus-not-found-error-page> |
| 177 | + `); |
| 178 | + |
| 179 | + const errorActions = component.shadowRoot?.querySelector('#error-actions'); |
| 180 | + expect(errorActions?.getAttribute('style')).to.include('gap: 32px'); |
| 181 | + }); |
| 182 | + |
| 183 | + it('applies correct gap spacing in error-actions when featureId is absent', async () => { |
| 184 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 185 | + <webstatus-not-found-error-page |
| 186 | + .location=${{search: ''}} |
| 187 | + ></webstatus-not-found-error-page> |
| 188 | + `); |
| 189 | + |
| 190 | + const errorActions = component.shadowRoot?.querySelector('#error-actions'); |
| 191 | + expect(errorActions?.getAttribute('style')).to.include('gap: 16px'); |
| 192 | + }); |
| 193 | +}); |
0 commit comments