Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 86b4d1f

Browse files
committed
Merge pull request #7132 from adobe/nj/cmv4-unit-test-fix
[cmv4] Fix unit tests for getSelectedText() to reflect new behavior
2 parents 969eefe + ceda080 commit 86b4d1f

2 files changed

Lines changed: 26 additions & 13 deletions

File tree

src/editor/Editor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,8 +1042,8 @@ define(function (require, exports, module) {
10421042
* Returns the currently selected text, or "" if no selection. Includes \n if the
10431043
* selection spans multiple lines (does NOT reflect the Document's line-endings style). By
10441044
* default, returns only the contents of the primary selection, unless `allSelections` is true.
1045-
* @param {boolean=} allSelections Whether to return the contents of all selections instead
1046-
* of just the primary selection. Default false.
1045+
* @param {boolean=} allSelections Whether to return the contents of all selections (separated
1046+
* by newlines) instead of just the primary selection. Default false.
10471047
* @return {!string} The selected text.
10481048
*/
10491049
Editor.prototype.getSelectedText = function (allSelections) {

test/spec/Editor-test.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -605,27 +605,40 @@ define(function (require, exports, module) {
605605
it("should return empty string for a cursor", function () {
606606
myEditor._codeMirror.setCursor(0, 2);
607607
expect(myEditor.getSelectedText()).toEqual("");
608+
expect(myEditor.getSelectedText(true)).toEqual("");
608609
});
609610

610611
it("should return the contents of a single selection", function () {
611-
myEditor._codeMirror.setSelection({line: 0, ch: 8}, {line: 0, ch: 12});
612-
expect(myEditor.getSelectedText()).toEqual("line");
612+
myEditor._codeMirror.setSelection({line: 0, ch: 8}, {line: 0, ch: 14});
613+
expect(myEditor.getSelectedText()).toEqual("line 0");
614+
expect(myEditor.getSelectedText(true)).toEqual("line 0");
613615
});
614616

615-
it("should return the contents of a multiple selection, separated by newlines", function () {
616-
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 12}},
617-
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 12}},
618-
{anchor: {line: 2, ch: 8}, head: {line: 2, ch: 12}}
617+
it("should return the primary selection by default, but concatenate contents if allSelections is true", function () {
618+
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}},
619+
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}},
620+
{anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}}
619621
]);
620-
expect(myEditor.getSelectedText()).toEqual("line\nline\nline");
622+
expect(myEditor.getSelectedText()).toEqual("line 2");
623+
expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2");
624+
});
625+
626+
it("should return a primary selection other than the last", function () {
627+
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}},
628+
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}},
629+
{anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}}
630+
], 1);
631+
expect(myEditor.getSelectedText()).toEqual("line 1");
632+
expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2");
621633
});
622634

623635
it("should return the contents of a multiple selection when some selections are reversed", function () {
624-
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 12}, head: {line: 0, ch: 8}},
625-
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 12}},
626-
{anchor: {line: 2, ch: 12}, head: {line: 2, ch: 8}}
636+
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 14}, head: {line: 0, ch: 8}},
637+
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}},
638+
{anchor: {line: 2, ch: 14}, head: {line: 2, ch: 8}}
627639
]);
628-
expect(myEditor.getSelectedText()).toEqual("line\nline\nline");
640+
expect(myEditor.getSelectedText()).toEqual("line 2");
641+
expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2");
629642
});
630643
});
631644

0 commit comments

Comments
 (0)