|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
5 | | -import {getValuesOfAllBodyRows} from '../../../testing/DataGridHelpers.js'; |
6 | 5 | import { |
7 | 6 | assertScreenshot, |
8 | | - dispatchClickEvent, |
9 | 7 | renderElementIntoDOM, |
10 | 8 | } from '../../../testing/DOMHelpers.js'; |
11 | 9 | import {createTarget} from '../../../testing/EnvironmentHelpers.js'; |
12 | 10 | import { |
13 | 11 | describeWithMockConnection, |
14 | 12 | setMockConnectionResponseHandler, |
15 | 13 | } from '../../../testing/MockConnection.js'; |
16 | | -import * as RenderCoordinator from '../../../ui/components/render_coordinator/render_coordinator.js'; |
| 14 | +import {createViewFunctionStub} from '../../../testing/ViewFunctionHelpers.js'; |
17 | 15 |
|
18 | 16 | import * as ApplicationComponents from './components.js'; |
19 | | - |
20 | | -async function renderBounceTrackingMitigationsView(): |
21 | | - Promise<ApplicationComponents.BounceTrackingMitigationsView.BounceTrackingMitigationsView> { |
22 | | - const element = document.createElement('div'); |
23 | | - element.style.display = 'block'; |
24 | | - element.style.width = '640px'; |
25 | | - element.style.height = '480px'; |
26 | | - const component = new ApplicationComponents.BounceTrackingMitigationsView.BounceTrackingMitigationsView(element); |
27 | | - renderElementIntoDOM(component); |
28 | | - |
29 | | - await component.updateComplete; |
30 | | - // The data-grid's renderer is scheduled, so we need to wait until the coordinator |
31 | | - // is done before we can test against it. |
32 | | - await RenderCoordinator.done(); |
33 | | - |
34 | | - return component; |
35 | | -} |
36 | | - |
37 | | -function getInternalDataGridShadowRoot( |
38 | | - component: ApplicationComponents.BounceTrackingMitigationsView.BounceTrackingMitigationsView): ShadowRoot { |
39 | | - const dataGrid = component.element.shadowRoot!.querySelector('devtools-data-grid')!; |
40 | | - assert.isNotNull(dataGrid.shadowRoot); |
41 | | - return dataGrid.shadowRoot; |
42 | | -} |
| 17 | +const {BounceTrackingMitigationsView, DEFAULT_VIEW, ScreenStatusType} = |
| 18 | + ApplicationComponents.BounceTrackingMitigationsView; |
43 | 19 |
|
44 | 20 | describeWithMockConnection('BounceTrackingMitigationsView', () => { |
45 | 21 | it('shows no message or table if the force run button has not been clicked', async () => { |
46 | 22 | createTarget(); |
47 | 23 | setMockConnectionResponseHandler('SystemInfo.getFeatureState', () => ({featureEnabled: true})); |
48 | 24 | setMockConnectionResponseHandler('Storage.runBounceTrackingMitigations', () => ({deletedSites: []})); |
49 | 25 |
|
50 | | - const component = await renderBounceTrackingMitigationsView(); |
51 | | - await RenderCoordinator.done(); |
52 | | - await component.updateComplete; |
53 | | - |
54 | | - const nullGridElement = component.element.shadowRoot!.querySelector('devtools-data-grid'); |
55 | | - assert.isNull(nullGridElement); |
56 | | - |
57 | | - await assertScreenshot('application/bounce-tracking-mitigations-view-initial.png'); |
| 26 | + const view = createViewFunctionStub(BounceTrackingMitigationsView); |
| 27 | + new BounceTrackingMitigationsView(undefined, view); |
58 | 28 |
|
59 | | - const sections = component.element.shadowRoot!.querySelectorAll('devtools-report-section'); |
60 | | - const sectionsText = Array.from(sections).map(section => section.textContent?.trim()); |
61 | | - const expected = [ |
62 | | - 'Force run', |
63 | | - 'Learn more: Bounce Tracking Mitigations', |
64 | | - ]; |
| 29 | + await view.nextInput; |
65 | 30 |
|
66 | | - assert.deepEqual(sectionsText, expected); |
| 31 | + assert.strictEqual(view.input.screenStatus, ScreenStatusType.RESULT); |
| 32 | + assert.isFalse(view.input.seenButtonClick); |
| 33 | + assert.lengthOf(view.input.trackingSites, 0); |
67 | 34 | }); |
68 | 35 |
|
69 | 36 | it('shows a message indicating that Bounce Tracking Mitigations are disabled', async () => { |
70 | 37 | createTarget(); |
71 | 38 | setMockConnectionResponseHandler('SystemInfo.getFeatureState', () => ({featureEnabled: false})); |
72 | 39 |
|
73 | | - const component = await renderBounceTrackingMitigationsView(); |
74 | | - await RenderCoordinator.done(); |
75 | | - await component.updateComplete; |
76 | | - |
77 | | - const nullGridElement = component.element.shadowRoot!.querySelector('devtools-data-grid'); |
78 | | - assert.isNull(nullGridElement); |
79 | | - |
80 | | - await assertScreenshot('application/bounce-tracking-mitigations-view-disabled.png'); |
| 40 | + const view = createViewFunctionStub(BounceTrackingMitigationsView); |
| 41 | + new BounceTrackingMitigationsView(undefined, view); |
81 | 42 |
|
82 | | - const sections = component.element.shadowRoot!.querySelectorAll('devtools-report-section'); |
83 | | - const sectionsText = Array.from(sections).map(section => section.textContent?.trim()); |
84 | | - const expected = [ |
85 | | - 'Bounce tracking mitigations are disabled.', |
86 | | - ]; |
| 43 | + await view.nextInput; |
87 | 44 |
|
88 | | - assert.deepEqual(sectionsText, expected); |
| 45 | + assert.strictEqual(view.input.screenStatus, ScreenStatusType.DISABLED); |
| 46 | + assert.isFalse(view.input.seenButtonClick); |
| 47 | + assert.lengthOf(view.input.trackingSites, 0); |
89 | 48 | }); |
90 | 49 |
|
91 | | - it('hides deleted sites table and shows explanation message when there are no deleted tracking sites', async () => { |
| 50 | + async function testForceRunClick(deletedSites: string[]) { |
| 51 | + const {promise: lock, resolve: freeLock} = Promise.withResolvers(); |
92 | 52 | createTarget(); |
93 | 53 | setMockConnectionResponseHandler('SystemInfo.getFeatureState', () => ({featureEnabled: true})); |
94 | 54 | const runBounceTrackingMitigationsPromise = new Promise(resolve => { |
95 | | - setMockConnectionResponseHandler('Storage.runBounceTrackingMitigations', () => { |
| 55 | + setMockConnectionResponseHandler('Storage.runBounceTrackingMitigations', async () => { |
| 56 | + await lock; |
96 | 57 | resolve(undefined); |
97 | | - return {deletedSites: []}; |
| 58 | + return {deletedSites}; |
98 | 59 | }); |
99 | 60 | }); |
100 | 61 |
|
101 | | - const component = await renderBounceTrackingMitigationsView(); |
102 | | - await RenderCoordinator.done(); |
103 | | - await component.updateComplete; |
| 62 | + const view = createViewFunctionStub(BounceTrackingMitigationsView); |
| 63 | + new BounceTrackingMitigationsView(undefined, view); |
104 | 64 |
|
105 | | - const forceRunButton = component.element.shadowRoot!.querySelector('[aria-label="Force run"]'); |
106 | | - assert.instanceOf(forceRunButton, HTMLElement); |
107 | | - dispatchClickEvent(forceRunButton); |
108 | | - await runBounceTrackingMitigationsPromise; |
| 65 | + await view.nextInput; |
109 | 66 |
|
110 | | - await RenderCoordinator.done(); |
111 | | - await component.updateComplete; |
| 67 | + assert.isFunction(view.input.runMitigations); |
112 | 68 |
|
113 | | - await assertScreenshot('application/bounce-tracking-mitigations-view-empty.png'); |
| 69 | + const runMitigationsPromise = view.input.runMitigations(); |
| 70 | + await view.nextInput; |
| 71 | + |
| 72 | + assert.strictEqual(view.input.screenStatus, ScreenStatusType.RUNNING); |
114 | 73 |
|
115 | | - const nullGridElement = component.element.shadowRoot!.querySelector('devtools-data-grid'); |
116 | | - assert.isNull(nullGridElement); |
| 74 | + freeLock(undefined); |
| 75 | + await Promise.all([runMitigationsPromise, runBounceTrackingMitigationsPromise]); |
| 76 | + await view.nextInput; |
117 | 77 |
|
118 | | - const sections = component.element.shadowRoot!.querySelectorAll('devtools-report-section'); |
119 | | - const sectionsText = Array.from(sections).map(section => section.textContent?.trim()); |
120 | | - const expected = [ |
121 | | - 'Force run', |
122 | | - 'State was not cleared for any potential bounce tracking sites. Either none were identified or third-party cookies are not blocked.', |
123 | | - 'Learn more: Bounce Tracking Mitigations', |
124 | | - ]; |
| 78 | + assert.strictEqual(view.input.screenStatus, ScreenStatusType.RESULT); |
| 79 | + assert.isTrue(view.input.seenButtonClick); |
| 80 | + assert.deepEqual(view.input.trackingSites, deletedSites); |
| 81 | + } |
125 | 82 |
|
126 | | - assert.deepEqual(sectionsText, expected); |
| 83 | + it('hides deleted sites table and shows explanation message when there are no deleted tracking sites', async () => { |
| 84 | + await testForceRunClick([]); |
127 | 85 | }); |
128 | 86 |
|
129 | 87 | it('renders deleted sites in a table', async () => { |
130 | | - createTarget(); |
131 | | - setMockConnectionResponseHandler('SystemInfo.getFeatureState', () => ({featureEnabled: true})); |
132 | | - setMockConnectionResponseHandler( |
133 | | - 'Storage.runBounceTrackingMitigations', () => ({deletedSites: ['tracker-1.example', 'tracker-2.example']})); |
| 88 | + await testForceRunClick(['tracker-1.example', 'tracker-2.example']); |
| 89 | + }); |
134 | 90 |
|
135 | | - const component = await renderBounceTrackingMitigationsView(); |
136 | | - await RenderCoordinator.done(); |
137 | | - await component.updateComplete; |
| 91 | + const createElement = () => { |
| 92 | + const element = document.createElement('div'); |
| 93 | + element.style.display = 'block'; |
| 94 | + element.style.width = '640px'; |
| 95 | + element.style.height = '480px'; |
| 96 | + renderElementIntoDOM(element); |
| 97 | + |
| 98 | + return element; |
| 99 | + }; |
| 100 | + |
| 101 | + const INITIAL_INPUT = { |
| 102 | + screenStatus: ScreenStatusType.INITIALIZING, |
| 103 | + trackingSites: [], |
| 104 | + seenButtonClick: false, |
| 105 | + runMitigations: () => Promise.resolve(), |
| 106 | + }; |
| 107 | + |
| 108 | + it('renders initial state', async () => { |
| 109 | + DEFAULT_VIEW( |
| 110 | + { |
| 111 | + ...INITIAL_INPUT, |
| 112 | + screenStatus: ScreenStatusType.RESULT, |
| 113 | + }, |
| 114 | + undefined, createElement()); |
138 | 115 |
|
139 | | - const forceRunButton = component.element.shadowRoot!.querySelector('[aria-label="Force run"]'); |
140 | | - assert.instanceOf(forceRunButton, HTMLElement); |
141 | | - dispatchClickEvent(forceRunButton); |
| 116 | + await assertScreenshot('application/bounce-tracking-mitigations-view-initial.png'); |
| 117 | + }); |
142 | 118 |
|
143 | | - await RenderCoordinator.done({waitForWork: true}); |
144 | | - await component.updateComplete; |
| 119 | + it('renders disabled state', async () => { |
| 120 | + DEFAULT_VIEW( |
| 121 | + { |
| 122 | + ...INITIAL_INPUT, |
| 123 | + screenStatus: ScreenStatusType.DISABLED, |
| 124 | + }, |
| 125 | + undefined, createElement()); |
145 | 126 |
|
146 | | - await assertScreenshot('application/bounce-tracking-mitigations-view-populated.png'); |
| 127 | + await assertScreenshot('application/bounce-tracking-mitigations-view-disabled.png'); |
| 128 | + }); |
147 | 129 |
|
148 | | - const dataGridShadowRoot = getInternalDataGridShadowRoot(component); |
149 | | - const rowValues = getValuesOfAllBodyRows(dataGridShadowRoot); |
150 | | - assert.deepEqual(rowValues, [ |
151 | | - ['tracker-1.example'], |
152 | | - ['tracker-2.example'], |
153 | | - ]); |
| 130 | + it('renders empty state', async () => { |
| 131 | + DEFAULT_VIEW( |
| 132 | + { |
| 133 | + ...INITIAL_INPUT, |
| 134 | + screenStatus: ScreenStatusType.RESULT, |
| 135 | + seenButtonClick: true, |
| 136 | + }, |
| 137 | + undefined, createElement()); |
| 138 | + |
| 139 | + await assertScreenshot('application/bounce-tracking-mitigations-view-empty.png'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('renders populated state', async () => { |
| 143 | + DEFAULT_VIEW( |
| 144 | + { |
| 145 | + ...INITIAL_INPUT, |
| 146 | + screenStatus: ScreenStatusType.RESULT, |
| 147 | + trackingSites: ['tracker-1.example', 'tracker-2.example'], |
| 148 | + seenButtonClick: true, |
| 149 | + }, |
| 150 | + undefined, createElement()); |
| 151 | + |
| 152 | + await assertScreenshot('application/bounce-tracking-mitigations-view-populated.png'); |
154 | 153 | }); |
155 | 154 | }); |
0 commit comments