Skip to content

Commit

Permalink
Merge pull request #20685 from Yoast/20675-replacement-variable-edito…
Browse files Browse the repository at this point in the history
…r-replacement-variable-editortestsreplacementvariableeditortestjs

20675 replacement variable editor replacement variable editortestsreplacementvariableeditortestjs
  • Loading branch information
igorschoester authored Oct 2, 2023
2 parents 2184cbd + 3cc1c6e commit 704cfc5
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 127 deletions.
5 changes: 0 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ overrides:
react/default-props-match-prop-types: 1
react/no-unused-prop-types: 1
require-jsdoc: 1
- files:
- "packages/replacement-variable-editor/**/*.js"
rules:
react/jsx-no-bind: 1
react/require-default-props: 1
- files:
- "packages/search-metadata-previews/**/*.js"
rules:
Expand Down
51 changes: 51 additions & 0 deletions packages/replacement-variable-editor/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module.exports = {
root: true,
"extends": [
"yoast",
],
settings: {
react: {
version: "detect",
},
},
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
ignorePatterns: [
"/build/",
"/coverage/",
"/dist/",
],
rules: {
"no-prototype-builtins": 0,
"comma-dangle": [
"error",
{
arrays: "always-multiline",
objects: "always-multiline",
imports: "always-multiline",
exports: "always-multiline",
functions: "never",
},
],
},
overrides: [
{
files: [ "tests/**/*.js" ],
env: {
jest: true,
},
rules: {
"no-restricted-imports": 0,
},
},
{
files: [ "**/*.js" ],
rules: {
"react/jsx-no-bind": 1,
"react/require-default-props": 1,
},
},
],
};
11 changes: 8 additions & 3 deletions packages/replacement-variable-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@
"styled-components": "^5.3.6"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/jest-dom": "^6.1.3",
"@testing-library/react": "^14.0.0",
"@testing-library/react-hooks": "^8.0.1",
"eslint": "^7.32.0",
"eslint-config-yoast": "^6.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-react": "^7.29.4",
"jest-styled-components": "^7.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-test-renderer": "^18.2.0"
"react-dom": "^18.2.0",
"react-test-renderer": "^18.2.0"
},
"peerDependencies": {
"@draft-js-plugins/mention": "^5.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable camelcase */
// External dependencies.
import React from "react";
import Editor from "@draft-js-plugins/editor";
Expand Down Expand Up @@ -515,7 +516,7 @@ class ReplacementVariableEditorStandalone extends React.Component {
*
* @returns {void}
*/
componentWillReceiveProps( nextProps ) {
UNSAFE_componentWillReceiveProps( nextProps ) {
const { content, replacementVariables, recommendedReplacementVariables } = this.props;
const { searchValue } = this.state;
const nextState = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,127 +1,139 @@
/* eslint-disable react/jsx-no-bind */
// import ReplacementVariableEditorStandalone, { ReplacementVariableEditorStandaloneInnerComponent }
// from "../src/ReplacementVariableEditorStandalone";
// import React from "react";
import React from "react";
import { render } from "./test-utils";
import ReplacementVariableEditorStandalone, { ReplacementVariableEditorStandaloneInnerComponent }
from "../src/ReplacementVariableEditorStandalone";

// jest.mock( "draft-js/lib/generateRandomKey", () => () => {
// let randomKey = global._testDraftJSRandomNumber;
jest.mock( "draft-js/lib/generateRandomKey", () => () => {
let randomKey = global._testDraftJSRandomNumber;

// if ( ! randomKey ) {
// randomKey = 0;
// }
if ( ! randomKey ) {
randomKey = 0;
}

// randomKey++;
// global._testDraftJSRandomNumber = randomKey;
randomKey++;
global._testDraftJSRandomNumber = randomKey;

// return randomKey + "";
// } );
return randomKey + "";
} );

describe( "ReplacementVariableEditor", () => {
describe( "ReplacementVariableEditorStandalone", () => {
it( "wraps a Draft.js editor instance", () => {

const { container } = render(
<ReplacementVariableEditorStandalone
replacementVariables={ [] }
content="Dummy content"
// eslint-disable-next-line react/jsx-no-bind
onChange={ () => {} }
ariaLabelledBy="id"
fieldId="test-field-id"
theme={ { isRtl: "false" } }
/>
);

expect( container ).toMatchSnapshot();
} );
} );

// describe( "suggestionsFilter", () => {
// let searchValue, replacementVariables, suggestions, replacementVariablesEditor, expected;

// beforeEach( () => {
// searchValue = "cat";
// replacementVariables = [
// {
// name: "category",
// label: "Category",
// value: "uncategorized",
// },
// {
// name: "primary_category",
// label: "Primary category",
// value: "uncategorized",
// },
// {
// name: "category_description",
// label: "Category description",
// value: "uncategorized",
// },
// {
// name: "date",
// label: "Date",
// value: "May 30, 2018",
// },
// ];

// const props = {
// replacementVariables,
// content: "Dummy content",
// onChange: () => {},
// ariaLabelledBy: "id",
// theme: { isRtl: false },
// };

// replacementVariablesEditor = new ReplacementVariableEditorStandaloneInnerComponent( props );

// suggestions = replacementVariablesEditor.mapReplacementVariablesToSuggestions( props.replacementVariables );

// expected = [
// {
// replaceName: "category",
// label: "Category",
// name: "Category",
// value: "uncategorized",
// },
// {
// replaceName: "category_description",
// label: "Category description",
// name: "Category description",
// value: "uncategorized",
// },
// ];
// } );

// it( "Replacement variables are correctly mapped to suggestions by mapReplacementVariablesToSuggestions.", () => {
// expected = [
// {
// replaceName: "category",
// label: "Category",
// name: "Category",
// value: "uncategorized",
// },
// {
// replaceName: "primary_category",
// label: "Primary category",
// name: "Primary category",
// value: "uncategorized",
// },
// {
// replaceName: "category_description",
// label: "Category description",
// name: "Category description",
// value: "uncategorized",
// },
// {
// replaceName: "date",
// label: "Date",
// name: "Date",
// value: "May 30, 2018",
// },
// ];

// const actual = replacementVariablesEditor.mapReplacementVariablesToSuggestions( replacementVariables );

// expect( actual ).toEqual( expected );
// } );

// it( "Returns only the replacement variables where the start of the name matches with the search value.", () => {
// const actual = replacementVariablesEditor.suggestionsFilter( searchValue, suggestions );

// expect( actual ).toEqual( expected );
// } );

// it( "Returns the matching replacement variables, regardless of upper- or lowercase in the search value.", () => {
// searchValue = "Cat";

// const actual = replacementVariablesEditor.suggestionsFilter( searchValue, suggestions );

// expect( actual ).toEqual( expected );
// } );
// } );
describe( "suggestionsFilter", () => {
let searchValue, replacementVariables, suggestions, replacementVariablesEditor, expected;

beforeEach( () => {
searchValue = "cat";
replacementVariables = [
{
name: "category",
label: "Category",
value: "uncategorized",
},
{
name: "primary_category",
label: "Primary category",
value: "uncategorized",
},
{
name: "category_description",
label: "Category description",
value: "uncategorized",
},
{
name: "date",
label: "Date",
value: "May 30, 2018",
},
];

const props = {
replacementVariables,
content: "Dummy content",
onChange: () => {},
ariaLabelledBy: "id",
theme: { isRtl: false },
};
suggestions = [
{ name: "cat", value: "meow" },
];
replacementVariablesEditor = new ReplacementVariableEditorStandaloneInnerComponent( props );
suggestions = replacementVariablesEditor.mapReplacementVariablesToSuggestions( props.replacementVariables );
expected = [
{
replaceName: "category",
label: "Category",
name: "Category",
value: "uncategorized",
},
{
replaceName: "category_description",
label: "Category description",
name: "Category description",
value: "uncategorized",
},
];
} );

it( "Replacement variables are correctly mapped to suggestions by mapReplacementVariablesToSuggestions.", () => {
expected = [
{
replaceName: "category",
label: "Category",
name: "Category",
value: "uncategorized",
},
{
replaceName: "primary_category",
label: "Primary category",
name: "Primary category",
value: "uncategorized",
},
{
replaceName: "category_description",
label: "Category description",
name: "Category description",
value: "uncategorized",
},
{
replaceName: "date",
label: "Date",
name: "Date",
value: "May 30, 2018",
},
];

const actual = replacementVariablesEditor.mapReplacementVariablesToSuggestions( replacementVariables );

expect( actual ).toEqual( expected );
} );

it( "Returns only the replacement variables where the start of the name matches with the search value.", () => {
const actual = replacementVariablesEditor.suggestionsFilter( searchValue, suggestions );

expect( actual ).toEqual( expected );
} );

it( "Returns the matching replacement variables, regardless of upper- or lowercase in the search value.", () => {
searchValue = "Cat";

const actual = replacementVariablesEditor.suggestionsFilter( searchValue, suggestions );

expect( actual ).toEqual( expected );
} );
} );
Loading

0 comments on commit 704cfc5

Please sign in to comment.