Skip to content

Commit 07070a1

Browse files
Merge pull request #15 from testing-library/fix-refetching-stale-elements
fix: stale element refetching
2 parents d524714 + be89ae7 commit 07070a1

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
"license": "ISC",
2929
"dependencies": {
3030
"@babel/runtime": "^7.4.3",
31-
"@testing-library/dom": "^7.0.4"
31+
"@testing-library/dom": "^7.0.4",
32+
"simmerjs": "^0.5.6"
3233
},
3334
"peerDependencies": {
3435
"webdriverio": "*"
3536
},
3637
"devDependencies": {
38+
"@types/simmerjs": "^0.5.1",
3739
"@typescript-eslint/eslint-plugin": "^4.14.0",
3840
"@typescript-eslint/parser": "^4.14.0",
3941
"@wdio/cli": "^7.3.1",

src/index.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import path from 'path'
44
import fs from 'fs'
55
import {queries as baseQueries} from '@testing-library/dom'
6+
import 'simmerjs'
67

78
import {
89
BrowserBase,
@@ -29,17 +30,28 @@ const DOM_TESTING_LIBRARY_UMD = fs
2930
.readFileSync(DOM_TESTING_LIBRARY_UMD_PATH)
3031
.toString()
3132

33+
const SIMMERJS = fs
34+
.readFileSync(require.resolve('simmerjs/dist/simmer.js'))
35+
.toString()
36+
3237
let _config: Partial<Config>
3338

3439
async function injectDOMTestingLibrary(container: ElementBase) {
3540
const shouldInject = await container.execute(function () {
36-
return !window.TestingLibraryDom
41+
return {
42+
domTestingLibrary: !window.TestingLibraryDom,
43+
simmer: !window.Simmer,
44+
}
3745
})
3846

39-
if (shouldInject) {
47+
if (shouldInject.domTestingLibrary) {
4048
await container.execute(DOM_TESTING_LIBRARY_UMD)
4149
}
4250

51+
if (shouldInject.simmer) {
52+
await container.execute(SIMMERJS)
53+
}
54+
4355
if (_config) {
4456
await container.execute(function (config: Config) {
4557
window.TestingLibraryDom.configure(config)
@@ -71,7 +83,7 @@ function executeQuery(
7183
container: HTMLElement,
7284
...args: any[]
7385
) {
74-
const done = args.pop() as (result: any) => void;
86+
const done = args.pop() as (result: any) => void
7587

7688
// @ts-ignore
7789
function deserializeObject(object) {
@@ -104,25 +116,20 @@ function executeQuery(
104116
waitForOptions,
105117
),
106118
)
107-
.then(done)
119+
.then((result) => {
120+
if (!result) {
121+
return done(null)
122+
}
123+
if (Array.isArray(result)) {
124+
return done(
125+
result.map((element) => ({selector: window.Simmer(element)})),
126+
)
127+
}
128+
return done({selector: window.Simmer(result)})
129+
})
108130
.catch((e) => done(e.message))
109131
}
110132

111-
/*
112-
Always include element key "element-6066-11e4-a52e-4f735466cecf": WebdriverIO
113-
checks whether this key is a string to determine if the selector is actually a
114-
WebElement JSON. If the selector is a WebElement JSON it uses it to create a new
115-
Element. There are valid WebElement JSONs that exclude the key but can be turned
116-
into Elements, such as { ELEMENT: elementId }; this can happen in setups that
117-
aren't generated by @wdio/cli.
118-
*/
119-
function createElement(container: ElementBase, elementValue: any) {
120-
return container.$({
121-
'element-6066-11e4-a52e-4f735466cecf': '',
122-
...elementValue,
123-
})
124-
}
125-
126133
function createQuery(element: ElementBase, queryName: string) {
127134
return async (...args: any[]) => {
128135
await injectDOMTestingLibrary(element)
@@ -143,10 +150,10 @@ function createQuery(element: ElementBase, queryName: string) {
143150
}
144151

145152
if (Array.isArray(result)) {
146-
return Promise.all(result.map(createElement.bind(null, element)))
153+
return Promise.all(result.map(({selector}) => element.$(selector)))
147154
}
148155

149-
return createElement(element, result)
156+
return element.$(result.selector)
150157
}
151158
}
152159

src/types.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ export type ElementBase = {
2020
...args: any[]
2121
): Promise<T>
2222

23-
execute<T>(
24-
script: string | ((...args: any[]) => T),
25-
...args: any[]
26-
): T
23+
execute<T>(script: string | ((...args: any[]) => T), ...args: any[]): T
2724

2825
executeAsync(script: string | ((...args: any[]) => void), ...args: any[]): any
2926
}

test/async/queries.e2e.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import refetchElement from 'webdriverio/build/utils/refetchElement'
2+
13
import {setupBrowser} from '../../src'
24

35
describe('queries', () => {
@@ -157,4 +159,14 @@ describe('queries', () => {
157159
/Unable to find an element with the text/,
158160
)
159161
})
162+
163+
it('can refetch an element', async () => {
164+
const {getByText} = setupBrowser(browser)
165+
166+
const button = await getByText('Unique Button Text')
167+
168+
expect(JSON.stringify(button)).toBe(
169+
JSON.stringify(await refetchElement(button, 'click')),
170+
)
171+
})
160172
})

0 commit comments

Comments
 (0)