Skip to content

Commit 3618f35

Browse files
authored
fix: generate exact: in asLocator only if it's different from the default (#23)
1 parent 6228b43 commit 3618f35

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

src/locatorGenerators.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
parseSelector,
2525
stringifySelector,
2626
} from './selectorParser'
27+
import { Ivya } from '.'
2728

2829
export type Language = 'javascript'
2930
export type LocatorType =
@@ -417,8 +418,8 @@ export class JavaScriptLocatorFactory implements LocatorFactory {
417418
attrs.push(`name: ${this.regexToSourceString(options.name)}`)
418419
} else if (typeof options.name === 'string') {
419420
attrs.push(`name: ${this.quote(options.name)}`)
420-
if (options.exact) {
421-
attrs.push(`exact: true`)
421+
if (options.exact != null && Ivya.options.exact !== options.exact) {
422+
attrs.push(`exact: ${options.exact}`)
422423
}
423424
}
424425
for (const { name, value } of options.attrs!) {
@@ -478,8 +479,8 @@ export class JavaScriptLocatorFactory implements LocatorFactory {
478479
if (isRegExp(body)) {
479480
return `${method}(${this.regexToSourceString(body)})`
480481
}
481-
return exact
482-
? `${method}(${this.quote(body)}, { exact: true })`
482+
return exact != null && Ivya.options.exact !== exact
483+
? `${method}(${this.quote(body)}, { exact: ${exact} })`
483484
: `${method}(${this.quote(body)})`
484485
}
485486

test/spec.test.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import {
88
Ivya,
99
asLocator,
1010
} from '../src'
11-
import { expect, test } from 'vitest'
11+
import { beforeEach, expect, test } from 'vitest'
12+
13+
beforeEach(() => {
14+
// the default
15+
Ivya.options.exact = false
16+
})
1217

1318
test('works correctly', () => {
1419
const button = document.createElement('button')
@@ -48,6 +53,64 @@ test('file input', () => {
4853
)
4954
})
5055

56+
test('asLocator generates "exact" correctly dependning on the default', () => {
57+
Ivya.options.exact = false
58+
59+
expect(asLocator('javascript', getByTextSelector('Hello'))).toMatchInlineSnapshot(
60+
`"getByText('Hello')"`
61+
)
62+
expect(
63+
asLocator('javascript', getByTextSelector('Hello', { exact: true }))
64+
).toMatchInlineSnapshot(`"getByText('Hello', { exact: true })"`)
65+
expect(
66+
asLocator('javascript', getByTextSelector('Hello', { exact: false }))
67+
).toMatchInlineSnapshot(`"getByText('Hello')"`)
68+
69+
expect(
70+
asLocator('javascript', getByRoleSelector('alert', { name: 'Hello' }))
71+
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`)
72+
expect(
73+
asLocator(
74+
'javascript',
75+
getByRoleSelector('alert', { name: 'Hello', exact: true })
76+
)
77+
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello', exact: true })"`)
78+
expect(
79+
asLocator(
80+
'javascript',
81+
getByRoleSelector('alert', { name: 'Hello', exact: false })
82+
)
83+
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`)
84+
85+
Ivya.options.exact = true
86+
87+
expect(asLocator('javascript', getByTextSelector('Hello'))).toMatchInlineSnapshot(
88+
`"getByText('Hello')"`
89+
)
90+
expect(
91+
asLocator('javascript', getByTextSelector('Hello', { exact: true }))
92+
).toMatchInlineSnapshot(`"getByText('Hello')"`)
93+
expect(
94+
asLocator('javascript', getByTextSelector('Hello', { exact: false }))
95+
).toMatchInlineSnapshot(`"getByText('Hello', { exact: false })"`)
96+
97+
expect(
98+
asLocator('javascript', getByRoleSelector('alert', { name: 'Hello' }))
99+
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`)
100+
expect(
101+
asLocator(
102+
'javascript',
103+
getByRoleSelector('alert', { name: 'Hello', exact: true })
104+
)
105+
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`)
106+
expect(
107+
asLocator(
108+
'javascript',
109+
getByRoleSelector('alert', { name: 'Hello', exact: false })
110+
)
111+
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello', exact: false })"`)
112+
})
113+
51114
test('global exact option affects all selector helpers', () => {
52115
Ivya.options.exact = true
53116

0 commit comments

Comments
 (0)