+
+
+
+
+ */
+
+ assert.equal(
+ Ex.getNextSibling(child1).getOrDie(),
+ document.getElementById("child2")! as ChildNode
+ );
+
+ assert.isTrue(Ex.getNextSibling(nestedChild).isNone());
+ });
+
+ it("getAttribute", () => {
+ const el = document.createElement("div");
+ el.setAttribute("id", "element");
+
+ assert.equal(
+ Ex.getAttribute(el, "id").getOrDie(),
+ el.getAttributeNode("id")
+ );
+ assert.isTrue(Ex.getAttribute(el, "di").isNone());
+ });
+
+ it("optionalDouble", () => {
+ assert.equal(Ex.optionalDouble(Optional.some(5)), 10);
+ assert.equal(Ex.optionalDouble(Optional.none()), 0);
+ });
+
+ it("trueIfSome", () => {
+ assert.isTrue(Ex.trueIfSome(Optional.some("hello")));
+ assert.isFalse(Ex.trueIfSome(Optional.none()));
+ });
+
+ it("getAgeOrDefault", () => {
+ assert.deepEqual(Ex.getAgeOrDefault(Optional.some({ age: 5 })), { age: 5 });
+ assert.deepEqual(Ex.getAgeOrDefault(Optional.none()), { age: 0 });
+ });
+
+ it("toPositiveInteger", () => {
+ // DONE: write a few test cases
+ assert.isTrue(Ex.toPositiveInteger(5).equals(Optional.some(5)));
+ assert.isTrue(Ex.toPositiveInteger(0).isNone());
+ assert.isTrue(Ex.toPositiveInteger(-1).isNone());
+ });
+
+ it("optionalToArray", () => {
+ assert.deepEqual(Ex.optionalToArray(Optional.some(10)), [10]);
+ assert.deepEqual(Ex.optionalToArray(Optional.some("hello")), ["hello"]);
+ assert.deepEqual(Ex.optionalToArray(Optional.none()), []);
+ });
+
+ it("arrayToOptional", () => {
+ assert.isTrue(Ex.arrayToOptional([10]).equals(Optional.some(10)));
+ assert.isTrue(Ex.arrayToOptional(["hello"]).equals(Optional.some("hello")));
+ assert.isTrue(Ex.arrayToOptional([]).isNone());
+ });
+
+ it("addThreeToOptional", () => {
+ assert.isTrue(
+ Ex.addThreeToOptional(Optional.some(10)).equals(Optional.some(13))
+ );
+ assert.isTrue(Ex.addThreeToOptional(Optional.none()).isNone());
+ });
+
+ it("prefixStrinigOptional", () => {
+ assert.isTrue(
+ Ex.prefixStringOptional(Optional.some(" world")).equals(
+ Optional.some("hello world")
+ )
+ );
+ assert.isTrue(Ex.prefixStringOptional(Optional.none()).isNone());
});
});
-// TODO: Now that you have finished all the test files in this directory,
+// DONE: Now that you have finished all the test files in this directory,
// try running all tests in the "part2" folder all using the `-d` argument in bedrock and specifying the parent directory.
diff --git a/src/test/ts/part3/Part3Ex1Test.ts b/src/test/ts/part3/Part3Ex1Test.ts
index b4e8bd7..a8f508c 100644
--- a/src/test/ts/part3/Part3Ex1Test.ts
+++ b/src/test/ts/part3/Part3Ex1Test.ts
@@ -1,7 +1,7 @@
-import { UiFinder } from '@ephox/agar';
-import { describe, it } from '@ephox/bedrock-client';
-import { TinyAssertions, TinyDom, TinyHooks } from '@ephox/mcagar';
-import { Editor } from 'tinymce';
+import { UiFinder } from "@ephox/agar";
+import { describe, it } from "@ephox/bedrock-client";
+import { TinyAssertions, TinyDom, TinyHooks } from "@ephox/mcagar";
+import { Editor } from "tinymce";
/*
In this part we're going to discuss working with the editor in a little more
@@ -9,14 +9,14 @@ detail. We'll look at this under the pretense of writing tests for editor
functionality, and try to sneak in some knowledge about the editor itself as we
go.
*/
-describe('Part3Ex1Test', () => {
+describe("Part3Ex1Test", () => {
/*
Enter the TinyHooks module, from mcagar (mc - Tiny[MC]E, agar - another
in-house testing library, more on that later). It lets you write the following.
*/
const hook = TinyHooks.bddSetup
({
// And then you put your settings in here
- toolbar: 'bold',
+ toolbar: "bold",
});
/*
NOTE: Even though we have assigned the result of our call to TinyHooks to a variable,
@@ -29,7 +29,7 @@ describe('Part3Ex1Test', () => {
TinyHooks takes care of a lot of important setup and teardown logic.
It's important to understand the lifecycle hooks and how TinyHooks utilises them.
- TODO: Take a look through Bedrock's docs and the BDD APIs available.
+ DONE: Take a look through Bedrock's docs and the BDD APIs available.
McAgar's BDD docs:
https://github.com/tinymce/tinymce/blob/develop/modules/mcagar/docs/bdd.md
@@ -38,7 +38,7 @@ describe('Part3Ex1Test', () => {
https://github.com/tinymce/tinymce/tree/develop/modules/mcagar/src/main/ts/ephox/mcagar/api/bdd
*/
- it('looks like an editor', () => {
+ it("looks like an editor", () => {
// it is safe to access the hook inside an "it" block.
const editor = hook.editor();
// Now what?
@@ -56,10 +56,16 @@ describe('Part3Ex1Test', () => {
UiFinder.exists(container, 'button[title="Bold"]');
});
- it('has content like an editor', () => {
+ it("has content like an editor", () => {
const editor = hook.editor();
- editor.setContent(/* TODO */ "Hello world
");
+ editor.setContent(
+ /* DONE - tinymce removes unstyled spans by default */
+ `
+ A paragraph with a span
+ A second paragraph
+ `
+ );
/*
Another useful module from mcagar, TinyAssertions is full of ways to make
@@ -68,11 +74,11 @@ describe('Part3Ex1Test', () => {
are numbers saying how many elements should match that selector inside the
editor.
- TODO: Edit the setContent call above to make this test pass.
+ DONE: Edit the setContent call above to make this test pass.
*/
TinyAssertions.assertContentPresence(editor, {
p: 2,
- span: 1
+ span: 1,
});
});
});
diff --git a/src/test/ts/part3/Part3Ex2Test.ts b/src/test/ts/part3/Part3Ex2Test.ts
index 3ed86e6..feb10e8 100644
--- a/src/test/ts/part3/Part3Ex2Test.ts
+++ b/src/test/ts/part3/Part3Ex2Test.ts
@@ -1,26 +1,33 @@
-import { Cursors } from '@ephox/agar';
-import { beforeEach, describe, it } from '@ephox/bedrock-client';
-import { TinyAssertions, TinyDom, TinyHooks, TinySelections } from '@ephox/mcagar';
-import { Traverse } from '@ephox/sugar';
-import { assert } from 'chai';
-import { Editor } from 'tinymce';
-
-describe('Part3Ex3Test', () => {
- const hook = TinyHooks.bddSetup({ height: '100vh' });
+import { Cursors } from "@ephox/agar";
+import { beforeEach, describe, it } from "@ephox/bedrock-client";
+import {
+ TinyAssertions,
+ TinyDom,
+ TinyHooks,
+ TinySelections,
+} from "@ephox/mcagar";
+import { Traverse } from "@ephox/sugar";
+import { assert } from "chai";
+import { Editor } from "tinymce";
+
+describe("Part3Ex3Test", () => {
+ const hook = TinyHooks.bddSetup({ height: "100vh" });
beforeEach(() => {
// Fun fact, hook.editor() also works inside of before / beforeEach
// callbacks, as long as you declare the hook first.
const editor = hook.editor();
- editor.setContent([
- 'Here is a bit of content
',
- 'And some bolded content
',
- '
'
- ].join(''));
+ editor.setContent(
+ [
+ "Here is a bit of content
",
+ "And some bolded content
",
+ '
',
+ ].join("")
+ );
});
- it('selects content like an editor', () => {
+ it("selects content like an editor", () => {
const editor = hook.editor();
/*
@@ -49,21 +56,27 @@ describe('Part3Ex3Test', () => {
const contentBody = TinyDom.body(editor);
// Let's get the And some bolded content
- const paragraph = Traverse.child(contentBody, 1).getOrDie('Unable to find second child of editor body');
+ const paragraph = Traverse.child(contentBody, 1).getOrDie(
+ "Unable to find second child of editor body"
+ );
// And then get the bolded (child 0 would be the text node "And some ")
- const strong = Traverse.child(paragraph, 1).getOrDie('Unable to find second child of paragraph');
+ const strong = Traverse.child(paragraph, 1).getOrDie(
+ "Unable to find second child of paragraph"
+ );
// And finally let's get the text node "bolded"
- const text = Traverse.child(strong, 0).getOrDie('Unable to find text inside strong element');
+ const text = Traverse.child(strong, 0).getOrDie(
+ "Unable to find text inside strong element"
+ );
range.setStart(text.dom, 0);
- range.setEnd(text.dom, 'bolded'.length);
+ range.setEnd(text.dom, "bolded".length);
editor.selection.setRng(range);
- assert.equal(editor.selection.getContent(), 'bolded');
+ assert.equal(editor.selection.getContent(), "bolded");
});
- it('selects content like an editor, take II', () => {
+ it("selects content like an editor, take II", () => {
const editor = hook.editor();
/*
Now in the last test, we put together a block of code that's a really good
@@ -80,7 +93,7 @@ describe('Part3Ex3Test', () => {
And here's how you'd do all of this with an agar API.
*/
const contentBody = TinyDom.body(editor);
- const text = Cursors.follow(contentBody, [ 1, 1, 0 ]).getOrDie();
+ const text = Cursors.follow(contentBody, [1, 1, 0]).getOrDie();
// Note that Cursors.follow only deals with DOM nodes, not offsets, and it uses the same node
// for both the selection start and end node
/*
@@ -93,24 +106,50 @@ describe('Part3Ex3Test', () => {
https://github.com/tinymce/tinymce/blob/develop/modules/mcagar/docs/bdd.md#tinyselections
*/
- TinySelections.setSelection(editor, [ 1, 1, 0 ], 0, [ 1, 1, 0 ], 'bolded'.length);
-
- assert.equal(editor.selection.getContent(), 'bolded');
+ TinySelections.setSelection(
+ editor,
+ [1, 1, 0],
+ 0,
+ [1, 1, 0],
+ "bolded".length
+ );
+
+ assert.equal(editor.selection.getContent(), "bolded");
});
- it('operates on the selection', () => {
+ it("operates on the selection", () => {
const editor = hook.editor();
- // TODO: move the selection to around the words "a bit of"
-
- // TODO: use an editor command to underline that content (https://www.tiny.cloud/docs/advanced/editor-command-identifiers/)
-
- TinyAssertions.assertContent(editor, [
- 'Here is a bit of content
',
- 'And some bolded content
',
- '
'
- ].join('\n'));
-
- // TODO: Write an assertion to test your changes (hint: TinyAssertions)
+ // DONE: move the selection to around the words "a bit of"
+ TinySelections.setSelection(
+ editor,
+ [0, 0],
+ 8,
+ [0, 0],
+ 8 + "a bit of".length
+ );
+
+ // DONE: use an editor command to underline that content (https://www.tiny.cloud/docs/advanced/editor-command-identifiers/)
+ editor.execCommand("Underline");
+
+ TinyAssertions.assertContent(
+ editor,
+ [
+ 'Here is a bit of content
',
+ "And some bolded content
",
+ '
',
+ ].join("\n")
+ );
+
+ // DONE: Write an assertion to test your changes (hint: TinyAssertions)
+ editor.execCommand("Bold");
+ TinyAssertions.assertContent(
+ editor,
+ [
+ 'Here is a bit of content
',
+ "And some bolded content
",
+ '
',
+ ].join("\n")
+ );
});
});