|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { ResultsCollector } from './page-objects/results-collector.js'; |
| 3 | + |
| 4 | +const HTML = '/favicon/index.html'; |
| 5 | +const CONFIG = './integration-test/test-pages/favicon/config/favicon-enabled.json'; |
| 6 | + |
| 7 | +test('favicon feature absent', async ({ page, baseURL }, testInfo) => { |
| 8 | + const CONFIG = './integration-test/test-pages/favicon/config/favicon-absent.json'; |
| 9 | + const favicon = ResultsCollector.create(page, testInfo.project.use); |
| 10 | + await favicon.load(HTML, CONFIG); |
| 11 | + |
| 12 | + // ensure first favicon item was sent |
| 13 | + const messages = await favicon.waitForMessage('faviconFound', 1); |
| 14 | + const url = new URL('/favicon/favicon.png', baseURL); |
| 15 | + |
| 16 | + expect(messages[0].payload.params).toStrictEqual({ |
| 17 | + favicons: [{ href: url.href, rel: 'shortcut icon' }], |
| 18 | + documentUrl: 'http://localhost:3220/favicon/index.html', |
| 19 | + }); |
| 20 | +}); |
| 21 | + |
| 22 | +test('favicon + monitor', async ({ page, baseURL }, testInfo) => { |
| 23 | + const favicon = ResultsCollector.create(page, testInfo.project.use); |
| 24 | + await favicon.load(HTML, CONFIG); |
| 25 | + |
| 26 | + // ensure first favicon item was sent |
| 27 | + await favicon.waitForMessage('faviconFound', 1); |
| 28 | + |
| 29 | + // now update it |
| 30 | + await page.getByRole('button', { name: 'Set override' }).click(); |
| 31 | + |
| 32 | + // wait for the second message |
| 33 | + const messages = await favicon.waitForMessage('faviconFound', 2); |
| 34 | + |
| 35 | + const url1 = new URL('/favicon/favicon.png', baseURL); |
| 36 | + const url2 = new URL('/favicon/new_favicon.png', baseURL); |
| 37 | + |
| 38 | + expect(messages[0].payload.params).toStrictEqual({ |
| 39 | + favicons: [{ href: url1.href, rel: 'shortcut icon' }], |
| 40 | + documentUrl: 'http://localhost:3220/favicon/index.html', |
| 41 | + }); |
| 42 | + |
| 43 | + expect(messages[1].payload.params).toStrictEqual({ |
| 44 | + favicons: [{ href: url2.href, rel: 'shortcut icon' }], |
| 45 | + documentUrl: 'http://localhost:3220/favicon/index.html', |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +test('favicon + monitor + newly added links', async ({ page, baseURL }, testInfo) => { |
| 50 | + const favicon = ResultsCollector.create(page, testInfo.project.use); |
| 51 | + await favicon.load(HTML, CONFIG); |
| 52 | + |
| 53 | + // ensure first favicon item was sent |
| 54 | + await favicon.waitForMessage('faviconFound', 1); |
| 55 | + |
| 56 | + // now cause a new item to be added |
| 57 | + await page.getByRole('button', { name: 'Add new' }).click(); |
| 58 | + |
| 59 | + // wait for the second message |
| 60 | + const messages = await favicon.waitForMessage('faviconFound', 2); |
| 61 | + |
| 62 | + const url1 = new URL('/favicon/favicon.png', baseURL); |
| 63 | + const url2 = new URL('/favicon/new_favicon.png', baseURL); |
| 64 | + |
| 65 | + expect(messages[0].payload.params).toStrictEqual({ |
| 66 | + favicons: [{ href: url1.href, rel: 'shortcut icon' }], |
| 67 | + documentUrl: 'http://localhost:3220/favicon/index.html', |
| 68 | + }); |
| 69 | + |
| 70 | + expect(messages[1].payload.params).toStrictEqual({ |
| 71 | + favicons: [ |
| 72 | + { href: url1.href, rel: 'shortcut icon' }, |
| 73 | + { href: url2.href, rel: 'shortcut icon' }, |
| 74 | + ], |
| 75 | + documentUrl: 'http://localhost:3220/favicon/index.html', |
| 76 | + }); |
| 77 | +}); |
| 78 | + |
| 79 | +test('favicon + monitor (many updates)', async ({ page, baseURL }, testInfo) => { |
| 80 | + const favicon = ResultsCollector.create(page, testInfo.project.use); |
| 81 | + await page.clock.install(); |
| 82 | + await favicon.load(HTML, CONFIG); |
| 83 | + |
| 84 | + // ensure first favicon item was sent |
| 85 | + await favicon.waitForMessage('faviconFound', 1); |
| 86 | + |
| 87 | + // now update it |
| 88 | + await page.getByRole('button', { name: 'Set many overrides' }).click(); |
| 89 | + await page.clock.fastForward(20); |
| 90 | + |
| 91 | + const messages = await favicon.outgoingMessages(); |
| 92 | + expect(messages).toHaveLength(1); |
| 93 | + |
| 94 | + await page.clock.fastForward(60); |
| 95 | + await page.clock.fastForward(100); |
| 96 | + |
| 97 | + { |
| 98 | + const messages = await favicon.outgoingMessages(); |
| 99 | + expect(messages).toHaveLength(3); |
| 100 | + } |
| 101 | + |
| 102 | + { |
| 103 | + const url1 = new URL('/favicon/favicon.png', baseURL); |
| 104 | + const url2 = new URL('/favicon/new_favicon.png?count=0', baseURL); |
| 105 | + const url3 = new URL('/favicon/new_favicon.png?count=1', baseURL); |
| 106 | + |
| 107 | + const messages = await favicon.outgoingMessages(); |
| 108 | + expect(messages.map((x) => /** @type {{params: any}} */ (x.payload).params)).toStrictEqual([ |
| 109 | + { |
| 110 | + favicons: [{ href: url1.href, rel: 'shortcut icon' }], |
| 111 | + documentUrl: 'http://localhost:3220/favicon/index.html', |
| 112 | + }, |
| 113 | + { |
| 114 | + favicons: [{ href: url2.href, rel: 'shortcut icon' }], |
| 115 | + documentUrl: 'http://localhost:3220/favicon/index.html', |
| 116 | + }, |
| 117 | + { |
| 118 | + favicons: [{ href: url3.href, rel: 'shortcut icon' }], |
| 119 | + documentUrl: 'http://localhost:3220/favicon/index.html', |
| 120 | + }, |
| 121 | + ]); |
| 122 | + } |
| 123 | +}); |
| 124 | + |
| 125 | +test('favicon + monitor disabled', async ({ page }, testInfo) => { |
| 126 | + const CONFIG = './integration-test/test-pages/favicon/config/favicon-monitor-disabled.json'; |
| 127 | + const favicon = ResultsCollector.create(page, testInfo.project.use); |
| 128 | + |
| 129 | + await page.clock.install(); |
| 130 | + |
| 131 | + await favicon.load(HTML, CONFIG); |
| 132 | + |
| 133 | + // ensure first favicon item was sent |
| 134 | + await favicon.waitForMessage('faviconFound', 1); |
| 135 | + |
| 136 | + // now update it |
| 137 | + await page.getByRole('button', { name: 'Set override' }).click(); |
| 138 | + |
| 139 | + await expect(page.locator('link')).toHaveAttribute('href', './new_favicon.png'); |
| 140 | + |
| 141 | + // account for the debounce |
| 142 | + await page.clock.fastForward(200); |
| 143 | + |
| 144 | + // ensure only 1 message was still sent (ie: the monitor is disabled) |
| 145 | + const messages = await favicon.outgoingMessages(); |
| 146 | + expect(messages).toHaveLength(1); |
| 147 | +}); |
| 148 | + |
| 149 | +test('favicon feature disabled completely', async ({ page }, testInfo) => { |
| 150 | + const CONFIG = './integration-test/test-pages/favicon/config/favicon-disabled.json'; |
| 151 | + const favicon = ResultsCollector.create(page, testInfo.project.use); |
| 152 | + |
| 153 | + await favicon.load(HTML, CONFIG); |
| 154 | + |
| 155 | + // this is here purely to guard against a false positive in this test. |
| 156 | + // without this manual `wait`, it might be possible for the following assertion to |
| 157 | + // pass, but just because it was too quick (eg: the first message wasn't sent yet) |
| 158 | + await page.waitForTimeout(100); |
| 159 | + |
| 160 | + const messages = await favicon.outgoingMessages(); |
| 161 | + expect(messages).toHaveLength(0); |
| 162 | +}); |
0 commit comments