Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/locatorGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
parseSelector,
stringifySelector,
} from './selectorParser'
import { Ivya } from '.'

export type Language = 'javascript'
export type LocatorType =
Expand Down Expand Up @@ -417,8 +418,8 @@ export class JavaScriptLocatorFactory implements LocatorFactory {
attrs.push(`name: ${this.regexToSourceString(options.name)}`)
} else if (typeof options.name === 'string') {
attrs.push(`name: ${this.quote(options.name)}`)
if (options.exact) {
attrs.push(`exact: true`)
if (options.exact != null && Ivya.options.exact !== options.exact) {
attrs.push(`exact: ${options.exact}`)
}
}
for (const { name, value } of options.attrs!) {
Expand Down Expand Up @@ -478,8 +479,8 @@ export class JavaScriptLocatorFactory implements LocatorFactory {
if (isRegExp(body)) {
return `${method}(${this.regexToSourceString(body)})`
}
return exact
? `${method}(${this.quote(body)}, { exact: true })`
return exact != null && Ivya.options.exact !== exact
? `${method}(${this.quote(body)}, { exact: ${exact} })`
: `${method}(${this.quote(body)})`
}

Expand Down
65 changes: 64 additions & 1 deletion test/spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
Ivya,
asLocator,
} from '../src'
import { expect, test } from 'vitest'
import { beforeEach, expect, test } from 'vitest'

beforeEach(() => {
// the default
Ivya.options.exact = false
})

test('works correctly', () => {
const button = document.createElement('button')
Expand Down Expand Up @@ -48,6 +53,64 @@ test('file input', () => {
)
})

test('asLocator generates "exact" correctly dependning on the default', () => {
Ivya.options.exact = false

expect(asLocator('javascript', getByTextSelector('Hello'))).toMatchInlineSnapshot(
`"getByText('Hello')"`
)
expect(
asLocator('javascript', getByTextSelector('Hello', { exact: true }))
).toMatchInlineSnapshot(`"getByText('Hello', { exact: true })"`)
expect(
asLocator('javascript', getByTextSelector('Hello', { exact: false }))
).toMatchInlineSnapshot(`"getByText('Hello')"`)

expect(
asLocator('javascript', getByRoleSelector('alert', { name: 'Hello' }))
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`)
expect(
asLocator(
'javascript',
getByRoleSelector('alert', { name: 'Hello', exact: true })
)
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello', exact: true })"`)
expect(
asLocator(
'javascript',
getByRoleSelector('alert', { name: 'Hello', exact: false })
)
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`)

Ivya.options.exact = true

expect(asLocator('javascript', getByTextSelector('Hello'))).toMatchInlineSnapshot(
`"getByText('Hello')"`
)
expect(
asLocator('javascript', getByTextSelector('Hello', { exact: true }))
).toMatchInlineSnapshot(`"getByText('Hello')"`)
expect(
asLocator('javascript', getByTextSelector('Hello', { exact: false }))
).toMatchInlineSnapshot(`"getByText('Hello', { exact: false })"`)

expect(
asLocator('javascript', getByRoleSelector('alert', { name: 'Hello' }))
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`)
expect(
asLocator(
'javascript',
getByRoleSelector('alert', { name: 'Hello', exact: true })
)
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello' })"`)
expect(
asLocator(
'javascript',
getByRoleSelector('alert', { name: 'Hello', exact: false })
)
).toMatchInlineSnapshot(`"getByRole('alert', { name: 'Hello', exact: false })"`)
})

test('global exact option affects all selector helpers', () => {
Ivya.options.exact = true

Expand Down
Loading