diff --git a/src/locatorGenerators.ts b/src/locatorGenerators.ts index 6643993..db9b666 100644 --- a/src/locatorGenerators.ts +++ b/src/locatorGenerators.ts @@ -24,6 +24,7 @@ import { parseSelector, stringifySelector, } from './selectorParser' +import { Ivya } from '.' export type Language = 'javascript' export type LocatorType = @@ -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!) { @@ -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)})` } diff --git a/test/spec.test.ts b/test/spec.test.ts index 9e29016..53fceda 100644 --- a/test/spec.test.ts +++ b/test/spec.test.ts @@ -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') @@ -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