Skip to content

Commit

Permalink
Merge pull request #102 from joomcode/fix/use-testId-instead-of-locat…
Browse files Browse the repository at this point in the history
…orId

fix: rename `locatorId` to `testId` everywhere
  • Loading branch information
uid11 authored Jan 11, 2025
2 parents d1e2d38 + 640e9a5 commit c6f82dd
Show file tree
Hide file tree
Showing 26 changed files with 140 additions and 168 deletions.
6 changes: 3 additions & 3 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ parserOptions:
sourceType: module
plugins: [simple-import-sort, typescript-sort-keys]
root: true
ignorePatterns: [bin/forks/*/package, build, local]
ignorePatterns: [build, local]
rules:
class-methods-use-this: off
complexity: [error, {max: 10}]
Expand Down Expand Up @@ -74,7 +74,7 @@ rules:
import/no-unassigned-import: error
import/no-unresolved:
- error
- {commonjs: true, ignore: [autotests, e2ed/testcafe]}
- {commonjs: true, ignore: [autotests]}
import/no-unused-modules:
- error
- ignoreExports:
Expand Down Expand Up @@ -109,7 +109,7 @@ rules:
- ':not(MethodDefinition, Property) > FunctionExpression'
no-return-await: off
no-shadow: off
no-underscore-dangle: [error, {allow: [_onConfigureResponse]}]
no-underscore-dangle: error
no-unused-expressions: off
no-useless-constructor: off
no-use-before-define: off
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(The MIT License)

Copyright (c) 2021-2024 SIA Joom
Copyright (c) 2021-2025 SIA Joom

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
2 changes: 1 addition & 1 deletion autotests/packs/allTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file Pack file (file with configuration of pack).
* Do not import anything (from `utils`, etc) into this file other than
* the types and values from `../configurator`, `e2ed/configurator`, `e2ed/constants`
* the types and values from `../configurator`, `e2ed/configurator`
* or other packs (because the pack is compiled separately from the tests themselves
* and has separate TypeScript scope).
*/
Expand Down
8 changes: 8 additions & 0 deletions autotests/packs/local.example.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* @file Pack for local development.
* Do not import anything (from `utils`, etc) into this file other than
* the types and values from `../configurator`, `e2ed/configurator`
* or other packs (because the pack is compiled separately from the tests themselves
* and has separate TypeScript scope).
*/

import {pack as allTestsPack} from './allTests';

import type {Pack} from 'autotests/configurator';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export class E2edReportExample extends Page<CustomPageParams> {
/**
* Button tabs in navigation bar with test retries.
*/
readonly navigationRetriesButton: Selector =
this.navigationRetries.findByLocatorId('RetryButton');
readonly navigationRetriesButton: Selector = this.navigationRetries.findByTestId('RetryButton');

/**
* Selected button tab in navigation bar with test retries.
Expand All @@ -52,7 +51,7 @@ export class E2edReportExample extends Page<CustomPageParams> {
/**
* Test run button.
*/
readonly testRunButton: Selector = this.testRunsList.findByLocatorId('TestRunButton');
readonly testRunButton: Selector = this.testRunsList.findByTestId('TestRunButton');

/**
* List of test runs of retry.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {cssSelector} from 'autotests/selectors';
import {getCssSelector} from 'autotests/selectors';

import type {Selector} from 'e2ed/types';

Expand All @@ -8,7 +8,7 @@ const testId = 'TestRunButton';
* `TestRun` button.
*/
export class TestRunButton {
static readonly parameters: string = cssSelector(testId, 'parameters');
static readonly parameters: string = getCssSelector(testId, 'parameters');

readonly selector: Selector;

Expand Down
17 changes: 3 additions & 14 deletions autotests/selectors/createSelector.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import {attributesOptions} from 'autotests/constants';
import {createSelectorFunction} from 'e2ed/selectors';

import type {CreateSelector} from 'e2ed/types';

/**
* Main functions for creating selectors and working with selectors.
* Main function for creating selectors and working with selectors.
*/
export const createSelector: CreateSelector = createSelectorFunction({
getLocatorAttributeName: (parameter) => {
if (parameter === 'id') {
return 'data-testid';
}

if (parameter === 'runhash') {
return 'data-runhash';
}

return `data-test-${parameter}`;
},
});
export const createSelector: CreateSelector = createSelectorFunction(attributesOptions);
2 changes: 1 addition & 1 deletion autotests/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {createSelector} from './createSelector';
export {htmlElementSelector} from './htmlElementSelector';
export {inputSelector} from './inputSelector';
export {cssSelector, locator, testId} from './locator';
export {getCssSelector, getTestId, locator} from './locator';
17 changes: 9 additions & 8 deletions autotests/selectors/locator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {attributesOptions} from 'autotests/constants';
import {createTestUtils} from 'e2ed/createLocator';
import {createTestLocator} from 'e2ed/createLocator';

import {createSelector} from './createSelector';

import type {LocatorFunction} from 'create-locator';
import type {Selector} from 'e2ed/types';

/**
* Test utils, that produce `Selector`, CSS selector string and `testId` string.
* Locator kit with `locator` function, that produce `Selector`,
* and additional `getSelector` and `getTestId` functions.
*/
const utils = createTestUtils({
const locatorKit = createTestLocator({
attributesOptions,
createLocatorByCssSelector: (selector) =>
createSelector(selector.replace('data-test-runhash', 'data-runhash')),
Expand All @@ -19,14 +20,14 @@ const utils = createTestUtils({
/**
* Locator, that produce `Selector`.
*/
export const locator: LocatorFunction<Selector> = utils.locator;
export const locator: LocatorFunction<Selector> = locatorKit.locator;

/**
* Locator, that produce CSS selector string.
* Get CSS selector string.
*/
export const cssSelector: LocatorFunction<string> = utils.selector;
export const getCssSelector: LocatorFunction<string> = locatorKit.getSelector;

/**
* Locator, that produce `testId` string.
* Get `testId` string.
*/
export const testId: LocatorFunction<string> = utils.testId;
export const getTestId: LocatorFunction<string> = locatorKit.getTestId;
2 changes: 1 addition & 1 deletion autotests/tests/e2edReportExample/selectorCustomMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test('selector custom methods', {meta: {testId: '15'}}, async () => {
reportPage.navigationRetriesButtonSelected.getDescription(),
'selector has apropriate description',
).eql(
'[data-testid="RetriesButtons"].findByLocatorId(RetryButton).filterByLocatorParameter(selected, true)',
'[data-testid="RetriesButtons"].findByTestId(RetryButton).filterByLocatorParameter(selected, true)',
);

await click(reportPage.navigationRetriesButton.nth(0));
Expand Down
26 changes: 13 additions & 13 deletions autotests/tests/internalTypeTests/selectors.skip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@ import {createSelector, htmlElementSelector, locator} from 'autotests/selectors'
import type {Selector} from 'e2ed/types';

// @ts-expect-error: wrong number of arguments
htmlElementSelector.findByLocatorId();
htmlElementSelector.findByTestId();
// @ts-expect-error: wrong type of arguments
htmlElementSelector.findByLocatorId(0);
htmlElementSelector.findByTestId(0);
// ok
htmlElementSelector.findByLocatorId('id') satisfies Selector;
htmlElementSelector.findByTestId('id') satisfies Selector;
// ok
htmlElementSelector.findByLocatorId('id').findByLocatorId('id2') satisfies Selector;
htmlElementSelector.findByTestId('id').findByTestId('id2') satisfies Selector;
// ok
htmlElementSelector.findByLocatorId('id').find('.test-children') satisfies Selector;
htmlElementSelector.findByTestId('id').find('.test-children') satisfies Selector;
// ok
htmlElementSelector.find('body').findByLocatorId('id') satisfies Selector;
htmlElementSelector.find('body').findByTestId('id') satisfies Selector;

// ok
createSelector('id').findByLocatorId('id').find('body').findByLocatorId('id') satisfies Selector;
createSelector('id').findByTestId('id').find('body').findByTestId('id') satisfies Selector;

// ok
locator('id').findByLocatorId('id').find('body').findByLocatorId('id') satisfies Selector;
locator('id').findByTestId('id').find('body').findByTestId('id') satisfies Selector;

// @ts-expect-error: wrong number of arguments
locator();

// ok
htmlElementSelector.filterByLocatorId('id') satisfies Selector;
htmlElementSelector.filterByTestId('id') satisfies Selector;

// ok
htmlElementSelector.filterByLocatorParameter('prop', 'value') satisfies Selector;
// ok
htmlElementSelector.findByLocatorParameter('prop', 'value') satisfies Selector;

// ok
void htmlElementSelector.getLocatorId();
void htmlElementSelector.getTestId();

// @ts-expect-error: wrong number of arguments
void htmlElementSelector.getLocatorId('id');
void htmlElementSelector.getTestId('id');

// @ts-expect-error: TODO: should be ok
void htmlElementSelector.hasLocatorId() satisfies Promise<boolean>;
void htmlElementSelector.hasTestId() satisfies Promise<boolean>;

// @ts-expect-error: wrong number of arguments
void htmlElementSelector.hasLocatorId('id');
void htmlElementSelector.hasTestId('id');

// @ts-expect-error: wrong number of arguments
void htmlElementSelector.hasLocatorParameter();
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
},
"dependencies": {
"@playwright/test": "1.49.1",
"create-locator": "0.0.25",
"create-locator": "0.0.27",
"get-modules-graph": "0.0.11",
"globby": "11.1.0"
},
"devDependencies": {
"@playwright/browser-chromium": "1.49.1",
"@types/node": "22.10.2",
"@types/node": "22.10.5",
"@typescript-eslint/eslint-plugin": "7.18.0",
"@typescript-eslint/parser": "7.18.0",
"assert-modules-support-case-insensitive-fs": "1.0.1",
Expand All @@ -45,7 +45,7 @@
"eslint-plugin-typescript-sort-keys": "3.3.0",
"husky": "9.1.7",
"prettier": "3.4.2",
"typescript": "5.7.2"
"typescript": "5.7.3"
},
"peerDependencies": {
"@types/node": ">=20",
Expand Down
4 changes: 2 additions & 2 deletions src/constants/attributesOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type {AttributesOptions} from 'create-locator';
* Attributes options for locators.
* @internal
*/
export const attributesOptions = {
export const attributesOptions: AttributesOptions = {
parameterAttributePrefix: 'data-test-',
testIdAttribute: 'data-testid',
testIdSeparator: '-',
} as const satisfies AttributesOptions;
};
2 changes: 1 addition & 1 deletion src/createLocator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// eslint-disable-next-line no-restricted-syntax
export * from 'create-locator';
// eslint-disable-next-line import/no-internal-modules
export {createTestUtils} from 'create-locator/createTestUtils';
export {createTestLocator} from 'create-locator/createTestLocator';
export type {PARAMETERS} from 'create-locator/oldTypes';
// eslint-disable-next-line import/no-internal-modules
export {getCssSelectorFromAttributesChain} from 'create-locator/getCssSelectorFromAttributesChain';
26 changes: 16 additions & 10 deletions src/selectors/createSelectorFunction.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import {setCustomInspectOnFunction} from '../utils/fn';
import {DESCRIPTION_KEY} from '../constants/internal';
import {generalLog} from '../utils/generalLog';
import {createSelectorCreator} from '../utils/selectors';
import {createCustomMethods, createGetTrap, Selector as SelectorClass} from '../utils/selectors';
import {setReadonlyProperty} from '../utils/setReadonlyProperty';

import type {CreateSelector, CreateSelectorFunctionOptions} from '../types/internal';
import type {CreateSelector, CreateSelectorFunctionOptions, Selector} from '../types/internal';

/**
* Creates `createSelector` function.
*/
export const createSelectorFunction = ({
getLocatorAttributeName,
}: CreateSelectorFunctionOptions): CreateSelector => {
setCustomInspectOnFunction(getLocatorAttributeName);
generalLog('Create selector functions', {getLocatorAttributeName});
export const createSelectorFunction = (
attributesOptions: CreateSelectorFunctionOptions,
): CreateSelector => {
generalLog('Create selector function', {attributesOptions});

const createSelector = createSelectorCreator(getLocatorAttributeName);
const customMethods = createCustomMethods(attributesOptions);

return createSelector;
return (locator) => {
const selector = new SelectorClass(locator) as unknown as Selector;

setReadonlyProperty(selector, DESCRIPTION_KEY, locator);

return new Proxy(selector, {get: createGetTrap(customMethods)});
};
};
Loading

0 comments on commit c6f82dd

Please sign in to comment.