-
Notifications
You must be signed in to change notification settings - Fork 912
/
Copy pathReplacementVariableEditorTest.js
139 lines (122 loc) · 3.45 KB
/
ReplacementVariableEditorTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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;
if ( ! randomKey ) {
randomKey = 0;
}
randomKey++;
global._testDraftJSRandomNumber = randomKey;
return randomKey + "";
} );
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 },
};
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 );
} );
} );