Skip to content

Commit

Permalink
Fixed review comments for null node test and callback test
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuntala-motukuri committed Aug 26, 2024
1 parent 45fe9fc commit ad6d7b8
Showing 1 changed file with 90 additions and 11 deletions.
101 changes: 90 additions & 11 deletions plugins/async-node/ios/Tests/AsynNodePluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,93 @@ class AsyncNodePluginTests: XCTestCase {
XCTAssertEqual(expectedMultiNode2Text, "new node from the hook 2")
}

func handleEmptyNode() {
let context = JSContext()
let plugin = AsyncNodePlugin { _,_ in
return .emptyNode
}
plugin.context = context
func testHandleEmptyNode() {
let handlerExpectation = XCTestExpectation(description: "first data did not change")

guard let context = JSContext() else {
XCTFail("JSContext initialization failed")
return

Check warning on line 301 in plugins/async-node/ios/Tests/AsynNodePluginTests.swift

View check run for this annotation

Codecov / codecov/patch

plugins/async-node/ios/Tests/AsynNodePluginTests.swift#L300-L301

Added lines #L300 - L301 were not covered by tests
}

var count = 0
var args: [JSValue] = []
var callbackFunction: JSValue?

let result = plugin.handleAsyncNodeReplacement(.emptyNode)
XCTAssertNil(result)
let resolve: AsyncHookHandler = { node, callback in
handlerExpectation.fulfill()
callbackFunction = callback

return .singleNode(.concrete(context.evaluateScript("""
(
{"asset": {"id": "text", "type": "text", "value":"new node from the hook 1"}}
)
""") ?? JSValue()))
}

let asyncNodePluginPlugin = AsyncNodePluginPlugin()
let plugin = AsyncNodePlugin(plugins: [asyncNodePluginPlugin], resolve)

plugin.context = context

XCTAssertNotNil(asyncNodePluginPlugin.context)

let player = HeadlessPlayerImpl(plugins: [ReferenceAssetsPlugin(), plugin], context: context)

let textExpectation = XCTestExpectation(description: "newText found")
let textExpectation2 = XCTestExpectation(description: "newText found")

var expectedMultiNode1Text: String = ""
var expectedMultiNode2Text: String = ""

player.hooks?.viewController.tap({ (viewController) in
viewController.hooks.view.tap { (view) in
view.hooks.onUpdate.tap { val in
count += 1

if count == 2 {
let newText1 = val
.objectForKeyedSubscript("values")
.objectAtIndexedSubscript(1)
.objectForKeyedSubscript("asset")
.objectForKeyedSubscript("value")
guard let textString1 = newText1?.toString() else { return XCTFail("newText was not a string") }

expectedMultiNode1Text = textString1
textExpectation.fulfill()
}

if count == 3 {
let newText2 = val
.objectForKeyedSubscript("values")
.objectAtIndexedSubscript(1)
.objectForKeyedSubscript("asset")
.objectForKeyedSubscript("value")
guard let textString2 = newText2?.toString() else { return XCTFail("newText was not a string") }

expectedMultiNode2Text = textString2
textExpectation2.fulfill()
}
}
}
})

player.start(flow: .asyncNodeJson, completion: { _ in})

wait(for: [handlerExpectation, textExpectation], timeout: 5)

XCTAssert(count == 2)
XCTAssertEqual(expectedMultiNode1Text, "new node from the hook 1")

let replacementResult = AsyncNodeHandlerType.emptyNode

args = [plugin.handleAsyncNodeReplacement(replacementResult) ?? JSValue()]

let _ = callbackFunction?.call(withArguments: args)

XCTAssert(count == 3)
XCTAssertEqual(expectedMultiNode2Text, "undefined")
}


func testHandleMultipleUpdatesThroughCallback() {

Expand Down Expand Up @@ -340,6 +416,7 @@ func testHandleMultipleUpdatesThroughCallback() {

let textExpectation = XCTestExpectation(description: "newText found")
let textExpectation2 = XCTestExpectation(description: "newText found")
let textExpectation3 = XCTestExpectation(description: "newText found")

var expectedMultiNode1Text: String = ""
var expectedMultiNode2Text: String = ""
Expand Down Expand Up @@ -377,10 +454,12 @@ func testHandleMultipleUpdatesThroughCallback() {

if count == 4 {

let newText3 = val
let newText3 = val
.objectForKeyedSubscript("values")
.objectAtIndexedSubscript(0)
.objectForKeyedSubscript("asset")
.objectForKeyedSubscript("label")
.objectForKeyedSubscript("asset")
.objectForKeyedSubscript("value")
guard let textString3 = newText3?.toString() else { return XCTFail("newText was not a string") }

Expand All @@ -393,7 +472,7 @@ func testHandleMultipleUpdatesThroughCallback() {

expectedMultiNode3Text = textString3
expectedMultiNode4Text = textString4
textExpectation2.fulfill()
textExpectation3.fulfill()
}
}
}
Expand Down Expand Up @@ -430,7 +509,7 @@ func testHandleMultipleUpdatesThroughCallback() {

XCTAssert(count == 4)
// asset that the value at index 0 for the object
XCTAssertEqual(expectedMultiNode3Text, "undefined")
XCTAssertEqual(expectedMultiNode3Text, "test")
XCTAssertEqual(expectedMultiNode4Text, "undefined")
}
}
Expand Down

0 comments on commit ad6d7b8

Please sign in to comment.