Skip to content

Commit

Permalink
Unit test for image plugin
Browse files Browse the repository at this point in the history
Summary: I forgot to include this file in D47215002

Reviewed By: thegreatercurve

Differential Revision: D47226955

fbshipit-source-id: 5c123f200bf2f31db076c43b1bec43c5a9fa7680
Amy Worrall authored and facebook-github-bot committed Jul 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c8239c4 commit 352a128
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

@testable import Lexical
@testable import LexicalInlineImagePlugin
import XCTest

class InlineImageTests: XCTestCase {
var view: LexicalView?
var editor: Editor {
get {
guard let editor = view?.editor else {
XCTFail("Editor unexpectedly nil")
fatalError()
}
return editor
}
}

override func setUp() {
view = LexicalView(editorConfig: EditorConfig(theme: Theme(), plugins: [InlineImagePlugin()]), featureFlags: FeatureFlags())
}

override func tearDown() {
view = nil
}

func testNewParaAfterImage() throws {
try editor.update {
let imageNode = ImageNode(url: "https://example.com/image.png", size: CGSize(width: 300, height: 300), sourceID: "")
let textNode1 = TextNode(text: "123")
let textNode2 = TextNode(text: "456")
if let selection = try getSelection() {
_ = try selection.insertNodes(nodes: [textNode1, imageNode, textNode2], selectStart: false)
}

guard let root = getRoot() else {
XCTFail()
return
}
XCTAssertEqual(root.getChildrenSize(), 1, "Root should have 1 child (paragraph)")

let newSelection = RangeSelection(anchor: Point(key: textNode2.getKey(), offset: 0, type: .text), focus: Point(key: textNode2.getKey(), offset: 0, type: .text), format: TextFormat())
try newSelection.insertParagraph()

XCTAssertEqual(root.getChildrenSize(), 2, "Root should now have 2 paragraphs")

let firstPara = root.getChildren()[0] as? ParagraphNode
let secondPara = root.getChildren()[1] as? ParagraphNode

guard let firstPara, let secondPara else {
XCTFail()
return
}

XCTAssertEqual(firstPara.getChildrenSize(), 2, "First para should contain 1 text node and 1 image node")
XCTAssertEqual(secondPara.getChildrenSize(), 1, "Second para should contain 1 text node")
}
}
}

0 comments on commit 352a128

Please sign in to comment.