|
| 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-notfound-error-page.js'; |
| 19 | +import {WebstatusNotFoundErrorPage} from '../webstatus-notfound-error-page.js'; |
| 20 | +import {Task} from '@lit/task'; |
| 21 | +import {APIClient} from '../../contexts/api-client-context.js'; |
| 22 | +import {GITHUB_REPO_ISSUE_LINK} from '../../utils/constants.js'; |
| 23 | + |
| 24 | +type SimilarFeature = {name: string; url: string}; |
| 25 | + |
| 26 | +describe('webstatus-notfound-error-page', () => { |
| 27 | + const featureIdWithMockResults = 'g'; |
| 28 | + const mockSimilarFeatures: SimilarFeature[] = [ |
| 29 | + {name: 'Feature One', url: '/features/dignissimos44'}, |
| 30 | + {name: 'Feature Two', url: '/features/fugiat37'}, |
| 31 | + ]; |
| 32 | + |
| 33 | + it('renders the correct error message when featureId is missing', async () => { |
| 34 | + const component = await fixture<WebstatusNotFoundErrorPage>( |
| 35 | + html`<webstatus-notfound-error-page |
| 36 | + .location=${{search: ''}} |
| 37 | + ></webstatus-notfound-error-page>`, |
| 38 | + ); |
| 39 | + |
| 40 | + expect( |
| 41 | + component.shadowRoot |
| 42 | + ?.querySelector('#error-status-code') |
| 43 | + ?.textContent?.trim(), |
| 44 | + ).to.equal('404'); |
| 45 | + |
| 46 | + expect( |
| 47 | + component.shadowRoot |
| 48 | + ?.querySelector('#error-headline') |
| 49 | + ?.textContent?.trim(), |
| 50 | + ).to.equal('Page not found'); |
| 51 | + |
| 52 | + expect( |
| 53 | + component.shadowRoot |
| 54 | + ?.querySelector('#error-detailed-message .error-message') |
| 55 | + ?.textContent?.trim(), |
| 56 | + ).to.equal("We couldn't find the page you're looking for."); |
| 57 | + }); |
| 58 | + |
| 59 | + it('renders correct message when featureId is provided', async () => { |
| 60 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 61 | + <webstatus-notfound-error-page |
| 62 | + .location=${{search: '?q=test-feature'}} |
| 63 | + ></webstatus-notfound-error-page> |
| 64 | + `); |
| 65 | + |
| 66 | + expect( |
| 67 | + component.shadowRoot?.querySelector('#error-detailed-message') |
| 68 | + ?.textContent, |
| 69 | + ).to.include('We could not find Feature ID: test-feature'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('displays "Loading similar features..." when the API request is pending', async () => { |
| 73 | + const component = await createComponentWithMockedSimilarFeatures( |
| 74 | + 'test-feature', |
| 75 | + [], |
| 76 | + {stayPending: true}, |
| 77 | + ); |
| 78 | + |
| 79 | + const loadingMessage = |
| 80 | + component.shadowRoot?.querySelector('.loading-message'); |
| 81 | + expect(loadingMessage).to.exist; |
| 82 | + expect(loadingMessage?.textContent?.trim()).to.equal( |
| 83 | + 'Loading similar features...', |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it('renders similar features when API returns results', async () => { |
| 88 | + const component = await createComponentWithMockedSimilarFeatures( |
| 89 | + featureIdWithMockResults, |
| 90 | + mockSimilarFeatures, |
| 91 | + ); |
| 92 | + |
| 93 | + const featureList = |
| 94 | + component.shadowRoot?.querySelectorAll('.feature-list li'); |
| 95 | + expect(featureList?.length).to.equal(2); |
| 96 | + expect(featureList?.[0]?.textContent?.trim()).to.equal('Feature One'); |
| 97 | + expect(featureList?.[1]?.textContent?.trim()).to.equal('Feature Two'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('renders only two buttons when featureId does not exist', async () => { |
| 101 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 102 | + <webstatus-notfound-error-page |
| 103 | + .location=${{search: ''}} |
| 104 | + ></webstatus-notfound-error-page> |
| 105 | + `); |
| 106 | + |
| 107 | + expect(component.shadowRoot?.querySelector('#error-action-search-btn')).to |
| 108 | + .not.exist; |
| 109 | + expect(component.shadowRoot?.querySelector('#error-action-home-btn')).to |
| 110 | + .exist; |
| 111 | + expect(component.shadowRoot?.querySelector('#error-action-report')).to |
| 112 | + .exist; |
| 113 | + }); |
| 114 | + |
| 115 | + it('renders all three buttons when featureId and similar results exist', async () => { |
| 116 | + const component = await createComponentWithMockedSimilarFeatures( |
| 117 | + featureIdWithMockResults, |
| 118 | + mockSimilarFeatures, |
| 119 | + ); |
| 120 | + |
| 121 | + expect(component.shadowRoot?.querySelector('#error-action-search-btn')).to |
| 122 | + .exist; |
| 123 | + expect(component.shadowRoot?.querySelector('#error-action-home-btn')).to |
| 124 | + .exist; |
| 125 | + expect(component.shadowRoot?.querySelector('#error-action-report')).to |
| 126 | + .exist; |
| 127 | + }); |
| 128 | + |
| 129 | + it('search button contains the correct query parameter when similar results exist', async () => { |
| 130 | + const component = await createComponentWithMockedSimilarFeatures( |
| 131 | + featureIdWithMockResults, |
| 132 | + mockSimilarFeatures, |
| 133 | + ); |
| 134 | + |
| 135 | + const searchButton = component.shadowRoot?.querySelector( |
| 136 | + '#error-action-search-btn', |
| 137 | + ); |
| 138 | + expect(searchButton?.getAttribute('href')).to.equal( |
| 139 | + `/?q=${featureIdWithMockResults}`, |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it('report issue button links to GitHub', async () => { |
| 144 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 145 | + <webstatus-notfound-error-page |
| 146 | + .location=${{search: ''}} |
| 147 | + ></webstatus-notfound-error-page> |
| 148 | + `); |
| 149 | + |
| 150 | + const reportButton = component.shadowRoot?.querySelector( |
| 151 | + '#error-action-report', |
| 152 | + ); |
| 153 | + expect(reportButton?.getAttribute('href')).to.equal(GITHUB_REPO_ISSUE_LINK); |
| 154 | + }); |
| 155 | + |
| 156 | + async function createComponentWithMockedSimilarFeatures( |
| 157 | + featureId: string, |
| 158 | + mockData: SimilarFeature[], |
| 159 | + options: {stayPending?: boolean} = {}, |
| 160 | + ): Promise<WebstatusNotFoundErrorPage> { |
| 161 | + const component = await fixture<WebstatusNotFoundErrorPage>(html` |
| 162 | + <webstatus-notfound-error-page |
| 163 | + .location=${{search: `?q=${featureId}`}} |
| 164 | + ></webstatus-notfound-error-page> |
| 165 | + `); |
| 166 | + |
| 167 | + component._similarResults = new Task<[APIClient, string], SimilarFeature[]>( |
| 168 | + component, |
| 169 | + { |
| 170 | + args: () => [undefined as unknown as APIClient, featureId], |
| 171 | + task: async () => { |
| 172 | + if (options.stayPending) return new Promise(() => {}); |
| 173 | + return mockData; |
| 174 | + }, |
| 175 | + }, |
| 176 | + ); |
| 177 | + |
| 178 | + component._similarResults.run(); |
| 179 | + await component.updateComplete; |
| 180 | + return component; |
| 181 | + } |
| 182 | +}); |
0 commit comments