|
| 1 | +import {DOCUMENT} from '@angular/core'; |
| 2 | +import {TestBed} from '@angular/core/testing'; |
| 3 | +import {Meta} from '@angular/platform-browser'; |
| 4 | +import siteConfig from '../../../ngmd.config'; |
| 5 | +import {PageMetaService, canonicalUrl} from './page-meta.service'; |
| 6 | + |
| 7 | +vi.mock('virtual:ngmd/page-meta', () => ({ |
| 8 | + pageMeta: { |
| 9 | + '/inspectors/router': {editUrl: '', lastUpdated: '', description: 'The Router tab.'}, |
| 10 | + '/contributing/kitchen-sink': {editUrl: '', lastUpdated: '', noIndex: true}, |
| 11 | + }, |
| 12 | +})); |
| 13 | + |
| 14 | +describe('canonicalUrl', () => { |
| 15 | + it('drops the query, fragment and trailing slash', () => { |
| 16 | + expect(canonicalUrl('/inspectors/router/?tab=1#agent-tools')).toBe( |
| 17 | + 'https://pangular-inspector.dev/inspectors/router', |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + it('keeps the slash of the homepage', () => { |
| 22 | + expect(canonicalUrl('/')).toBe('https://pangular-inspector.dev/'); |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +describe('PageMetaService', () => { |
| 27 | + const tag = (selector: string) => TestBed.inject(Meta).getTag(selector)?.content; |
| 28 | + |
| 29 | + it('sets the canonical link, og:url, titles and the page description', () => { |
| 30 | + TestBed.inject(PageMetaService).setPage( |
| 31 | + '/inspectors/router#agent-tools', |
| 32 | + 'Pangular Inspector | Router', |
| 33 | + ); |
| 34 | + |
| 35 | + const canonical = TestBed.inject(DOCUMENT).head.querySelector('link[rel="canonical"]'); |
| 36 | + expect(canonical?.getAttribute('href')).toBe( |
| 37 | + 'https://pangular-inspector.dev/inspectors/router', |
| 38 | + ); |
| 39 | + expect(tag('property="og:url"')).toBe('https://pangular-inspector.dev/inspectors/router'); |
| 40 | + expect(tag('property="og:title"')).toBe('Pangular Inspector | Router'); |
| 41 | + expect(tag('name="twitter:title"')).toBe('Pangular Inspector | Router'); |
| 42 | + for (const selector of [ |
| 43 | + 'name="description"', |
| 44 | + 'property="og:description"', |
| 45 | + 'name="twitter:description"', |
| 46 | + ]) |
| 47 | + expect(tag(selector)).toBe('The Router tab.'); |
| 48 | + expect(tag('name="robots"')).toBeUndefined(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('falls back to the site description, and marks a noIndex page', () => { |
| 52 | + const pageMeta = TestBed.inject(PageMetaService); |
| 53 | + |
| 54 | + pageMeta.setPage('/contributing/kitchen-sink', 'Kitchen sink'); |
| 55 | + expect(tag('name="description"')).toBe(siteConfig.site.description); |
| 56 | + expect(tag('name="robots"')).toBe('noindex'); |
| 57 | + |
| 58 | + pageMeta.setPage('/inspectors/router', 'Router'); |
| 59 | + expect(tag('name="robots"')).toBeUndefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('keeps one canonical link across pages', () => { |
| 63 | + const pageMeta = TestBed.inject(PageMetaService); |
| 64 | + pageMeta.setPage('/a', 'A'); |
| 65 | + pageMeta.setPage('/b', 'B'); |
| 66 | + |
| 67 | + const links = TestBed.inject(DOCUMENT).head.querySelectorAll('link[rel="canonical"]'); |
| 68 | + expect(links.length).toBe(1); |
| 69 | + expect(links[0].getAttribute('href')).toBe('https://pangular-inspector.dev/b'); |
| 70 | + }); |
| 71 | +}); |
0 commit comments