|
| 1 | +/** @flow */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const listAppUtils = require('./list-app-utils'); |
| 6 | +const devToolsUtils = require('./devtools-utils'); |
| 7 | +const {test, expect} = require('@playwright/test'); |
| 8 | +const config = require('../../playwright.config'); |
| 9 | +test.use(config); |
| 10 | +test.describe('Components', () => { |
| 11 | + let page; |
| 12 | + |
| 13 | + test.beforeEach(async ({browser}) => { |
| 14 | + page = await browser.newPage(); |
| 15 | + |
| 16 | + await page.goto('http://localhost:8080/e2e.html', { |
| 17 | + waitUntil: 'domcontentloaded', |
| 18 | + }); |
| 19 | + |
| 20 | + await page.waitForSelector('#iframe'); |
| 21 | + |
| 22 | + await devToolsUtils.clickButton(page, 'TabBarButton-components'); |
| 23 | + }); |
| 24 | + |
| 25 | + test('Should display initial React components', async () => { |
| 26 | + const appRowCount = await page.evaluate(() => { |
| 27 | + const {createTestNameSelector, findAllNodes} = window.REACT_DOM_APP; |
| 28 | + const container = document.getElementById('iframe').contentDocument; |
| 29 | + const rows = findAllNodes(container, [ |
| 30 | + createTestNameSelector('ListItem'), |
| 31 | + ]); |
| 32 | + return rows.length; |
| 33 | + }); |
| 34 | + expect(appRowCount).toBe(3); |
| 35 | + |
| 36 | + const devToolsRowCount = await devToolsUtils.getElementCount( |
| 37 | + page, |
| 38 | + 'ListItem' |
| 39 | + ); |
| 40 | + expect(devToolsRowCount).toBe(3); |
| 41 | + }); |
| 42 | + |
| 43 | + test('Should display newly added React components', async () => { |
| 44 | + await listAppUtils.addItem(page, 'four'); |
| 45 | + |
| 46 | + const count = await devToolsUtils.getElementCount(page, 'ListItem'); |
| 47 | + expect(count).toBe(4); |
| 48 | + }); |
| 49 | + |
| 50 | + test('Should allow elements to be inspected', async () => { |
| 51 | + // Select the first list item in DevTools. |
| 52 | + await devToolsUtils.selectElement(page, 'ListItem', 'List\nApp'); |
| 53 | + |
| 54 | + // Then read the inspected values. |
| 55 | + const [propName, propValue, sourceText] = await page.evaluate(() => { |
| 56 | + const {createTestNameSelector, findAllNodes} = window.REACT_DOM_DEVTOOLS; |
| 57 | + const container = document.getElementById('devtools'); |
| 58 | + |
| 59 | + const editableName = findAllNodes(container, [ |
| 60 | + createTestNameSelector('InspectedElementPropsTree'), |
| 61 | + createTestNameSelector('EditableName'), |
| 62 | + ])[0]; |
| 63 | + const editableValue = findAllNodes(container, [ |
| 64 | + createTestNameSelector('InspectedElementPropsTree'), |
| 65 | + createTestNameSelector('EditableValue'), |
| 66 | + ])[0]; |
| 67 | + const source = findAllNodes(container, [ |
| 68 | + createTestNameSelector('InspectedElementView-Source'), |
| 69 | + ])[0]; |
| 70 | + |
| 71 | + return [editableName.value, editableValue.value, source.innerText]; |
| 72 | + }); |
| 73 | + |
| 74 | + expect(propName).toBe('label'); |
| 75 | + expect(propValue).toBe('"one"'); |
| 76 | + expect(sourceText).toContain('ListApp.js'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('should allow props to be edited', async () => { |
| 80 | + // Select the first list item in DevTools. |
| 81 | + await devToolsUtils.selectElement(page, 'ListItem', 'List\nApp'); |
| 82 | + |
| 83 | + // Then edit the label prop. |
| 84 | + await page.evaluate(() => { |
| 85 | + const {createTestNameSelector, focusWithin} = window.REACT_DOM_DEVTOOLS; |
| 86 | + const container = document.getElementById('devtools'); |
| 87 | + |
| 88 | + focusWithin(container, [ |
| 89 | + createTestNameSelector('InspectedElementPropsTree'), |
| 90 | + createTestNameSelector('EditableValue'), |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | + page.keyboard.press('Backspace'); // " |
| 95 | + page.keyboard.press('Backspace'); // e |
| 96 | + page.keyboard.press('Backspace'); // n |
| 97 | + page.keyboard.press('Backspace'); // o |
| 98 | + page.keyboard.insertText('new"'); |
| 99 | + page.keyboard.press('Enter'); |
| 100 | + |
| 101 | + await page.waitForFunction(() => { |
| 102 | + const {createTestNameSelector, findAllNodes} = window.REACT_DOM_APP; |
| 103 | + const container = document.getElementById('iframe').contentDocument; |
| 104 | + const rows = findAllNodes(container, [ |
| 105 | + createTestNameSelector('ListItem'), |
| 106 | + ])[0]; |
| 107 | + return rows.innerText === 'new'; |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + test('should load and parse hook names for the inspected element', async () => { |
| 112 | + // Select the List component DevTools. |
| 113 | + await devToolsUtils.selectElement(page, 'List', 'App'); |
| 114 | + |
| 115 | + // Then click to load and parse hook names. |
| 116 | + await devToolsUtils.clickButton(page, 'LoadHookNamesButton'); |
| 117 | + |
| 118 | + // Make sure the expected hook names are parsed and displayed eventually. |
| 119 | + await page.waitForFunction( |
| 120 | + hookNames => { |
| 121 | + const { |
| 122 | + createTestNameSelector, |
| 123 | + findAllNodes, |
| 124 | + } = window.REACT_DOM_DEVTOOLS; |
| 125 | + const container = document.getElementById('devtools'); |
| 126 | + |
| 127 | + const hooksTree = findAllNodes(container, [ |
| 128 | + createTestNameSelector('InspectedElementHooksTree'), |
| 129 | + ])[0]; |
| 130 | + |
| 131 | + if (!hooksTree) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + const hooksTreeText = hooksTree.innerText; |
| 136 | + |
| 137 | + for (let i = 0; i < hookNames.length; i++) { |
| 138 | + if (!hooksTreeText.includes(hookNames[i])) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return true; |
| 144 | + }, |
| 145 | + ['State(items)', 'Ref(inputRef)'] |
| 146 | + ); |
| 147 | + }); |
| 148 | + |
| 149 | + test('should allow searching for component by name', async () => { |
| 150 | + async function getComponentSearchResultsCount() { |
| 151 | + return await page.evaluate(() => { |
| 152 | + const { |
| 153 | + createTestNameSelector, |
| 154 | + findAllNodes, |
| 155 | + } = window.REACT_DOM_DEVTOOLS; |
| 156 | + const container = document.getElementById('devtools'); |
| 157 | + |
| 158 | + const element = findAllNodes(container, [ |
| 159 | + createTestNameSelector('ComponentSearchInput-ResultsCount'), |
| 160 | + ])[0]; |
| 161 | + return element.innerText; |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + await page.evaluate(() => { |
| 166 | + const {createTestNameSelector, focusWithin} = window.REACT_DOM_DEVTOOLS; |
| 167 | + const container = document.getElementById('devtools'); |
| 168 | + |
| 169 | + focusWithin(container, [ |
| 170 | + createTestNameSelector('ComponentSearchInput-Input'), |
| 171 | + ]); |
| 172 | + }); |
| 173 | + |
| 174 | + page.keyboard.insertText('List'); |
| 175 | + let count = await getComponentSearchResultsCount(); |
| 176 | + expect(count).toBe('1 | 4'); |
| 177 | + |
| 178 | + page.keyboard.insertText('Item'); |
| 179 | + count = await getComponentSearchResultsCount(); |
| 180 | + expect(count).toBe('1 | 3'); |
| 181 | + |
| 182 | + page.keyboard.press('Enter'); |
| 183 | + count = await getComponentSearchResultsCount(); |
| 184 | + expect(count).toBe('2 | 3'); |
| 185 | + |
| 186 | + page.keyboard.press('Enter'); |
| 187 | + count = await getComponentSearchResultsCount(); |
| 188 | + expect(count).toBe('3 | 3'); |
| 189 | + |
| 190 | + page.keyboard.press('Enter'); |
| 191 | + count = await getComponentSearchResultsCount(); |
| 192 | + expect(count).toBe('1 | 3'); |
| 193 | + |
| 194 | + page.keyboard.press('Shift+Enter'); |
| 195 | + count = await getComponentSearchResultsCount(); |
| 196 | + expect(count).toBe('3 | 3'); |
| 197 | + |
| 198 | + page.keyboard.press('Shift+Enter'); |
| 199 | + count = await getComponentSearchResultsCount(); |
| 200 | + expect(count).toBe('2 | 3'); |
| 201 | + |
| 202 | + page.keyboard.press('Shift+Enter'); |
| 203 | + count = await getComponentSearchResultsCount(); |
| 204 | + expect(count).toBe('1 | 3'); |
| 205 | + }); |
| 206 | +}); |
0 commit comments